|
| 1 | +--- |
| 2 | +title: Publish revisions with Azure Pipelines in Azure Container Apps |
| 3 | +description: Learn to automatically create new revisions in Azure Container Apps using an Azure DevOps pipeline |
| 4 | +services: container-apps |
| 5 | +author: craigshoemaker |
| 6 | +ms.service: container-apps |
| 7 | +ms.topic: how-to |
| 8 | +ms.date: 11/09/2022 |
| 9 | +ms.author: cshoe |
| 10 | +--- |
| 11 | + |
| 12 | +# Deploy to Azure Container Apps from Azure Pipelines (preview) |
| 13 | + |
| 14 | +Azure Container Apps allows you to use Azure Pipelines to publish [revisions](revisions.md) to your container app. As commits are pushed to your [Azure DevOps repository](/azure/devops/repos/), a pipeline is triggered which updates the container image in the container registry. Azure Container Apps creates a new revision based on the updated container image. |
| 15 | + |
| 16 | +The pipeline is triggered by commits to a specific branch in your repository. When creating the pipeline, you decide which branch is the trigger. |
| 17 | + |
| 18 | +## Container Apps Azure Pipelines task |
| 19 | + |
| 20 | +To build and deploy your container app, add the [`AzureContainerAppsRC`](https://marketplace.visualstudio.com/items?itemName=microsoft-oryx.AzureContainerAppsRC) (preview) Azure Pipelines task to your pipeline. |
| 21 | + |
| 22 | +The task supports the following scenarios: |
| 23 | + |
| 24 | +* Build from a Dockerfile and deploy to Container Apps |
| 25 | +* Build from source code without a Dockerfile and deploy to Container Apps. Supported languages include .NET, Node.js, PHP, Python, and Ruby |
| 26 | +* Deploy an existing container image to Container Apps |
| 27 | + |
| 28 | +### Usage examples |
| 29 | + |
| 30 | +Here are some common scenarios for using the task. For more information, see the [task's documentation](https://github.com/Azure/container-apps-deploy-pipelines-task/blob/main/README.md). |
| 31 | + |
| 32 | +#### Build and deploy to Container Apps |
| 33 | + |
| 34 | +The following snippet shows how to build a container image from source code and deploy it to Container Apps. |
| 35 | + |
| 36 | +```yaml |
| 37 | +steps: |
| 38 | +- task: AzureContainerAppsRC@0 |
| 39 | + inputs: |
| 40 | + appSourcePath: '$(Build.SourcesDirectory)/src' |
| 41 | + azureSubscription: 'my-subscription-service-connection' |
| 42 | + acrName: 'myregistry' |
| 43 | + containerAppName: 'my-container-app' |
| 44 | + resourceGroup: 'my-container-app-rg' |
| 45 | +``` |
| 46 | +
|
| 47 | +The task uses the Dockerfile in `appSourcePath` to build the container image. If no Dockerfile is found, the task attempts to build the container image from source code in `appSourcePath`. |
| 48 | + |
| 49 | +#### Deploy an existing container image to Container Apps |
| 50 | + |
| 51 | +The following snippet shows how to deploy an existing container image to Container Apps. |
| 52 | + |
| 53 | +```yaml |
| 54 | +steps: |
| 55 | + - task: AzureContainerAppsRC@0 |
| 56 | + inputs: |
| 57 | + azureSubscription: 'my-subscription-service-connection' |
| 58 | + acrName: 'myregistry' |
| 59 | + containerAppName: 'my-container-app' |
| 60 | + resourceGroup: 'my-container-app-rg' |
| 61 | + imageToDeploy: 'myregistry.azurecr.io/my-container-app:$(Build.BuildId)' |
| 62 | +``` |
| 63 | + |
| 64 | +> [!IMPORTANT] |
| 65 | +> If you're building a container image in a separate step, make sure you use a unique tag such as the build ID instead of a stable tag like `latest`. For more information, see [Image tag best practices](../container-registry/container-registry-image-tag-version.md). |
| 66 | + |
| 67 | +### Authenticate with Azure Container Registry |
| 68 | + |
| 69 | +The Azure Container Apps task needs to authenticate with your Azure Container Registry to push the container image. The container app also needs to authenticate with your Azure Container Registry to pull the container image. |
| 70 | + |
| 71 | +To push images, the task automatically authenticates with the container registry specified in `acrName` using the service connection provided in `azureSubscription`. |
| 72 | + |
| 73 | +To pull images, Azure Container Apps uses either managed identity (recommended) or admin credentials to authenticate with the Azure Container Registry. To use managed identity, the target container app for the task must be [configured to use managed identity](managed-identity-image-pull.md). To authenticate with the registry's admin credentials, set the task's `acrUsername` and `acrPassword` inputs. |
| 74 | + |
| 75 | +## Configuration |
| 76 | + |
| 77 | +Take the following steps to configure an Azure DevOps pipeline to deploy to Azure Container Apps. |
| 78 | + |
| 79 | +> [!div class="checklist"] |
| 80 | +> * Create an Azure DevOps repository for your app |
| 81 | +> * Create a container app with managed identity enabled |
| 82 | +> * Assign the `AcrPull` role for the Azure Container Registry to the container app's managed identity |
| 83 | +> * Install the Azure Container Apps task from the Azure DevOps Marketplace |
| 84 | +> * Configure an Azure DevOps service connection for your Azure subscription |
| 85 | +> * Create an Azure DevOps pipeline |
| 86 | + |
| 87 | +### Prerequisites |
| 88 | + |
| 89 | +| Requirement | Instructions | |
| 90 | +|--|--| |
| 91 | +| Azure account | If you don't have one, [create an account for free](https://azure.microsoft.com/free/?WT.mc_id=A261C142F). You need the *Contributor* or *Owner* permission on the Azure subscription to proceed. Refer to [Assign Azure roles using the Azure portal](../role-based-access-control/role-assignments-portal.md?tabs=current) for details. | |
| 92 | +| Azure Devops project | Go to [Azure DevOps](https://azure.microsoft.com/services/devops/) and select *Start free*. Then create a new project. | |
| 93 | +| Azure CLI | Install the [Azure CLI](/cli/azure/install-azure-cli).| |
| 94 | + |
| 95 | +### Create an Azure DevOps repository and clone the source code |
| 96 | + |
| 97 | +Before creating a pipeline, the source code for your app must be in a repository. |
| 98 | + |
| 99 | +1. Log in to [Azure DevOps](https://dev.azure.com/) and navigate to your project. |
| 100 | + |
| 101 | +1. Open the **Repos** page. |
| 102 | + |
| 103 | +1. In the top navigation bar, select the repositories dropdown and select **Import repository**. |
| 104 | + |
| 105 | +1. Enter the following information and select **Import**: |
| 106 | + |
| 107 | + | Field | Value | |
| 108 | + |--|--| |
| 109 | + | **Repository type** | Git | |
| 110 | + | **Clone URL** | `https://github.com/Azure-Samples/containerapps-albumapi-csharp.git` | |
| 111 | + | **Name** | `my-container-app` | |
| 112 | + |
| 113 | +1. Select **Clone** to view the repository URL and copy it. |
| 114 | + |
| 115 | +1. Open a terminal and run the following command to clone the repository: |
| 116 | + |
| 117 | + ```bash |
| 118 | + git clone <REPOSITORY_URL> my-container-app |
| 119 | + ``` |
| 120 | + |
| 121 | + Replace `<REPOSITORY_URL>` with the URL you copied. |
| 122 | + |
| 123 | +### Create a container app and configure managed identity |
| 124 | + |
| 125 | +Create your container app using the `az containerapp up` command with the following steps. This command creates Azure resources, builds the container image, stores the image in a registry, and deploys to a container app. |
| 126 | + |
| 127 | +After your app is created, you can add a managed identity to your app and assign the identity the `AcrPull` role to allow the identity to pull images from the registry. |
| 128 | + |
| 129 | +[!INCLUDE [container-apps-github-devops-setup.md](../../includes/container-apps-github-devops-setup.md)] |
| 130 | + |
| 131 | +### Install the Azure Container Apps task |
| 132 | + |
| 133 | +The Azure Container Apps Azure Pipelines task is currently in preview. Before you use the task, you must install it from the Azure DevOps Marketplace. |
| 134 | + |
| 135 | +1. Open the [Azure Container Apps task](https://marketplace.visualstudio.com/items?itemName=microsoft-oryx.AzureContainerAppsRC) in the Azure DevOps Marketplace. |
| 136 | + |
| 137 | +1. Select **Get it free**. |
| 138 | + |
| 139 | +1. Select your Azure DevOps organization and select **Install**. |
| 140 | + |
| 141 | +### Create an Azure DevOps service connection |
| 142 | + |
| 143 | +To deploy to Azure Container Apps, you need to create an Azure DevOps service connection for your Azure subscription. |
| 144 | + |
| 145 | +1. In Azure DevOps, select **Project settings**. |
| 146 | + |
| 147 | +1. Select **Service connections**. |
| 148 | + |
| 149 | +1. Select **New service connection**. |
| 150 | + |
| 151 | +1. Select **Azure Resource Manager**. |
| 152 | + |
| 153 | +1. Select **Service principal (automatic)** and select **Next**. |
| 154 | + |
| 155 | +1. Enter the following information and select **Save**: |
| 156 | + |
| 157 | + | Field | Value | |
| 158 | + |--|--| |
| 159 | + | **Subscription** | Select your Azure subscription. | |
| 160 | + | **Resource group** | Select the resource group (`my-container-app-rg`) that contains your container app and container registry. | |
| 161 | + | **Service connection name** | `my-subscription-service-connection` | |
| 162 | + |
| 163 | +To learn more about service connections, see [Connect to Microsoft Azure](/azure/devops/pipelines/library/connect-to-azure). |
| 164 | + |
| 165 | +### Create an Azure DevOps YAML pipeline |
| 166 | + |
| 167 | +1. In your Azure DevOps project, select **Pipelines**. |
| 168 | + |
| 169 | +1. Select **New pipeline**. |
| 170 | + |
| 171 | +1. Select **Azure Repos Git**. |
| 172 | + |
| 173 | +1. Select the repo that contains your source code (`my-container-app`). |
| 174 | + |
| 175 | +1. Select **Starter pipeline**. |
| 176 | + |
| 177 | +1. In the editor, replace the contents of the file with the following YAML: |
| 178 | + |
| 179 | + ```yaml |
| 180 | + trigger: |
| 181 | + branches: |
| 182 | + include: |
| 183 | + - main |
| 184 | +
|
| 185 | + pool: |
| 186 | + vmImage: ubuntu-latest |
| 187 | +
|
| 188 | + steps: |
| 189 | + - task: AzureContainerAppsRC@0 |
| 190 | + inputs: |
| 191 | + appSourcePath: '$(Build.SourcesDirectory)/src' |
| 192 | + azureSubscription: '<AZURE_SUBSCRIPTION_SERVICE_CONNECTION>' |
| 193 | + acrName: '<ACR_NAME>' |
| 194 | + containerAppName: 'my-container-app' |
| 195 | + resourceGroup: 'my-container-app-rg' |
| 196 | + ``` |
| 197 | + |
| 198 | + Replace `<AZURE_SUBSCRIPTION_SERVICE_CONNECTION>` with the name of the Azure DevOps service connection (`my-subscription-service-connection`) you created in the previous step and `<ACR_NAME>` with the name of your Azure Container Registry. |
| 199 | + |
| 200 | +1. Select **Save and run**. |
| 201 | + |
| 202 | +An Azure Pipelines run starts to build and deploy your container app. To check its progress, navigate to *Pipelines* and select the run. During the first pipeline run, you may be prompted to authorize the pipeline to use your service connection. |
| 203 | + |
| 204 | +To deploy a new revision of your app, push a new commit to the *main* branch. |
0 commit comments