A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/Azure/build-vm-image below:

Azure/build-vm-image: Github action to create custom virtual machine images that contain artifacts built in CI workflows

GitHub Action to Build Custom Virtual Machine Images

With the Build Azure Virtual Machine Image action, you can now create custom virtual machine images that contain artifacts produced in your CI/CD workflows and have pre-installed software. This action not only lets you build customized images but also distribute them using image managing Azure services like Shared Image Gallery. These images can then be used for creating Virtual Machines or Virtual Machine Scale Sets

The definition of this Github Action is in action.yml.

Note that this action uses the Azure Image Builder service in the background for creating and publishing images.

{
    "Name": "Image Creation Role",
    "IsCustom": true,
    "Description": "Azure Image Builder access to create resources for the image build",
    "Actions": [
        "Microsoft.Compute/galleries/read",
        "Microsoft.Compute/galleries/images/read",
        "Microsoft.Compute/galleries/images/versions/read",
        "Microsoft.Compute/galleries/images/versions/write",

        "Microsoft.Compute/images/write",
        "Microsoft.Compute/images/read",
        "Microsoft.Compute/images/delete"
    ],
    "NotActions": [
  
    ],
    "AssignableScopes": [
      "/subscriptions/<subscriptionID>/resourceGroups/<rgName>"
    ]
  }

Learn more about configuring permissions for Azure Image builder Service using Powershell or Azure CLI.

End-to-End Sample Workflows Sample workflow to create a custom Windows OS image and distribute it as a Managed Image
name: create_custom_windows_image

on: push

jobs:
  BUILD-CUSTOM-IMAGE:
    runs-on: ubuntu-latest    
    steps:
    - name: CHECKOUT
      uses: actions/checkout@v2
  

    - name: AZURE LOGIN 
      uses: azure/login@v1
      with:
        creds: ${{secrets.AZURE_CREDENTIALS}}

    - name: BUILD WEBAPP
      run: sudo ${{ GITHUB.WORKSPACE }}/webApp/buildscript.sh # Runs necessary build scripts and copies built artifacts to  ${{ GITHUB.WORKSPACE }}/workflow-artifacts
      

    - name: BUILD-CUSTOM-VM-IMAGE      
      uses: azure/build-vm-image@v0
      with:        
        resource-group-name: 'myResourceGroup'
        managed-identity: 'myImageBuilderIdentity'
        location: 'eastus2'
        source-os-type: 'windows'        
        source-image: MicrosoftWindowsServer:WindowsServer:2019-Datacenter:latest        
        customizer-script: |
          & 'c:\workflow-artifacts\webApp\webconfig.ps1'

The above workflow will use a Microsoft Windows Server platform image as base image, inject files present in directory ${{ GITHUB.WORKSPACE }}/worflow-artifacts of GitHub runner into the base image at default customizer-destination directory and run image customizations(E.g. Set up IIS web server, configure bindings etc) using script webconfig.ps1, finally it will distribute the baked custom image as a Managed Image(default distribution)

Sample workflow to create a custom Ubuntu OS image and distribute it as Managed Image
on: push

jobs:      
  job1:
    runs-on: ubuntu-latest
    name: Create Custom Linux Image
    steps:
    - name: Checkout
      uses: actions/checkout@v2
    
    - name: Create Workflow Artifacts
      run: |
        cd  "$GITHUB_WORKFLOW"
        mkdir worflow-artifacts/        
        echo "echo Installing World... " > $GITHUB_WORKSPACE/workflow-artifacts/install-world.sh  # You can have your own installation script here

    
    - name: Login via Az module
      uses: azure/login@v1
      with:
        creds: ${{secrets.AZURE_CREDENTIALS}}
    
    - name: Build and Distribute Custom VM Image      
      uses: azure/build-vm-image@v0
      with:        
        resource-group-name: 'myResourceGroup'
        location: 'eastus2'
        managed-identity: 'myImageBuilderIdentity'
        source-os-type: 'linux'
        source-image-type: 'PlatformImage'
        source-image: Canonical:UbuntuServer:18.04-LTS:latest 
        customizer-source: ${{ GITHUB.WORKSPACE }}/workflow-artifacts
        customizer-script: |
          sudo mkdir /buildArtifacts
          sudo cp -r /tmp/ /buildArtifacts/
          sh /buildArtifacts/workflow-artifacts/install-world.sh

        

The above workflow will use a linux platform image as base image, inject files present in directory ${{ GITHUB.WORKSPACE }}/worflow-artifacts of GitHub runner into the base image at default customizer-destination directory and run install-world.sh script. Finally it will distribute the baked custom image as a Managed Image(default distribution)

Sample workflow to create a custom Ubuntu OS image and distribute through Shared Image Gallery
on: push

jobs:
  BUILD-CUSTOM-UBUNTU-IMAGE:
    runs-on: ubuntu-latest    
    steps:
    - name: CHECKOUT
      uses: actions/checkout@v2
  

    - name: AZURE LOGIN 
      uses: azure/login@v1
      with:
        creds: ${{secrets.AZURE_CREDENTIALS}}

    - name: BUILD WEBAPP
      run: sudo ${{ GITHUB.WORKSPACE }}/webApp/buildscript.sh # Run necessary build scripts and copies built artifacts to  ${{ GITHUB.WORKSPACE }}/workflow-artifacts
      

    - name: BUILD-CUSTOM-VM-IMAGE      
      uses: azure/build-vm-image@v0
      with:        
        resource-group-name: 'myResourceGroup'
        managed-identity: 'myImageBuilderIdentity'
        location: 'eastus2'
        source-os-type: 'linux'        
        source-image: Canonical:UbuntuServer:18.04-LTS:latest      
        customizer-script: |
          sh /tmp/workflow-artifacts/install.sh
        dist-type: 'SharedImageGallery'
        dist-resource-id: '/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myResourceGroup/providers/Microsoft.Compute/galleries/AppTeam/images/ImagesWithApp'
        dist-location: 'eastus2'
          
        

The above workflow will use a linux platform image as base image, inject files present in directory ${{ GITHUB.WORKSPACE }}/worflow-artifacts of GitHub runner into the base image at default customizer-destination directory and run install.sh script. Finally it will distribute the baked custom image through Shared Image Gallery

Snippet to spin up a virtual machine from custom image

You can easily create a Virtual Machine using AZ CLI commands. Here is a simple example to do it using GitHub workflows.

- name: CREATE VM
  uses: azure/CLI@v1
  with:
    azcliversion: 2.0.72
    inlineScript: |
      az vm create --resource-group myResourceGroup  --name "app-vm-${{ GITHUB.RUN_NUMBER }}"  --admin-username vmUserName --admin-password "${{ secrets.VM_PWD }}" --location  eastus2 \
      --image "${{ steps.<workflow_step_id>.outputs.custom-image-uri }}"              

You can also take a look at the end to end tutorial that describes how to use this action and also create a Virtual machine from customized image.

Configure credentials for Azure login action:

With the Azure login Action, you can perform an Azure login using Azure service principal. The credentials of Azure Service Principal can be added as secrets in the GitHub repository and then used in the workflow. Follow the below steps to generate credentials and store in github.

  
   az ad sp create-for-rbac --name "myApp" --role contributor \
                            --scopes /subscriptions/{subscription-id} \
                            --sdk-auth
                            
  # Replace {subscription-id} with the subscription identifiers
  
  # The command should output a JSON object similar to this:

  {
    "clientId": "<GUID>",
    "clientSecret": "<GUID>",
    "subscriptionId": "<GUID>",
    "tenantId": "<GUID>",
    (...)
  }
  

If needed, you can modify the Azure CLI command to further reduce the scope for which permissions are provided. Here is the command that gives contributor access to only a resource group.

  
   az ad sp create-for-rbac --name "myApp" --role contributor \
                            --scopes /subscriptions/{subscription-id}/resourceGroups/{resource-group} \
                            --sdk-auth
                            
  # Replace {subscription-id}, {resource-group} with the subscription and resource group identifiers.
  

You can also provide permissions to multiple scopes using the Azure CLI command:

  
   az ad sp create-for-rbac --name "myApp" --role contributor \
                            --scopes /subscriptions/{subscription-id}/resourceGroups/{resource-group1} \
                            /subscriptions/{subscription-id}/resourceGroups/{resource-group2} \
                            --sdk-auth
                            
  # Replace {subscription-id}, {resource-group1}, {resource-group2} with the subscription and resource group identifiers.
  

If you have any changes you’d like to see or suggestions for this action, we’d love your feedback ❤️ . Please feel free to raise a GitHub issue in this repository describing your suggestion. This would enable us to label and track it properly. You can do the same if you encounter a problem with the feature as well.

This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit https://cla.opensource.microsoft.com.

When you submit a pull request, a CLA bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., status check, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA.

This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4