|
2 | 2 | title: Custom container CI/CD from GitHub Actions
|
3 | 3 | description: Learn how to use GitHub Actions to deploy your custom Linux container to App Service from a CI/CD pipeline.
|
4 | 4 | ms.topic: article
|
5 |
| -ms.date: 12/04/2020 |
| 5 | +ms.date: 12/15/2021 |
6 | 6 | ms.author: jafreebe
|
7 | 7 | ms.reviewer: ushan
|
8 | 8 | ms.custom: github-actions-azure
|
@@ -32,7 +32,7 @@ For an Azure App Service container workflow, the file has three sections:
|
32 | 32 |
|
33 | 33 | ## Generate deployment credentials
|
34 | 34 |
|
35 |
| -The recommended way to authenticate with Azure App Services for GitHub Actions is with a publish profile. You can also authenticate with a service principal but the process requires more steps. |
| 35 | +The recommended way to authenticate with Azure App Services for GitHub Actions is with a publish profile. You can also authenticate with a service principal or Open ID Connect but the process requires more steps. |
36 | 36 |
|
37 | 37 | Save your publish profile credential or service principal as a [GitHub secret](https://docs.github.com/en/actions/reference/encrypted-secrets) to authenticate with Azure. You'll access the secret within your workflow.
|
38 | 38 |
|
@@ -74,6 +74,52 @@ In the example, replace the placeholders with your subscription ID, resource gro
|
74 | 74 | > [!IMPORTANT]
|
75 | 75 | > It is always a good practice to grant minimum access. The scope in the previous example is limited to the specific App Service app and not the entire resource group.
|
76 | 76 |
|
| 77 | +# [OpenID Connect](#tab/openid) |
| 78 | + |
| 79 | +OpenID Connect is an authentication method that uses short-lived tokens. Setting up [OpenID Connect with GitHub Actions](https://docs.github.com/en/actions/deployment/security-hardening-your-deployments/about-security-hardening-with-openid-connect) is more complex process that offers hardened security. |
| 80 | + |
| 81 | +1. If you do not have an existing application, register a [new Active Directory application and service principal that can access resources](/azure/active-directory/develop/howto-create-service-principal-portal). Create the Active Directory application. |
| 82 | + |
| 83 | + ```azurecli-interactive |
| 84 | + az ad app create --display-name myApp |
| 85 | + ``` |
| 86 | +
|
| 87 | + This command will output JSON with an `appId` that is your `client-id`. Save the value to use as the `AZURE_CLIENT_ID` GitHub secret later. |
| 88 | +
|
| 89 | + You'll use the `objectId` value when creating federated credentials with Graph API and reference it as the `APPLICATION-OBJECT-ID`. |
| 90 | +
|
| 91 | +1. Create a service principal. Replace the `$appID` with the appId from your JSON output. |
| 92 | +
|
| 93 | + This command generates JSON output with a different `objectId` and will be used in the next step. The new `objectId` is the `assignee-object-id`. |
| 94 | + |
| 95 | + Copy the `appOwnerTenantId` to use as a GitHub secret for `AZURE_TENANT_ID` later. |
| 96 | +
|
| 97 | + ```azurecli-interactive |
| 98 | + az ad sp create --id $appId |
| 99 | + ``` |
| 100 | +
|
| 101 | +1. Create a new role assignment by subscription and object. By default, the role assignment will be tied to your default subscription. Replace `$subscriptionId` with your subscription ID and `$assigneeObjectId` with the generated `assignee-object-id`. Learn [how to manage Azure subscriptions with the Azure CLI](/cli/azure/manage-azure-subscriptions-azure-cli). |
| 102 | +
|
| 103 | + ```azurecli-interactive |
| 104 | + az role assignment create --role contributor --subscription $subscriptionId --assignee-object-id $assigneeObjectId --assignee-principal-type ServicePrincipal |
| 105 | + ``` |
| 106 | +
|
| 107 | +1. Run the following command to [create a new federated identity credential](/graph/api/application-post-federatedidentitycredentials?view=graph-rest-beta&preserve-view=true) for your active directory application. |
| 108 | +
|
| 109 | +* Replace `APPLICATION-OBJECT-ID` with the **objectId (generated while creating app)** for your Active Directory application. |
| 110 | +* Set a value for `CREDENTIAL-NAME` to reference later. |
| 111 | +* Set the `subject`. The value of this is defined by GitHub depending on your workflow: |
| 112 | + * Jobs in your GitHub Actions environment: `repo:< Organization/Repository >:environment:< Name >` |
| 113 | + * For Jobs not tied to an environment, include the ref path for branch/tag based on the ref path used for triggering the workflow: `repo:< Organization/Repository >:ref:< ref path>`. For example, `repo:n-username/ node_express:ref:refs/heads/my-branch` or `repo:n-username/ node_express:ref:refs/tags/my-tag`. |
| 114 | + * For workflows triggered by a pull request event: `repo:< Organization/Repository >:pull_request`. |
| 115 | +
|
| 116 | +```azurecli |
| 117 | +az rest --method POST --uri 'https://graph.microsoft.com/beta/applications/<APPLICATION-OBJECT-ID>/federatedIdentityCredentials' --body '{"name":"<CREDENTIAL-NAME>","issuer":"https://token.actions.githubusercontent.com","subject":"repo:organization/repository:ref:refs/heads/main","description":"Testing","audiences":["api://AzureADTokenExchange"]}' |
| 118 | +``` |
| 119 | + |
| 120 | +To learn how to create a Create an active directory application, service principal, and federated credentials in Azure portal, see [Connect GitHub and Azure](/azure/developer/github/connect-from-azure#use-the-azure-login-action-with-openid-connect). |
| 121 | + |
| 122 | + |
77 | 123 | ---
|
78 | 124 | ## Configure the GitHub secret for authentication
|
79 | 125 |
|
@@ -104,6 +150,21 @@ When you configure the workflow file later, you use the secret for the input `cr
|
104 | 150 | with:
|
105 | 151 | creds: ${{ secrets.AZURE_CREDENTIALS }}
|
106 | 152 | ```
|
| 153 | +# [OpenID Connect](#tab/openid) |
| 154 | + |
| 155 | +You need to provide your application's **Client ID**, **Tenant ID** and **Subscription ID** to the login action. These values can either be provided directly in the workflow or can be stored in GitHub secrets and referenced in your workflow. Saving the values as GitHub secrets is the more secure option. |
| 156 | + |
| 157 | +1. Open your GitHub repository and go to **Settings**. |
| 158 | + |
| 159 | +1. Create secrets for `AZURE_CLIENT_ID`, `AZURE_TENANT_ID`, and `AZURE_SUBSCRIPTION_ID`. Use these values from your Active Directory application for your GitHub secrets. You can find these values in the Azure portal by searching for your active directory application. |
| 160 | + |
| 161 | + |GitHub Secret | Active Directory Application | |
| 162 | + |---------|---------| |
| 163 | + |AZURE_CLIENT_ID | Application (client) ID | |
| 164 | + |AZURE_TENANT_ID | Directory (tenant) ID | |
| 165 | + |AZURE_SUBSCRIPTION_ID | Subscription ID | |
| 166 | + |
| 167 | +1. Save each secret by selecting **Add secret**. |
107 | 168 |
|
108 | 169 | ---
|
109 | 170 |
|
@@ -254,6 +315,49 @@ jobs:
|
254 | 315 | az logout
|
255 | 316 | ```
|
256 | 317 |
|
| 318 | +# [OpenID Connect](#tab/openid) |
| 319 | + |
| 320 | +```yaml |
| 321 | +on: [push] |
| 322 | +name: Linux_Container_Node_Workflow |
| 323 | +
|
| 324 | +permissions: |
| 325 | + id-token: write |
| 326 | + contents: read |
| 327 | +
|
| 328 | +jobs: |
| 329 | + build-and-deploy: |
| 330 | + runs-on: ubuntu-latest |
| 331 | + steps: |
| 332 | + # checkout the repo |
| 333 | + - name: 'Checkout GitHub Action' |
| 334 | + uses: actions/checkout@main |
| 335 | + |
| 336 | + - name: 'Login via Azure CLI' |
| 337 | + uses: azure/login@v1 |
| 338 | + with: |
| 339 | + client-id: ${{ secrets.AZURE_CLIENT_ID }} |
| 340 | + tenant-id: ${{ secrets.AZURE_TENANT_ID }} |
| 341 | + subscription-id: ${{ secrets.AZURE_SUBSCRIPTION_ID }} |
| 342 | + |
| 343 | + - uses: azure/docker-login@v1 |
| 344 | + with: |
| 345 | + login-server: mycontainer.azurecr.io |
| 346 | + username: ${{ secrets.REGISTRY_USERNAME }} |
| 347 | + password: ${{ secrets.REGISTRY_PASSWORD }} |
| 348 | + - run: | |
| 349 | + docker build . -t mycontainer.azurecr.io/myapp:${{ github.sha }} |
| 350 | + docker push mycontainer.azurecr.io/myapp:${{ github.sha }} |
| 351 | + |
| 352 | + - uses: azure/webapps-deploy@v2 |
| 353 | + with: |
| 354 | + app-name: 'myapp' |
| 355 | + images: 'mycontainer.azurecr.io/myapp:${{ github.sha }}' |
| 356 | + |
| 357 | + - name: Azure logout |
| 358 | + run: | |
| 359 | + az logout |
| 360 | +``` |
257 | 361 | ---
|
258 | 362 |
|
259 | 363 | ## Next steps
|
|
0 commit comments