|
| 1 | + |
| 2 | +# Deploy infrastructure using GitHub Actions |
| 3 | + |
| 4 | +!!! info "Module Duration" |
| 5 | + 30 minutes |
| 6 | + |
| 7 | +GitHub Actions is a great way to automate your workflow. In this section, we will create a GitHub Action workflow to |
| 8 | +deploy the infrastructure components of our application. |
| 9 | + |
| 10 | +The workshop repository contains a GitHub Action workflow file that will be used to deploy the infrastructure |
| 11 | +components of our application. Follow the steps below to create a GitHub Action workflow to deploy the |
| 12 | +infrastructure components of our application. |
| 13 | + |
| 14 | +### Fork the GitHub repository |
| 15 | + |
| 16 | +Start by forking the workshop repository to your GitHub account. Follow the steps below to fork the workshop: |
| 17 | + |
| 18 | +1. Navigate to the workshop repository at [:material-github: Azure/aca-dotnet-workshop](https://github.com/Azure/aca-dotnet-workshop){target=_blank} |
| 19 | +2. Click the **Fork** button in the top-right corner of the page. |
| 20 | +3. Select your GitHub account to fork the repository to. |
| 21 | +4. Wait for the repository to be forked. |
| 22 | + |
| 23 | +### Configure Repository for OIDC Authentication with Azure AD |
| 24 | + |
| 25 | +In order to use the GitHub Actions workflow to deploy the infrastructure components of our application, we need to |
| 26 | +log in to Azure using the Azure CLI with [Azure login](https://github.com/marketplace/actions/azure-login) action. |
| 27 | + |
| 28 | +The Azure login action supports two different ways of authenticating with Azure: |
| 29 | + |
| 30 | +- [Service principal with secrets](https://learn.microsoft.com/en-us/azure/developer/github/connect-from-azure?tabs=azure-portal%2Clinux#use-the-azure-login-action-with-a-service-principal-secret) |
| 31 | +- [OpenID Connect (OIDC) with a Azure service principal using a Federated Identity Credential](https://learn.microsoft.com/en-us/azure/developer/github/connect-from-azure?tabs=azure-portal%2Clinux#use-the-azure-login-action-with-openid-connect) |
| 32 | + |
| 33 | +In this workshop, we will use the OIDC authentication method. Assuming you are already logged in using azure cli |
| 34 | +locally, follow the steps below to configure the repository for OIDC authentication with Azure AD either using powershell or bash/wsl: |
| 35 | + |
| 36 | +=== "PowerShell" |
| 37 | + |
| 38 | + - Execute the following commands in PowerShell to create an Azure AD application and service principal. |
| 39 | + |
| 40 | + ```powershell |
| 41 | + $AZURE_TENANT = az account show -o tsv --query tenantId |
| 42 | + $SUBSCRIPTION_ID = az account show -o tsv --query id |
| 43 | + |
| 44 | + $APP_ID = az ad app create --display-name aca-dotnet-workshop-oidc --query appId -otsv |
| 45 | + |
| 46 | + az ad sp create --id $APP_ID --query appId -otsv |
| 47 | + |
| 48 | + $OBJECT_ID = az ad app show --id $APP_ID --query id -otsv |
| 49 | + ``` |
| 50 | + |
| 51 | + - Execute below command to create a federated identity credential for the Azure AD application. |
| 52 | + |
| 53 | + !!! note |
| 54 | + Replace `<Repo owner>` in below json with your GitHub username where you forked the workshop repository. |
| 55 | + |
| 56 | + ```powershell |
| 57 | + az rest --method POST --uri "https://graph.microsoft.com/beta/applications/$OBJECT_ID/federatedIdentityCredentials" --body '{\"name\":\"aca-dotnet-workshop-federated-identity\",\"issuer\":\"https://token.actions.githubusercontent.com\",\"subject\":\"repo:<Repo owner>/aca-dotnet-workshop:ref:refs/heads/main\",\"description\":\"GitHub\",\"audiences\":[\"api://AzureADTokenExchange\"]}' --headers "Content-Type=application/json" |
| 58 | + ``` |
| 59 | + |
| 60 | + - Perform role assignment for the Azure AD application to access the subscription. |
| 61 | + |
| 62 | + ```powershell |
| 63 | + az role assignment create --assignee $APP_ID --role contributor --scope /subscriptions/$SUBSCRIPTION_ID |
| 64 | + az role assignment create --assignee $APP_ID --role 'User Access Administrator' --scope /subscriptions/$SUBSCRIPTION_ID |
| 65 | + ``` |
| 66 | + |
| 67 | +=== "Bash/WSL" |
| 68 | + |
| 69 | + - Execute the following commands in PowerShell to create an Azure AD application and service principal. |
| 70 | + |
| 71 | + ```bash |
| 72 | + AZURE_TENANT = $(az account show -o tsv --query tenantId) |
| 73 | + SUBSCRIPTION_ID = $(az account show -o tsv --query id) |
| 74 | + |
| 75 | + APP_ID = $(az ad app create --display-name serverless-webapp-kotlin-oidc --query appId -otsv) |
| 76 | + |
| 77 | + az ad sp create --id $APP_ID --query appId -otsv |
| 78 | + |
| 79 | + OBJECT_ID = $(az ad app show --id $APP_ID --query id -otsv) |
| 80 | + ``` |
| 81 | + |
| 82 | + - Execute below command to create a federated identity credential for the Azure AD application. |
| 83 | + |
| 84 | + !!! note |
| 85 | + Replace `<Repo owner>` in below json with your GitHub username where you forked the workshop repository. |
| 86 | + |
| 87 | + ```bash |
| 88 | + cat <<EOF > body.json |
| 89 | + { |
| 90 | + "name": "aca-dotnet-workshop-federated-identity", |
| 91 | + "issuer": "https://token.actions.githubusercontent.com", |
| 92 | + "subject": "repo:<Repo owner>/aca-dotnet-workshop:ref:refs/heads/main", |
| 93 | + "description": "GitHub", |
| 94 | + "audiences": [ |
| 95 | + "api://AzureADTokenExchange" |
| 96 | + ] |
| 97 | + } |
| 98 | + EOF |
| 99 | + |
| 100 | + az rest --method POST --uri "https://graph.microsoft.com/beta/applications/$OBJECT_ID/federatedIdentityCredentials" --body @body.json |
| 101 | + ``` |
| 102 | + |
| 103 | + - Perform role assignment for the Azure AD application to access the subscription. |
| 104 | + |
| 105 | + ```bash |
| 106 | + az role assignment create --assignee $APP_ID --role contributor --scope /subscriptions/$SUBSCRIPTION_ID |
| 107 | + az role assignment create --assignee $APP_ID --role 'User Access Administrator' --scope /subscriptions/$SUBSCRIPTION_ID |
| 108 | + ``` |
| 109 | + |
| 110 | +### Configure GitHub Repository Secrets |
| 111 | + |
| 112 | +Configure secrets details in GitHub repo as described here in [create GitHub secrets](https://learn.microsoft.com/en-us/azure/developer/github/connect-from-azure?tabs=azure-cli%2Clinux#create-github-secrets). |
| 113 | +Use below values mapped to relevant secrets in GitHub. |
| 114 | + |
| 115 | +```bash |
| 116 | +# AZURE_SUBSCRIPTION_ID |
| 117 | +echo $SUBSCRIPTION_ID |
| 118 | +# AZURE_TENANT_ID |
| 119 | +echo $AZURE_TENANT |
| 120 | +# AZURE_CLIENT_ID |
| 121 | +echo $APP_ID |
| 122 | +``` |
| 123 | + |
| 124 | +### Configure GitHub Repository Variables |
| 125 | + |
| 126 | +Configure repository variables as shown below: |
| 127 | + |
| 128 | +```bash |
| 129 | +# LOCATION: Azure region where resources will be deployed |
| 130 | +LOCATION=<location> |
| 131 | + |
| 132 | +# RESOURCE_GROUP: Name of the resource group which will be created and resources will be deployed |
| 133 | +RESOURCE_GROUP=<resource group name> |
| 134 | + |
| 135 | +# (OPTIONAL)CONTAINER_REGISTRY_NAME: Unique name of the container registry which will be created and where images will be imported |
| 136 | +CONTAINER_REGISTRY_NAME=<container registry name> |
| 137 | +``` |
| 138 | + |
| 139 | +!!! note |
| 140 | + |
| 141 | + Repository variables `CONTAINER_REGISTRY_NAME` is only needed by workflow, if you wish the images to be deployed from private ACR. |
| 142 | + |
| 143 | + You may chose to skip defining this variable and the workflow will use the [public github container registry images](https://github.com/orgs/Azure/packages?repo_name=aca-dotnet-workshop) to deploy the images. |
| 144 | + |
| 145 | +### Trigger GitHub Actions Workflow |
| 146 | + |
| 147 | +With these steps completed, you are now ready to trigger the GitHub Actions workflow name **Build and deploy |
| 148 | +infrastructure as code to Azure** using **workflow dispatch** to deploy the infrastructure components of our |
| 149 | +application. |
| 150 | + |
| 151 | +!!! success |
| 152 | + |
| 153 | + Your GitHub Actions workflow should be triggered and the infrastructure components of our application should be deployed successfully. |
| 154 | + |
| 155 | +  |
| 156 | + |
| 157 | + |
| 158 | +??? info "Want to delete the resources deployed by the workflow?" |
| 159 | + |
| 160 | + Trigger the workflow again using **workflow dispatch** and select **checkbox** option. |
| 161 | + |
| 162 | +  |
0 commit comments