GitHub Action — Deploy Applications to AKS

Kasun Rajapakse
Ascentic Technology
4 min readJun 2, 2020

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 a 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 the master branch.

Next we look into jobs related to CI workflow

jobs:
build:
runs-on: ubuntu-16.04
steps:
- uses: actions/checkout@v1
- name: Set up Python 3.7
uses: actions/setup-python@v1
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)

The 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 the 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 under settings in repository.

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

- uses: actions/checkout@master

- uses: Azure/docker-login@v1
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 the AKS cluster for application deployment.

# Set the target AKS cluster.
- uses: Azure/aks-set-context@v1
with:
creds: '${{ secrets.AZURE_CREDENTIALS }}'
cluster-name: aksdemokasun
resource-group: AKS-Demo-Cluster-RG

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

- uses: Azure/k8s-create-secret@v1
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/k8s-deploy@v1
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 as follows.

Following Demo shows the steps explained above

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

--

--

Kasun Rajapakse
Ascentic Technology

Anything related to Azure, AWS, GCP, Containers & Kubernetes. Technology enthusiastic, Learner, Blogger