|
| 1 | +--- |
| 2 | +title: Continuous integration with Azure Pipelines | Microsoft Docs |
| 3 | +description: Learn how to continuously build, test, and deploy Azure Resource Manager templates. |
| 4 | +services: azure-resource-manager |
| 5 | +documentationcenter: '' |
| 6 | +author: mumian |
| 7 | +manager: carmonm |
| 8 | +editor: |
| 9 | + |
| 10 | +ms.service: azure-resource-manager |
| 11 | +ms.workload: multiple |
| 12 | +ms.tgt_pltfrm: na |
| 13 | +ms.devlang: na |
| 14 | +ms.date: 06/10/2019 |
| 15 | +ms.topic: tutorial |
| 16 | +ms.author: jgao |
| 17 | +--- |
| 18 | + |
| 19 | +# Tutorial: Continuous integration of Azure Resource Manager templates with Azure Pipelines |
| 20 | + |
| 21 | +Learn how to use Azure Pipelines to continuously build and deploy Azure Resource Manager template projects. |
| 22 | + |
| 23 | +Azure DevOps provides developer services to support teams to plan work, collaborate on code development, and build and deploy applications. Developers can work in the cloud using Azure DevOps Services. Azure DevOps provides an integrated set of features that you can access through your web browser or IDE client. Azure Pipeline is one of these features. Azure Pipelines is a fully featured continuous integration (CI) and continuous delivery (CD) service. It works with your preferred Git provider and can deploy to most major cloud services. Then you can automate the build, testing, and deployment of your code to Microsoft Azure, Google Cloud Platform, or Amazon Web Services. |
| 24 | + |
| 25 | +This tutorial is designed for Azure Resource Manager template developers who are new Azure DevOps Services and Azure Pipelines. If you are already familiar with GitHub and DevOps, you can skip to [Create a pipeline](#create-a-pipeline). |
| 26 | + |
| 27 | +> [!NOTE] |
| 28 | +> Pick a project name. When you go through the tutorial, replace any of the **AzureRmPipeline** with your project name. |
| 29 | +
|
| 30 | +This tutorial covers the following tasks: |
| 31 | + |
| 32 | +> [!div class="checklist"] |
| 33 | +> * Prepare a GitHub repository |
| 34 | +> * Create an Azure DevOps project |
| 35 | +> * Create an Azure pipeline |
| 36 | +> * Verify the pipeline deployment |
| 37 | +> * Update the template and redeploy |
| 38 | +> * Clean up resources |
| 39 | +
|
| 40 | +If you don't have an Azure subscription, [create a free account](https://azure.microsoft.com/free/) before you begin. |
| 41 | + |
| 42 | +## Prerequisites |
| 43 | + |
| 44 | +To complete this article, you need: |
| 45 | + |
| 46 | +* **A GitHub account**, where you use it to create a repository for your templates. If you don’t have one, you can [create one for free](https://github.com). For more information about using GitHub repositories, see [Build GitHub repositories](/azure/devops/pipelines/repos/github). |
| 47 | +* **Install Git**. This tutorial instruction uses *Git Bash* or *Git Shell*. For instructions, see [Install Git]( https://www.atlassian.com/git/tutorials/install-git). |
| 48 | +* **An Azure DevOps organization**. If you don't have one, you can create one for free. See [Create an organization or project collection]( https://docs.microsoft.com/azure/devops/organizations/accounts/create-organization?view=azure-devops). |
| 49 | +* **[Visual Studio Code](https://code.visualstudio.com/) with the Resource Manager Tools extension**. See [Install the extension |
| 50 | +](./resource-manager-quickstart-create-templates-use-visual-studio-code.md#prerequisites). |
| 51 | + |
| 52 | +## Prepare a GitHub repository |
| 53 | + |
| 54 | +GitHub is used to store your project source code including Resource Manager templates. For other supported repositories, see [repositories supported by Azure DevOps](/azure/devops/pipelines/repos/?view=azure-devops#supported-repository-types). |
| 55 | + |
| 56 | +### Create a GitHub repository |
| 57 | + |
| 58 | +If you don’t have a GitHub account, see [Prerequisites](#prerequisites). |
| 59 | + |
| 60 | +1. Sign in to [GitHub](https://github.com). |
| 61 | +2. Select your account image on the upper right corner, and then select **Your repositories**. |
| 62 | + |
| 63 | +  |
| 64 | + |
| 65 | +1. Select **New**, a green button. |
| 66 | +1. In **Repository name**, enter a repository name. For example, **AzureRmPipeline-repo**. Remember to replace any of **AzureRmPipeline** with your project name. You can select either **Public** or **private** for going through this tutorial. And then select **Create repository**. |
| 67 | +1. Write down the URL. The repository URL is the following format: |
| 68 | + |
| 69 | + ```url |
| 70 | + https://github.com/[YourAccountName]/[YourRepositoryName] |
| 71 | + ``` |
| 72 | + |
| 73 | +This repository is referred to as a *remote repository*. Each of the developers of the same project can clone his/her own *local repository*, and merge the changes to the remote repository. |
| 74 | + |
| 75 | +### Clone the remote repository |
| 76 | + |
| 77 | +1. Open Git Shell or Git Bash. See [Prerequisites](#prerequisites). |
| 78 | +1. Verify your current folder is **github**. |
| 79 | +1. Run the following command: |
| 80 | + |
| 81 | + ```bash |
| 82 | + git clone https://github.com/[YourAccountName]/[YourGitHubRepositoryName] |
| 83 | + cd [YourGitHubRepositoryName] |
| 84 | + mkdir CreateAzureStorage |
| 85 | + cd CreateAzureStorage |
| 86 | + pwd |
| 87 | + ``` |
| 88 | + |
| 89 | + Replace **[YourAccountName]** with your GitHub account name, and replace **[YourGitHubRepositoryName]** with your repository name you created in the previous procedure. |
| 90 | + |
| 91 | + The following screenshots shows an example. |
| 92 | + |
| 93 | +  |
| 94 | + |
| 95 | +The **CreateAzureStorage** folder is the folder where the template is stored. The **pwd** command shows the folder path. The path is where you save the template to in the following procedure. |
| 96 | + |
| 97 | +### Download a Quickstart template |
| 98 | + |
| 99 | +Instead of creating a template, you can download a [Quickstart template]( https://raw.githubusercontent.com/Azure/azure-quickstart-templates/master/101-storage-account-create/azuredeploy.json). This template creates an Azure Storage account. |
| 100 | + |
| 101 | +1. Open Visual Studio code. See [Prerequisites](#prerequisites). |
| 102 | +2. Open the template with the following URL: |
| 103 | + |
| 104 | + ```URL |
| 105 | + https://raw.githubusercontent.com/Azure/azure-quickstart-templates/master/101-storage-account-create/azuredeploy.json |
| 106 | + ``` |
| 107 | + |
| 108 | +3. Save the file as **azuredeploy.json** to the **CreateAzureStorage** folder. Both the folder name and the file name are used as they are in the pipeline. If you change these names, you must update the names used in the pipeline. |
| 109 | + |
| 110 | +### Push the template to the remote repository |
| 111 | + |
| 112 | +The azuredeploy.json has been added to the local repository. Next, you upload the template to the remote repository. |
| 113 | + |
| 114 | +1. Open *Git Shell* or *Git Bash*, if it is not opened. |
| 115 | +1. Change directory to the CreateAzureStorage folder in your local repository. |
| 116 | +1. Verify the **azuredeploy.json** file is in the folder. |
| 117 | +1. Run the following command: |
| 118 | + |
| 119 | + ```bash |
| 120 | + git add . |
| 121 | + git commit -m “Add a new create storage account template.” |
| 122 | + git push origin master |
| 123 | + ``` |
| 124 | + |
| 125 | + You might get a warning about LF. You can ignore the warning. **master** is the master branch. You typically create a branch for each update. To simplify the tutorial, you use the master branch directly. |
| 126 | +1. Browse to your GitHub repository from a browser. The URL is **https://github.com/[YourAccountName]/[YourGitHubRepository]**. You shall see the **CreateAzureStorage** folder and **Azuredeploy.json** inside the folder. |
| 127 | + |
| 128 | +So far, you have created a GitHub repository, and uploaded a template to the repository. |
| 129 | + |
| 130 | +## Create a DevOps project |
| 131 | + |
| 132 | +A DevOps organization is needed before you can proceed to the next procedure. If you don’t have one, see [Prerequisites](#prerequisites). |
| 133 | + |
| 134 | +1. Sign in to [Azure DevOps](https://dev.azure.com). |
| 135 | +1. Select a DevOps organization from the left. |
| 136 | + |
| 137 | +  |
| 138 | + |
| 139 | +1. Select **Create project**. If you don't have any projects, the create project page is opened automatically. |
| 140 | +1. Enter the following values: |
| 141 | +
|
| 142 | + * **Project name**: enter a project name. You can use the project name you picked at the very beginning of the tutorial. |
| 143 | + * **Version control**: Select **Git**. You might need to expand **Advanced** to see **Version control**. |
| 144 | +
|
| 145 | + Use the default value for the other properties. |
| 146 | +1. Select **Create project**. |
| 147 | +
|
| 148 | +Create a service connection that is used to deploy projects to Azure. |
| 149 | +
|
| 150 | +1. Select **Project settings** from the bottom of the left menu. |
| 151 | +1. Select **Service connections** under **Pipelines**. |
| 152 | +1. Select **New Service connection**, and then select **AzureResourceManager**. |
| 153 | +1. Enter the following values: |
| 154 | +
|
| 155 | + * **Connection name**: enter a connection name. For example, **AzureRmPipeline-conn**. Write down this name, you need the name when you create your pipeline. |
| 156 | + * **Scope level**: select **Subscription**. |
| 157 | + * **Subscription**: select your subscription. |
| 158 | + * **Resource Group**: Leave it blank. |
| 159 | + * **Allow all pipelines to use this connection**. (selected) |
| 160 | +1. Select **OK**. |
| 161 | +
|
| 162 | +## Create a pipeline |
| 163 | +
|
| 164 | +Until now, you have completed the following tasks. If you skip the previous sections because you are familiar with GitHub and DevOps, you must complete the tasks before you continue. |
| 165 | +
|
| 166 | +- Create a GitHub repository, and save [this template](https://raw.githubusercontent.com/Azure/azure-quickstart-templates/master/101-storage-account-create/azuredeploy.json) to the **CreateAzureStorage** folder in the repository. |
| 167 | +- Create a DevOps project, and create an Azure Resource Manager service connection. |
| 168 | +
|
| 169 | +To create a pipeline with a step to deploy a template: |
| 170 | +
|
| 171 | +1. Select **Pipelines** from the left menu. |
| 172 | +1. Select **New pipeline**. |
| 173 | +1. From the **Connect** tab, select **GitHub**. If asked, enter your GitHub credentials, and then follow the instructions. If you see the following screen, select **Only select repositories**, and verify your repository is in the list before you select **Approve & Install**. |
| 174 | +
|
| 175 | +  |
| 176 | +
|
| 177 | +1. From the **Select** tab, select your repository. The default name is **[YourAccountName]/[YourGitHubRepositoryName]**. |
| 178 | +1. From the **Configure** tab, select **Starter pipeline**. It shows the **azure-pipelines.yml** pipeline file with two script steps. |
| 179 | +1. Replace the **steps** section with the following YAML: |
| 180 | +
|
| 181 | + ```yaml |
| 182 | + steps: |
| 183 | + - task: AzureResourceGroupDeployment@2 |
| 184 | + inputs: |
| 185 | + azureSubscription: '[YourServiceConnectionName]' |
| 186 | + action: 'Create Or Update Resource Group' |
| 187 | + resourceGroupName: '[EnterANewResourceGroupName]' |
| 188 | + location: 'Central US' |
| 189 | + templateLocation: 'Linked artifact' |
| 190 | + csmFile: 'CreateAzureStorage/azuredeploy.json' |
| 191 | + deploymentMode: 'Incremental' |
| 192 | + ``` |
| 193 | +
|
| 194 | + It shall look like: |
| 195 | +
|
| 196 | +  |
| 197 | +
|
| 198 | + Make the following changes: |
| 199 | +
|
| 200 | + * **azureSubscription**: update the value with the service connection created in the previous procedure. |
| 201 | + * **action**: the **Create Or Update Resource Group** action does 2 actions - 1. create a resource group if a new resource group name is provided; 2. deploy the template specified. |
| 202 | + * **resourceGroupName**: specify a new resource group name. For example, **AzureRmPipeline-rg**. |
| 203 | + * **location**: specify the location for the resource group. |
| 204 | + * **templateLocation**: when **Linked artifact** is specified, the task looks for the template file directly from the connected repository. |
| 205 | + * **csmFile** is the path to the template file. You don't need to specify a template parameters file because all of the parameters defined in the template have default values. |
| 206 | + |
| 207 | + For more information about the task, see [Azure Resource Group Deployment task](/azure/devops/pipelines/tasks/deploy/azure-resource-group-deployment) |
| 208 | +1. Select **Save and run**. |
| 209 | +1. Select **Save and run** again. A copy of the YAML file is saved into the connected repository. You can see the YAML file by browse to your repository. |
| 210 | +1. Verify that the pipeline is executed successfully. |
| 211 | + |
| 212 | +  |
| 213 | + |
| 214 | +## Verify the deployment |
| 215 | + |
| 216 | +1. Sign in to the [Azure portal](https://portal.azure.com). |
| 217 | +1. Open the resource group. The name is what you specified in the pipeline YAML file. You shall see one storage account created. The storage account name starts with **store**. |
| 218 | +1. Select the storage account name to open it. |
| 219 | +1. Select **Properties**. Notice the **SKU** is **Standard_LRS**. |
| 220 | + |
| 221 | +  |
| 222 | + |
| 223 | +## Update and redeploy |
| 224 | + |
| 225 | +When you update the template and push the changes to the remote repository, the pipeline automatically updates the resources, the storage account in this case. |
| 226 | + |
| 227 | +1. Open **azuredeploy.json** from your local repository in Visual Studio Code. |
| 228 | +1. Update the **defaultValue** of **storageAccountType** to **Standard_GRS**. See the following screenshot: |
| 229 | + |
| 230 | +  |
| 231 | + |
| 232 | +1. Save the changes. |
| 233 | +1. Push the changes to the remote repository by running the following commands from Git Bash/Shell. |
| 234 | + |
| 235 | + ```bash |
| 236 | + git pull origin master |
| 237 | + git add . |
| 238 | + git commit -m “Add a new create storage account template.” |
| 239 | + git push origin master |
| 240 | + ``` |
| 241 | + |
| 242 | + The first command syncs the local repository with the remote repository. Remember the pipeline YAML file was added to the remote repository. |
| 243 | + |
| 244 | + With the master branch of the remote repository updated, the pipeline is fired again. |
| 245 | + |
| 246 | +To verify the changes, you can check the SKU of the storage account. See [Verify the deployment](#verify-the-deployment). |
| 247 | + |
| 248 | +## Clean up resources |
| 249 | + |
| 250 | +When the Azure resources are no longer needed, clean up the resources you deployed by deleting the resource group. |
| 251 | + |
| 252 | +1. From the Azure portal, select **Resource group** from the left menu. |
| 253 | +2. Enter the resource group name in the **Filter by name** field. |
| 254 | +3. Select the resource group name. |
| 255 | +4. Select **Delete resource group** from the top menu. |
| 256 | + |
| 257 | +You might also want to delete the GitHub repository and the Azure DevOps project. |
| 258 | + |
| 259 | +## Next steps |
| 260 | + |
| 261 | +In this tutorial, you create an Azure DevOps pipeline to deploy an Azure Resource Manager template. To learn how to deploy Azure resources across multiple regions, and how to use safe deployment practices, see |
| 262 | + |
| 263 | +> [!div class="nextstepaction"] |
| 264 | +> [Use Azure Deployment Manager](./resource-manager-tutorial-deploy-vm-extensions.md) |
0 commit comments