Deploy to Azure AKS with Github Actions

| March 08, 2020 | 3

Github Actions may not be feature complete or even close to CI/CD rivals [Azure Devops or [Jenkins when it comes to having the most bells and whistles but there is something fresh, new and lightweight to it which i really like. Sure there are some basic things missing such as manually triggering an action, but i am sure that is coming soon.

Its free for open source projects and it is very easy to get started with. Lets see if we can get an automated deployment to an Azure AKS cluster.

Time for some CI/CD

This will be my simple CI/CD pipeline steps:

  • Check out code (I will use my Asp.Net Core [WhoamiCore as the application)
  • Build Docker container
  • Push container to github repo
  • Set Kubernetes context
  • Apply docker manifest files (deployment, service and ingress)
#Put action .yml file in .github/workflows folder.
name: Build and push AKS
on:
push:
#Trigger when a new tag is pushed to repo
#This could just as easily be on code push to master
#but using tags allow a very nice workflow
tags:
- '*'
jobs:
build:
#Run on a GitHub managed agent
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1

#Set environment variable with the tag id
- name: Set env
run: echo ::set-env name=RELEASE_VERSION::${GITHUB_REF#refs/tags/}

Ok – now we have checked out the code in an managed ubuntu container. It already got things like docker installed!

    #Login to dockerhub.io. Other repos also work fine
- uses: azure/docker-login@v1
with:
#Get credentials from Dockerhub secret store
username: ${{ secrets.REGISTRY_USERNAME }}
password: ${{ secrets.REGISTRY_PASSWORD }}
#Build and push container image
- run: |
docker build . -t magohl/whoamicore:${{env.RELEASE_VERSION}}
docker push magohl/whoamicore:${{env.RELEASE_VERSION}}

And finally lets deploy to Azure AKS

     #Set Kubernetes context (Azure AKS)
- uses: azure/aks-set-context@v1
with:
creds: '${{ secrets.AZURE_CREDENTIALS }}' # Azure credentials
resource-group: 'magnus-aks'
cluster-name: 'kramerica-k8s'
id: login

# Deploy to Azure AKS using kubernetes
- uses: Azure/k8s-deploy@v1
with:
namespace: default
#Specify what manifest file or files to use
manifests: |
.manifests/ingress.yaml
.manifests/deployment.yaml
.manifests/service.yaml
#This will replace any image in manifest files with this
#specific version
images: |
index.docker.io/magohl/whoamicore:${{env.RELEASE_VERSION}}

Lets make a change. In my case i changed the deployment to use 5 replicas instead of 3. I create a new tag/release and head over to the Action tab in GitHub.

We can follow the progress in a nice way.

Let´s verify in the ingress reverse proxy ([Traefik) that we now got 5 replicas

The application changes is now automatically deployed to my Azure AKS cluster.

GitHub actions also support running self hosted GitHub Actions agents. This can be a great fit for both enterprise/onprem and local development machine scenarios. More on that later!

comments powered by Disqus