|
| 1 | +--- |
| 2 | +title: Deploying Docker to Azure App Service |
| 3 | +intro: You can deploy a Docker container to Azure App Service as part of your continuous deployment (CD) workflows. |
| 4 | +versions: |
| 5 | + fpt: '*' |
| 6 | + ghes: '*' |
| 7 | + ghae: '*' |
| 8 | + ghec: '*' |
| 9 | +type: tutorial |
| 10 | +topics: |
| 11 | + - CD |
| 12 | + - Containers |
| 13 | + - Docker |
| 14 | + - Azure App Service |
| 15 | +--- |
| 16 | + |
| 17 | +{% data reusables.actions.enterprise-beta %} |
| 18 | +{% data reusables.actions.enterprise-github-hosted-runners %} |
| 19 | + |
| 20 | +## Introduction |
| 21 | + |
| 22 | +This guide explains how to use {% data variables.product.prodname_actions %} to build and deploy a Docker container to [Azure App Service](https://azure.microsoft.com/services/app-service/). |
| 23 | + |
| 24 | +{% ifversion fpt or ghec or ghae-issue-4856 %} |
| 25 | + |
| 26 | +{% note %} |
| 27 | + |
| 28 | +**Note**: {% data reusables.actions.about-oidc-short-overview %} and "[Configuring OpenID Connect in Azure](/actions/deployment/security-hardening-your-deployments/configuring-openid-connect-in-azure)." |
| 29 | + |
| 30 | +{% endnote %} |
| 31 | + |
| 32 | +{% endif %} |
| 33 | + |
| 34 | +## Prerequisites |
| 35 | + |
| 36 | +Before creating your {% data variables.product.prodname_actions %} workflow, you will first need to complete the following setup steps: |
| 37 | + |
| 38 | +{% data reusables.actions.create-azure-app-plan %} |
| 39 | + |
| 40 | +1. Create a web app. |
| 41 | + |
| 42 | + For example, you can use the Azure CLI to create an Azure App Service web app: |
| 43 | + |
| 44 | + ```bash{:copy} |
| 45 | + az webapp create \ |
| 46 | + --name MY_WEBAPP_NAME \ |
| 47 | + --plan MY_APP_SERVICE_PLAN \ |
| 48 | + --resource-group MY_RESOURCE_GROUP \ |
| 49 | + --deployment-container-image-name nginx:latest |
| 50 | + ``` |
| 51 | + |
| 52 | + In the command above, replace the parameters with your own values, where `MY_WEBAPP_NAME` is a new name for the web app. |
| 53 | + |
| 54 | +{% data reusables.actions.create-azure-publish-profile %} |
| 55 | + |
| 56 | +1. Set registry credentials for your web app. |
| 57 | + |
| 58 | + Create a personal access token with the `repo` and `read:packages` scopes. For more information, see "[Creating a personal access token](/authentication/keeping-your-account-and-data-secure/creating-a-personal-access-token)." |
| 59 | + |
| 60 | + Set `DOCKER_REGISTRY_SERVER_URL` to `https://ghcr.io`, `DOCKER_REGISTRY_SERVER_USERNAME` to the GitHub username or organization that owns the repository, and `DOCKER_REGISTRY_SERVER_PASSWORD` to your personal access token from above. This will give your web app credentials so it can pull the container image after your workflow pushes a newly built image to the registry. You can do this with the following Azure CLI command: |
| 61 | + |
| 62 | + ```shell |
| 63 | + az webapp config appsettings set \ |
| 64 | + --name MY_WEBAPP_NAME \ |
| 65 | + --resource-group MY_RESOURCE_GROUP \ |
| 66 | + --settings DOCKER_REGISTRY_SERVER_URL=https://ghcr.io DOCKER_REGISTRY_SERVER_USERNAME=MY_REPOSITORY_OWNER DOCKER_REGISTRY_SERVER_PASSWORD=MY_PERSONAL_ACCESS_TOKEN |
| 67 | +``` |
| 68 | + |
| 69 | +{% ifversion fpt or ghes > 3.0 or ghae or ghec %} |
| 70 | +5. Optionally, configure a deployment environment. {% data reusables.actions.about-environments %} |
| 71 | +{% endif %} |
| 72 | + |
| 73 | +## Creating the workflow |
| 74 | + |
| 75 | +Once you've completed the prerequisites, you can proceed with creating the workflow. |
| 76 | + |
| 77 | +The following example workflow demonstrates how to build and deploy a Docker container to Azure App Service when there is a push to the `main` branch. |
| 78 | + |
| 79 | +Ensure that you set `AZURE_WEBAPP_NAME` in the workflow `env` key to the name of the web app you created. |
| 80 | + |
| 81 | +{% data reusables.actions.delete-env-key %} |
| 82 | + |
| 83 | +```yaml{:copy} |
| 84 | +{% data reusables.actions.actions-not-certified-by-github-comment %} |
| 85 | +
|
| 86 | +name: Build and deploy a container to an Azure Web App |
| 87 | +
|
| 88 | +env: |
| 89 | + AZURE_WEBAPP_NAME: MY_WEBAPP_NAME # set this to your application's name |
| 90 | +
|
| 91 | +on: |
| 92 | + push: |
| 93 | + branches: |
| 94 | + - main |
| 95 | +
|
| 96 | +permissions: |
| 97 | + contents: 'read' |
| 98 | + packages: 'write' |
| 99 | +
|
| 100 | +jobs: |
| 101 | + build: |
| 102 | + runs-on: ubuntu-latest |
| 103 | +
|
| 104 | + steps: |
| 105 | + - uses: actions/checkout@v2 |
| 106 | +
|
| 107 | + - name: Set up Docker Buildx |
| 108 | + uses: docker/setup-buildx-action@v1 |
| 109 | +
|
| 110 | + - name: Log in to GitHub container registry |
| 111 | + uses: docker/login-action@v1.10.0 |
| 112 | + with: |
| 113 | + registry: ghcr.io |
| 114 | + username: {% raw %}${{ github.actor }}{% endraw %} |
| 115 | + password: {% raw %}${{ secrets.GITHUB_TOKEN }}{% endraw %} |
| 116 | +
|
| 117 | + - name: Lowercase the repo name |
| 118 | + run: echo "REPO=${GITHUB_REPOSITORY,,}" >>${GITHUB_ENV} |
| 119 | +
|
| 120 | + - name: Build and push container image to registry |
| 121 | + uses: docker/build-push-action@v2 |
| 122 | + with: |
| 123 | + push: true |
| 124 | + tags: ghcr.io/{% raw %}${{ env.REPO }}{% endraw %}:{% raw %}${{ github.sha }}{% endraw %} |
| 125 | + file: ./Dockerfile |
| 126 | +
|
| 127 | + deploy: |
| 128 | + runs-on: ubuntu-latest |
| 129 | + needs: build |
| 130 | + environment: |
| 131 | + name: 'production' |
| 132 | + url: {% raw %}${{ steps.deploy-to-webapp.outputs.webapp-url }}{% endraw %} |
| 133 | +
|
| 134 | + steps: |
| 135 | + - name: Lowercase the repo name |
| 136 | + run: echo "REPO=${GITHUB_REPOSITORY,,}" >>${GITHUB_ENV} |
| 137 | +
|
| 138 | + - name: Deploy to Azure Web App |
| 139 | + id: deploy-to-webapp |
| 140 | + uses: azure/webapps-deploy@0b651ed7546ecfc75024011f76944cb9b381ef1e |
| 141 | + with: |
| 142 | + app-name: {% raw %}${{ env.AZURE_WEBAPP_NAME }}{% endraw %} |
| 143 | + publish-profile: {% raw %}${{ secrets.AZURE_WEBAPP_PUBLISH_PROFILE }}{% endraw %} |
| 144 | + images: 'ghcr.io/{% raw %}${{ env.REPO }}{% endraw %}:{% raw %}${{ github.sha }}{% endraw %}' |
| 145 | +``` |
| 146 | + |
| 147 | +## Additional resources |
| 148 | + |
| 149 | +The following resources may also be useful: |
| 150 | + |
| 151 | +* For the original starter workflow, see [`azure-container-webapp.yml`](https://github.com/actions/starter-workflows/blob/main/deployments/azure-container-webapp.yml) in the {% data variables.product.prodname_actions %} `starter-workflows` repository. |
| 152 | +* The action used to deploy the web app is the official Azure [`Azure/webapps-deploy`](https://github.com/Azure/webapps-deploy) action. |
| 153 | +* For more examples of GitHub Action workflows that deploy to Azure, see the [actions-workflow-samples](https://github.com/Azure/actions-workflow-samples) repository. |
0 commit comments