|
| 1 | +--- |
| 2 | +title: Deploy with Azure Pipelines |
| 3 | +description: Learn how to use Azure Pipelines to deploy your custom Windows container to App Service from a CI/CD pipeline. |
| 4 | +ms.topic: article |
| 5 | +ms.date: 6/10/2024 |
| 6 | +author: jefmarti |
| 7 | +ms.author: jefmarti |
| 8 | +--- |
| 9 | + |
| 10 | +# Deploy a custom container to App Service using Azure Pipelines |
| 11 | + |
| 12 | +Azure DevOps enables you to host, build, plan and test your code with complimentary workflows. Using Azure Pipelines as one of these workflows allows you to deploy your application with CI/CD that works with any platform and cloud. A pipeline is defined as a YAML file in the root directory of your repository. |
| 13 | + |
| 14 | +In this article, we will use Azure Pipelines to deploy a Windows container application to App Service from a Git repository in Azure DevOps. It assumes you already have a .NET application with a supporting dockerfile in Azure DevOps. |
| 15 | + |
| 16 | +## Prerequisites |
| 17 | + |
| 18 | +- An Azure account with an active subscription. [Create an account for free](https://azure.microsoft.com/free/?WT.mc_id=A261C142F). |
| 19 | +- An Azure DevOps organization. [Create one for free](/azure/devops/pipelines/get-started/pipelines-sign-up). |
| 20 | +- A working Windows app with Dockerfile hosted on [Azure Repos](https://docs.github.com/get-started/quickstart/create-a-repo). |
| 21 | + |
| 22 | +## 1. Add a Service Connection |
| 23 | +Before you create your pipeline, you should first create your Service Connection since you will be asked to choose and verify your connection when creating your template. A Service Connection will allow you to connect to your registry of choice (ACR or Docker Hub) when using the task templates. When adding a new service connection, choose the Docker Registry option. The following form will ask you to choose Docker Hub or Azure Container Registry along with pertaining information. To follow along with this tutorial, use Azure Container Registry. You can create a new Service Connection following the directions [here](https://learn.microsoft.com/azure/devops/pipelines/library/service-endpoints?view=azure-devops&tabs=yaml#create-new). |
| 24 | + |
| 25 | +## 2. Secure your secrets |
| 26 | +Since we are using sensitive information that you don’t want others to access, we will use variables to protect our information. Create a variable by following the directions [here](https://learn.microsoft.com/azure/devops/pipelines/process/variables?view=azure-devops&tabs=yaml%2Cbatch#secret-variables). |
| 27 | + |
| 28 | +To add a Variable, you click the **Variables** button next to the Save button in the top-right of the editing view for your pipeline. Select the **New Variable** button and enter your information. Add the variables below with your own secrets appropriate from each resource. |
| 29 | + |
| 30 | +- vmImageName: ‘windows-latest’ |
| 31 | +- imageRepository: ‘your-image-repo-name’ |
| 32 | +- dockerfilePath: ‘$(Build.SourcesDirectory)/path/to/Dockerfile’ |
| 33 | +- dockerRegistryServiceConnection: ‘your-service-connection-number’ |
| 34 | + |
| 35 | +## 3. Create a new pipeline |
| 36 | +Once your repository is created with your .NET application and supporting dockerfile, you can create your pipeline following these steps. |
| 37 | + |
| 38 | +1. Navigate to **Pipelines** on the left menu bar and click on the **Create pipeline** button |
| 39 | +1. On the next screen, select **Azure Repos Git** as your repository option and select the repository where you code is |
| 40 | +1. Under the Configure tab choose the **Starter Pipeline** option |
| 41 | +1. Under the next Review tab, click the **Save** button |
| 42 | + |
| 43 | +## 4. Build and push image to Azure container registry |
| 44 | +After your pipeline is created and saved, you will need to edit the pipeline to run the steps for building the container, pushing to a registry, and deploying the image to App Service. To start, navigate to the **Pipelines** menu, choose your pipeline that you just created and click the **Edit** button. |
| 45 | + |
| 46 | +First, you need to add the docker task so you can build the image. Add the following code and replace the Dockerfile: app/Dockerfile with the path to your Dockerfile. |
| 47 | + |
| 48 | +```yaml |
| 49 | +trigger: |
| 50 | + - main |
| 51 | + |
| 52 | + pool: |
| 53 | + vmImage: 'windows-latest' |
| 54 | + |
| 55 | + variables: |
| 56 | + vmImageName: 'windows-latest' |
| 57 | + imageRepository: 'your-image-repo-name' |
| 58 | + dockerfilePath: '$(Build.SourcesDirectory)/path/to/Dockerfile' |
| 59 | + dockerRegistryServiceConnection: 'your-service-connection-number' |
| 60 | + |
| 61 | +- stage: Build |
| 62 | + displayName: Build and push stage |
| 63 | + jobs: |
| 64 | + - job: Build |
| 65 | + displayName: Build job |
| 66 | + pool: |
| 67 | + vmImage: $(vmImageName) |
| 68 | + steps: |
| 69 | + - task: Docker@2 |
| 70 | + displayName: Build and push an image to container registry |
| 71 | + inputs: |
| 72 | + command: buildAndPush |
| 73 | + repository: $(imageRepository) |
| 74 | + dockerfile: $(dockerfilePath) |
| 75 | + containerRegistry: $(dockerRegistryServiceConnection) |
| 76 | + tags: | |
| 77 | + $(tag) |
| 78 | +``` |
| 79 | +
|
| 80 | +## 5. Add the App Service deploy task |
| 81 | +Next, you’ll need to setup the deploy task. This will require your subscription name, application name, and container registry. Add a new stage to the yaml file by pasting the code below. |
| 82 | +
|
| 83 | +```yaml |
| 84 | +- stage: Deploy |
| 85 | + displayName: Deploy to App Service |
| 86 | + jobs: |
| 87 | + - job: Deploy |
| 88 | + displayName: Deploy |
| 89 | + pool: |
| 90 | + vmImage: $(vmImageName) |
| 91 | + steps: |
| 92 | +``` |
| 93 | +
|
| 94 | +Next, navigate to the **Show assistant** tab in the upper right hand corner and find the **Azure App Service deploy** task and fill out the following form: |
| 95 | +
|
| 96 | +- Connection type: Azure Resource Manager |
| 97 | +- Azure subscription: your-subscription-name |
| 98 | +- App Service type: Web App for Containers (Windows) |
| 99 | +- App Service name: your-app-name |
| 100 | +- Registry or Namespace: your-azure-container-registry-namespace |
| 101 | +- Image: your-azure-container-registry-image-name |
| 102 | +
|
| 103 | +Once you have those filled out, click the **Add** button to add the task below: |
| 104 | +
|
| 105 | +```yaml |
| 106 | +- task: AzureRmWebAppDeployment@4 |
| 107 | + inputs: |
| 108 | + ConnectionType: 'AzureRM' |
| 109 | + azureSubscription: 'my-subscription-name' |
| 110 | + appType: 'webAppHyperVContainer' |
| 111 | + WebAppName: 'my-app-name' |
| 112 | + DockerNamespace: 'myregsitry.azurecr.io' |
| 113 | + DockerRepository: 'dotnetframework:12' |
| 114 | +``` |
| 115 | +
|
| 116 | +After you’ve added the task the pipeline is ready to run. Click the **Validate and save** button and run the pipeline. The pipeline will go through the steps to build and push the Windows container image to Azure Container Registry and deploy the image to App Service. |
| 117 | +
|
| 118 | +Below is the example of the full yaml file: |
| 119 | +
|
| 120 | +```yaml |
| 121 | +trigger: |
| 122 | + - main |
| 123 | + |
| 124 | + pool: |
| 125 | + vmImage: 'windows-latest' |
| 126 | + |
| 127 | + variables: |
| 128 | + vmImageName: 'windows-latest' |
| 129 | + imageRepository: 'your-image-repo-name' |
| 130 | + dockerfilePath: '$(Build.SourcesDirectory)/path/to/Dockerfile' |
| 131 | + dockerRegistryServiceConnection: 'your-service-connection-number' |
| 132 | + |
| 133 | +- stage: Build |
| 134 | + displayName: Build and push stage |
| 135 | + jobs: |
| 136 | + - job: Build |
| 137 | + displayName: Build job |
| 138 | + pool: |
| 139 | + vmImage: $(vmImageName) |
| 140 | + steps: |
| 141 | + - task: Docker@2 |
| 142 | + displayName: Build and push an image to container registry |
| 143 | + inputs: |
| 144 | + command: buildAndPush |
| 145 | + repository: $(imageRepository) |
| 146 | + dockerfile: $(dockerfilePath) |
| 147 | + containerRegistry: $(dockerRegistryServiceConnection) |
| 148 | + tags: | |
| 149 | + $(tag) |
| 150 | +
|
| 151 | +- stage: Deploy |
| 152 | + displayName: Deploy to App Service |
| 153 | + jobs: |
| 154 | + - job: Deploy |
| 155 | + displayName: Deploy |
| 156 | + pool: |
| 157 | + vmImage: $(vmImageName) |
| 158 | + steps: |
| 159 | + - task: AzureRmWebAppDeployment@4 |
| 160 | + inputs: |
| 161 | + ConnectionType: 'AzureRM' |
| 162 | + azureSubscription: 'my-subscription-name' |
| 163 | + appType: 'webAppHyperVContainer' |
| 164 | + WebAppName: 'my-app-name' |
| 165 | + DockerNamespace: 'myregsitry.azurecr.io' |
| 166 | + DockerRepository: 'dotnetframework:12' |
| 167 | +``` |
0 commit comments