Partager via


Implement continuous deployment of your app to an Azure Virtual Machine Scale Set

TFS 2017

Note

In Microsoft Team Foundation Server (TFS) 2018 and previous versions, build and release pipelines are called definitions, runs are called builds, service connections are called service endpoints, stages are called environments, and jobs are called phases.

The Build Machine Image task makes it easy for users who are new to immutable VHD-based deployments to use Packer without learning concepts such as provisioners and builders. If you are deploying to virtual machines by using deployment scripts, you can use this task for either creating new virtual machine instances or for creating and updating virtual machine scale sets.

The autogenerate mode of the task generates the Packer configuration with:

  • Builder for Azure
  • Provisioners that depend on the type of base operating system selected. For Linux, this is shell script. For Windows, this is PowerShell script. The deployment script provided is used by the provisioner.

A custom Packer configuration JSON file can also be used.

Get set up

Begin with a CI build

Before you begin, you need a CI build that creates your app. To set up CI, see:

Create the release pipeline

  1. Open the Releases tab of Azure Pipelines and choose the "+" icon to create a new release pipeline.

  2. In the Create release pipeline dialog, select the Empty template and choose Next.

  3. In the next page, select the build pipeline you created earlier and choose Create. This creates a new release pipeline with one default stage.

  4. In the new release pipeline, select + Add tasks and add these tasks:

    • Build Machine Image
    • Azure PowerShell

    The Build Machine Image uses Packer to create a VHD. The entire process is:

    • Create a new virtual machine with the selected base operating system
    • Install all the prerequisites and the application on the VM by using a deployment script
    • Create a VHD and store it in the Azure storage account
    • Delete the new virtual machine that was created

  5. Configure the Build Machine Image task as follows:

    Build Machine Image Deploy: Build Machine Image - Build machine image using Packer.

    • Packer template: You can use your own packer configuration JSON file or use the autogenerate feature where the task generates a packer template for you. This example uses the autogenerated packer configuration.

    • Azure subscription: Select a connection from the list under Available Azure Service Connections or create a more restricted permissions connection to your Azure subscription. For more information, see Azure Resource Manager service connection.

    • Storage location: The location of storage account where the VHD will be stored. This should be the same location where the virtual machine scale set is located, or where it will be created.

    • Base Image Source: You can choose from either a curated gallery of OS images, or provide the URL of your custom image. For example, Ubuntu 16.04 LTS

    • Deployment Package: Specify the path of the deployment package directory relative to $(System.DefaultWorkingDirectory). For example, $(System.DefaultWorkingDirectory)/Packer-NodeJs/drop

    • Deployment Script: Specify the relative path to the PowerShell script (for Windows) or shell script (for Linux) that deploys the package. This script should be within the deployment package path selected above. For example, Deploy/ubuntu/deployNodejsApp.sh. The script may need to install Curl, Node.js, NGINX, and PM2; copy the application; and then configure NGINX and PM2 to run the application.

    • Output - Image URL: Provide a name for the output variable that will hold the URL of the generated machine image. For example, bakedImageUrl

    Azure PowerShell Deploy: Azure PowerShell - Run a PowerShell script to update the Virtual Machine Scale Set with the new VHD.

    • Azure Connection Type: Select Azure Resource Manager

    • Azure RM Subscription: Select a connection from the list under Available Azure Service Connections or create a more restricted permissions connection to your Azure subscription. For more information, see Azure Resource Manager service connection.

    • Script type: Select Inline Script

    • Inline Script: Enter the script shown below to update the virtual machine scale set.

    Use the following script for the Inline Script parameter of the Azure PowerShell task:

    # get the VMSS model
    
    $vmss = Get-AzureRmVmss -ResourceGroupName resource_group_name -VMScaleSetName VM_scale_set_name
    
    # set the new version in the model data
    
    $vmss.virtualMachineProfile.storageProfile.osDisk.image.uri="$(bakedImageUrl)"
    
    # update the virtual machine scale set model
    
    Update-AzureRmVmss -ResourceGroupName resource_group_name -Name resource_group_name -VirtualMachineScaleSet $vmss
    

    You can use variables to pass values such as the resource group and virtual machine scale set names to the script if you wish.

  6. In the Deployment conditions dialog for the stage, ensure that the Trigger section is set to After release creation.

  7. Enter a name for the release pipeline and save it.

  8. Create a new release, select the latest build, and ensure that the application has been deployed correctly and has generated the VHD.

FAQ

I use TFS on-premises and I don't see some of these features. Why not?

Some of these features are available only on Azure Pipelines and not yet available on-premises. Some features are available on-premises if you have upgraded to the latest version of TFS.

Help and support