Create two deployment workflows using GitHub Actions and Microsoft Azure.
Nicely done! ❤️
GitHub Actions is cloud agnostic, so any cloud will work. We'll show how to deploy to Azure in this course.
What are Azure resources? In Azure, a resource is an entity managed by Azure. We'll use the following Azure resources in this course:
- A web app is how we'll be deploying our application to Azure.
- A resource group is a collection of resources, like web apps and virtual machines (VMs).
- An App Service plan is what runs our web app and manages the billing (our app should run for free).
Through the power of GitHub Actions, we can create, configure, and destroy these resources through our workflow files.
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.
- Open a new browser tab, and work on the steps in your second tab while you read the instructions in this tab.
- Create a personal access token with the
repo
andwrite:packages
scopes. For more information, see "Creating a personal access token." - 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. - With this done we can move on to setting up our workflow.
Configuring your Azure environment
To deploy successfully to our Azure environment:
- Create a new branch called
azure-configuration
by clicking on the branch dropdown on the top, left hand corner of theCode
tab on your repository page. - Once you're in the new
azure-configuration
branch, go into the.github/workflows
directory and create a new file titledspinup-destroy.yml
by clicking Add file. Copy and paste the following into this new file:name: Configure Azure environment on: pull_request: types: [labeled] env: IMAGE_REGISTRY_URL: ghcr.io AZURE_RESOURCE_GROUP: cd-with-actions AZURE_APP_PLAN: actions-ttt-deployment AZURE_LOCATION: '"East US"' ############################################### ### Replace <username> with GitHub username ### ############################################### AZURE_WEBAPP_NAME: <username>-ttt-app jobs: setup-up-azure-resources: runs-on: ubuntu-latest if: contains(github.event.pull_request.labels.*.name, 'spin up environment') steps: - name: Checkout repository uses: actions/checkout@v4 - name: Azure login uses: azure/login@v2 with: creds: ${{ secrets.AZURE_CREDENTIALS }} - name: Create Azure resource group if: success() run: | az group create --location ${{env.AZURE_LOCATION}} --name ${{env.AZURE_RESOURCE_GROUP}} --subscription ${{secrets.AZURE_SUBSCRIPTION_ID}} - name: Create Azure app service plan if: success() run: | az appservice plan create --resource-group ${{env.AZURE_RESOURCE_GROUP}} --name ${{env.AZURE_APP_PLAN}} --is-linux --sku F1 --subscription ${{secrets.AZURE_SUBSCRIPTION_ID}} - name: Create webapp resource if: success() run: | 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}} - name: Configure webapp to use GHCR if: success() run: | 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}} destroy-azure-resources: runs-on: ubuntu-latest if: contains(github.event.pull_request.labels.*.name, 'destroy environment') steps: - name: Checkout repository uses: actions/checkout@v4 - name: Azure login uses: azure/login@v2 with: creds: ${{ secrets.AZURE_CREDENTIALS }} - name: Destroy Azure environment if: success() run: | az group delete --name ${{env.AZURE_RESOURCE_GROUP}} --subscription ${{secrets.AZURE_SUBSCRIPTION_ID}} --yes
- Click Commit changes... and select
Commit directly to the azure-configuration branch.
before clicking Commit changes. - Go to the Pull requests tab of the repository.
- There should be a yellow banner with the
azure-configuration
branch where you can click Compare & pull request. - Set the title of the Pull request to:
Added spinup-destroy.yml workflow
and clickCreate pull request
.
We will cover the key functionality below and then put the workflow to use by applying a label to the pull request.
This new workflow has two jobs:
- Set up Azure resources will run if the pull request contains a label with the name "spin up environment".
- Destroy Azure resources will run if the pull request contains a label with the name "destroy environment".
In addition to each job, there's a few global environment variables:
AZURE_RESOURCE_GROUP
,AZURE_APP_PLAN
, andAZURE_WEBAPP_NAME
are names for our resource group, app service plan, and web app, respectively, which we'll reference over multiple steps and workflowsAZURE_LOCATION
lets us specify the region for the data centers, where our app will ultimately be deployed.
Setting up Azure resources
The first job sets up the Azure resources as follows:
- Logs into your Azure account with the
azure/login
action. TheAZURE_CREDENTIALS
secret you created earlier is used for authentication. - Creates an Azure resource group by running
az group create
on the Azure CLI, which is pre-installed on the GitHub-hosted runner. - Creates an App Service plan by running
az appservice plan create
on the Azure CLI. - Creates a web app by running
az webapp create
on the Azure CLI. - Configures the newly created web app to use GitHub Packages by using
az webapp config
on the Azure CLI. Azure can be configured to use its own Azure Container Registry, DockerHub, or a custom (private) registry. In this case, we'll configure GitHub Packages as a custom registry.
Destroying Azure resources
The second job destroys Azure resources so that you do not use your free minutes or incur billing. The job works as follows:
- Logs into your Azure account with the
azure/login
action. TheAZURE_CREDENTIALS
secret you created earlier is used for authentication. - Deletes the resource group we created earlier using
az group delete
on the Azure CLI.
- 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 theazure-configuration
branch. - Back in the Pull request, create and apply the
spin up environment
label to your open pull request - 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.
- Wait about 20 seconds then refresh this page (the one you're following instructions from). GitHub Actions will automatically update to the next step.
Get help: Post in our discussion board • Review the GitHub status page
© 2023 GitHub • Code of Conduct • MIT License