GitHub Action – Deploy Applications to AKS

GitHub action allows developers and DevOps engineers to automate the software development workflows from the same code store. We can write individual tasks (actions) to create custom workflows. Workflows are custom automated procedures we can set up in the repository to build, test, packaged, released, and deployed.

With the use of GitHub action, we can build a Continues Integration (CI) and Continuous Deployment (CD) from directly to different cloud environments. Those workflows run in Linux, Windows, macOS GitHub hosted machines.

From this post, I’ll demonstrate how to build an flask application and create a docker image, push to ACR (Azure Container Registry) and finally deploy to AKS (Azure Kubernetes Service)

Create GitHub Workflows inside the repository

The first thing we have to do is to create the .github folder inside the project. This folder is used to create workflows. Inside the folder create another folder called workflows. For this demo, I’ll create two workflows.

  • CI Workflow – Building the application
  • CD Workflow – Deploy to AKS

CI Workflow

First, take a look at the CI workflow this workflow content a few steps. CI workflow build the application and download any required dependencies

First part of the CI workflow contains the which action trigger the workflow

name: Flask Application
on:
  pull_request:
    branches:
    - master

This CI workflow triggers when we made a pull request to master branch.

Next we look in to jobs related to CI workflow

jobs:
  build:
    runs-on: ubuntu-16.04
    steps:
      - uses: actions/[email protected]
      - name: Set up Python 3.7
        uses: actions/[email protected]
        with:
          python-version: 3.7
      - name: Install Dependencies for the app
        run: |
          python -m pip install --upgrade pip
          pip install -r requirements.txt

Under the jobs first, we need to specify which hosted agent going to use for the build. In this case, ubuntu-16.04 used and under runs-on, we specify this configuration.

Under steps, specify the tasks to be tun on this workflow. In this demo, we run two tasks. First task setup the python version for the application and second task install any dependencies related to the application.

CD Workflow

CD (Continues Deployment) workflow we define the application packaging using docker image then push this image to ACR (Azure Container Registry) then deploy the application deploys to AKS (Azure Kubernetes Service)

First part of the CD process this we need to specify the with which action the workflow triggers.

name: Deploy to AKS Cluster
on:
  pull_request:
    branches:
    - master

Next we need to specify steps under the jobs. First checkout the code from master branch and then use docker login, to login to the ACR to build and push the image. Username and Password are sensitive and we can store them in GitHub secrets and refer it as ${{ secrets.ACR_USERNAME }}. Secrets can found on under settings in repository.

Next we use action to build docker image and push the build image to ACR.

- uses: actions/[email protected]
    
    - uses: Azure/[email protected]
      with:
        login-server: cloudlifeacr.azurecr.io
        username: ${{ secrets.ACR_USERNAME }}
        password: ${{ secrets.ACR_PASSWORD }}
    
    - run: |
        docker build . -t cloudlifeacr.azurecr.io/k8sflask:${{ github.sha }}
        docker push cloudlifeacr.azurecr.io/k8sflask:${{ github.sha }}

Next use Azure AKS context to login to AKS cluster for application deployment.

    # Set the target AKS cluster.
    - uses: Azure/[email protected]
      with:
        creds: '${{ secrets.AZURE_CREDENTIALS }}'
        cluster-name: aksdemokasun
        resource-group: AKS-Demo-Cluster-RG

Next create a ImagePullSecret to push the image from private ACR repository.

    - uses: Azure/k8s-create[email protected]
      with:
        container-registry-url: cloudlifeacr.azurecr.io
        container-registry-username: ${{ secrets.ACR_USERNAME }}
        container-registry-password: ${{ secrets.ACR_PASSWORD }}
        secret-name: k8s-secret

Next, deploy the application using the YAML manifests. For this application, we use deployment and a LoadBalance service. YAML files can be found in this repo under manifests

- uses: Azure/[email protected]
      with:
        manifests: |
          manifests/deployment.yaml
          manifests/service.yaml
        images: |
          cloudlifeacr.azurecr.io/k8sflask:${{ github.sha }}
        imagepullsecrets: |
          k8s-secret

Complete CD workflow as follows. link to AKS deployment actions are follows.

Following Demo shows the steps explained above

GitHub Actions are very useful for checking different check on repository or can use as CI/CD workflow to deploy applications.