|
| 1 | +## Step 3: Spin up an environment based on labels |
| 2 | + |
| 3 | +_Nicely done! :heart:_ |
| 4 | + |
| 5 | +GitHub Actions is cloud agnostic, so any cloud will work. We'll show how to deploy to Azure in this course. |
| 6 | + |
| 7 | +**What are _Azure resources_?** In Azure, a resource is an entity managed by Azure. We'll use the following Azure resources in this course: |
| 8 | + |
| 9 | +- A [web app](https://docs.microsoft.com/en-us/azure/app-service/overview) is how we'll be deploying our application to Azure. |
| 10 | +- A [resource group](https://docs.microsoft.com/en-us/azure/azure-resource-manager/management/overview#resource-groups) is a collection of resources, like web apps and virtual machines (VMs). |
| 11 | +- An [App Service plan](https://docs.microsoft.com/en-us/azure/app-service/overview-hosting-plans) is what runs our web app and manages the billing (our app should run for free). |
| 12 | + |
| 13 | +Through the power of GitHub Actions, we can create, configure, and destroy these resources through our workflow files. |
| 14 | + |
| 15 | +### :keyboard: Activity 1: Set up a personal access token (PAT) |
| 16 | + |
| 17 | +Personal access tokens (PATs) are an alternative to using passwords for authentication to GitHub. We will use a PAT to allow your web app to pull the container image after your workflow pushes a newly built image to the registry. |
| 18 | + |
| 19 | +1. Open a new browser tab, and work on the steps in your second tab while you read the instructions in this tab. |
| 20 | +2. Create a personal access token with the `repo` and `read:packages` scopes. For more information, see ["Creating a personal access token."](https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/creating-a-personal-access-token) |
| 21 | +3. Once you have generated the token we will need to store it in a secret so that it can be used within a workflow. Create a new repository secret named `CR_PAT` and paste the PAT token in as the value. |
| 22 | +4. With this done we can move on to setting up our workflow. |
| 23 | + |
| 24 | +**Configuring your Azure environment** |
| 25 | + |
| 26 | +To deploy successfully to our Azure environment: |
| 27 | + |
| 28 | +1. Create a new branch called `azure-configuration` by clicking on the branch dropdown on the top, left hand corner of the `Code` tab on your repository page. |
| 29 | +2. Once you're in the new `azure-configuration` branch, go into the `.github/workflows` directory and create a new file titled `spinup-destroy.yml` by clicking **Add file**. |
| 30 | + |
| 31 | +Copy and paste the following into this new file: |
| 32 | + |
| 33 | +```yaml |
| 34 | +name: Configure Azure environment |
| 35 | + |
| 36 | +on: |
| 37 | + pull_request: |
| 38 | + types: [labeled] |
| 39 | + |
| 40 | +env: |
| 41 | + IMAGE_REGISTRY_URL: ghcr.io |
| 42 | + AZURE_RESOURCE_GROUP: cd-with-actions |
| 43 | + AZURE_APP_PLAN: actions-ttt-deployment |
| 44 | + AZURE_LOCATION: '"Central US"' |
| 45 | + ############################################### |
| 46 | + ### Replace <username> with GitHub username ### |
| 47 | + ############################################### |
| 48 | + AZURE_WEBAPP_NAME: <username>-ttt-app |
| 49 | + |
| 50 | +jobs: |
| 51 | + setup-up-azure-resources: |
| 52 | + runs-on: ubuntu-latest |
| 53 | + if: contains(github.event.pull_request.labels.*.name, 'spin up environment') |
| 54 | + steps: |
| 55 | + - name: Checkout repository |
| 56 | + uses: actions/checkout@v3 |
| 57 | + |
| 58 | + - name: Azure login |
| 59 | + uses: azure/login@v1 |
| 60 | + with: |
| 61 | + creds: ${{ secrets.AZURE_CREDENTIALS }} |
| 62 | + |
| 63 | + - name: Create Azure resource group |
| 64 | + if: success() |
| 65 | + run: | |
| 66 | + az group create --location ${{env.AZURE_LOCATION}} --name ${{env.AZURE_RESOURCE_GROUP}} --subscription ${{secrets.AZURE_SUBSCRIPTION_ID}} |
| 67 | +
|
| 68 | + - name: Create Azure app service plan |
| 69 | + if: success() |
| 70 | + run: | |
| 71 | + az appservice plan create --resource-group ${{env.AZURE_RESOURCE_GROUP}} --name ${{env.AZURE_APP_PLAN}} --is-linux --sku F1 --subscription ${{secrets.AZURE_SUBSCRIPTION_ID}} |
| 72 | +
|
| 73 | + - name: Create webapp resource |
| 74 | + if: success() |
| 75 | + run: | |
| 76 | + az webapp create --resource-group ${{ env.AZURE_RESOURCE_GROUP }} --plan ${{ env.AZURE_APP_PLAN }} --name ${{ env.AZURE_WEBAPP_NAME }} --deployment-container-image-name nginx --subscription ${{secrets.AZURE_SUBSCRIPTION_ID}} |
| 77 | +
|
| 78 | + - name: Configure webapp to use GHCR |
| 79 | + if: success() |
| 80 | + run: | |
| 81 | + az webapp config container set --docker-custom-image-name nginx --docker-registry-server-password ${{secrets.CR_PAT}} --docker-registry-server-url https://${{env.IMAGE_REGISTRY_URL}} --docker-registry-server-user ${{github.actor}} --name ${{ env.AZURE_WEBAPP_NAME }} --resource-group ${{ env.AZURE_RESOURCE_GROUP }} --subscription ${{secrets.AZURE_SUBSCRIPTION_ID}} |
| 82 | +
|
| 83 | + destroy-azure-resources: |
| 84 | + runs-on: ubuntu-latest |
| 85 | + |
| 86 | + if: contains(github.event.pull_request.labels.*.name, 'destroy environment') |
| 87 | + |
| 88 | + steps: |
| 89 | + - name: Checkout repository |
| 90 | + uses: actions/checkout@v3 |
| 91 | + |
| 92 | + - name: Azure login |
| 93 | + uses: azure/login@v1 |
| 94 | + with: |
| 95 | + creds: ${{ secrets.AZURE_CREDENTIALS }} |
| 96 | + |
| 97 | + - name: Destroy Azure environment |
| 98 | + if: success() |
| 99 | + run: | |
| 100 | + az group delete --name ${{env.AZURE_RESOURCE_GROUP}} --subscription ${{secrets.AZURE_SUBSCRIPTION_ID}} --yes |
| 101 | +``` |
| 102 | +
|
| 103 | +3. Click **Commit changes...** and select `Commit directly to the azure-configuration branch.` before clicking **Commit changes**. |
| 104 | +4. Go to the Pull requests tab of the repository. |
| 105 | +5. There should be a yellow banner with the `azure-configuration` branch where you can click **Compare & pull request**. |
| 106 | +6. Set the title of the Pull request to: `Added spinup-destroy.yml workflow` and click `Create pull request`. |
| 107 | + |
| 108 | +We will cover the key functionality below and then put the workflow to use by applying a label to the pull request. |
| 109 | + |
| 110 | +This new workflow has two jobs: |
| 111 | + |
| 112 | +1. **Set up Azure resources** will run if the pull request contains a label with the name "spin up environment". |
| 113 | +2. **Destroy Azure resources** will run if the pull request contains a label with the name "destroy environment". |
| 114 | + |
| 115 | +In addition to each job, there's a few global environment variables: |
| 116 | + |
| 117 | +- `AZURE_RESOURCE_GROUP`, `AZURE_APP_PLAN`, and `AZURE_WEBAPP_NAME` are names for our resource group, app service plan, and web app, respectively, which we'll reference over multiple steps and workflows |
| 118 | +- `AZURE_LOCATION` lets us specify the [region](https://azure.microsoft.com/en-us/global-infrastructure/regions/) for the data centers, where our app will ultimately be deployed. |
| 119 | + |
| 120 | +**Setting up Azure resources** |
| 121 | + |
| 122 | +The first job sets up the Azure resources as follows: |
| 123 | + |
| 124 | +1. Logs into your Azure account with the [`azure/login`](https://github.com/Azure/login) action. The `AZURE_CREDENTIALS` secret you created earlier is used for authentication. |
| 125 | +1. Creates an [Azure resource group](https://docs.microsoft.com/en-us/azure/azure-resource-manager/management/overview#resource-groups) by running [`az group create`](https://docs.microsoft.com/en-us/cli/azure/group?view=azure-cli-latest#az-group-create) on the Azure CLI, which is [pre-installed on the GitHub-hosted runner](https://help.github.com/en/actions/reference/software-installed-on-github-hosted-runners). |
| 126 | +1. Creates an [App Service plan](https://docs.microsoft.com/en-us/azure/app-service/overview-hosting-plans) by running [`az appservice plan create`](https://docs.microsoft.com/en-us/cli/azure/appservice/plan?view=azure-cli-latest#az-appservice-plan-create) on the Azure CLI. |
| 127 | +1. Creates a [web app](https://docs.microsoft.com/en-us/azure/app-service/overview) by running [`az webapp create`](https://docs.microsoft.com/en-us/cli/azure/webapp?view=azure-cli-latest#az-webapp-create) on the Azure CLI. |
| 128 | +1. Configures the newly created web app to use [GitHub Packages](https://help.github.com/en/packages/publishing-and-managing-packages/about-github-packages) by using [`az webapp config`](https://docs.microsoft.com/en-us/cli/azure/webapp/config?view=azure-cli-latest) on the Azure CLI. Azure can be configured to use its own [Azure Container Registry](https://docs.microsoft.com/en-us/azure/container-registry/), [DockerHub](https://docs.docker.com/docker-hub/), or a custom (private) registry. In this case, we'll configure GitHub Packages as a custom registry. |
| 129 | + |
| 130 | +**Destroying Azure resources** |
| 131 | + |
| 132 | +The second job destroys Azure resources so that you do not use your free minutes or incur billing. The job works as follows: |
| 133 | + |
| 134 | +1. Logs into your Azure account with the [`azure/login`](https://github.com/Azure/login) action. The `AZURE_CREDENTIALS` secret you created earlier is used for authentication. |
| 135 | +1. Deletes the resource group we created earlier using [`az group delete`](https://docs.microsoft.com/en-us/cli/azure/group?view=azure-cli-latest#az-group-delete) on the Azure CLI. |
| 136 | + |
| 137 | +### :keyboard: Activity 2: Apply labels to create resources |
| 138 | + |
| 139 | +1. Edit the `spinup-destroy.yml` file in your open pull request and replace any `<username>` placeholders with your GitHub username. Commit this change directly to the `azure-configuration` branch. |
| 140 | +1. Back in the Pull request, create and apply the `spin up environment` label to your open pull request |
| 141 | +1. Wait for the GitHub Actions workflow to run and spin up your Azure environment. You can follow along in the Actions tab or in the pull request merge box. |
| 142 | +1. Wait about 20 seconds then refresh this page (the one you're following instructions from). [GitHub Actions](https://docs.github.com/en/actions) will automatically update to the next step. |
0 commit comments