How to Create a GitHub Action to deploy to Azure AKS

In this unit, you’ll complete the following tasks: Create a GitHub Action to implement a deployment pipeline Increment the coupon service version in the Helm chart, Verify that the changes were deployed to the AKS cluster, Roll back a deployment

Create the deployment action

Create a GitHub Action for deployment of the coupon service with the following steps:

  1. Select the Actions tab again, select the New workflow button, and select the set up a workflow yourself link.
  2. Replace the YAML in the editor with the following YAML:
    name: eShop deploy
    
    on:
      push:
        paths:
        - 'deploy/k8s/helm-simple/coupon/*'
        branches: [ main ]
    
    jobs:
      deploy-to-aks:
        runs-on: ubuntu-latest
        steps:
        - name: Azure Kubernetes set context
          uses: Azure/aks-set-context@v1
          with:
            creds: ${{ secrets.AZURE_CREDENTIALS }}
            resource-group: 'eshop-learn-rg'
            cluster-name: 'eshop-learn-aks'
    
        - name: Get code from the repository
          uses: actions/checkout@v1
          with:
            ref: main
    
        - name: Helm tool installer
          uses: Azure/setup-helm@v1
    
        - name: Azure Login
          uses: Azure/login@v1.1
          with:
            creds: ${{ secrets.AZURE_CREDENTIALS }}
    
        - name: Deploy
          run: >
            helm upgrade 
            --install eshoplearn-coupon
            --namespace=default 
            --set registry=${{ secrets.REGISTRY_LOGIN_SERVER }} 
            --set imagePullPolicy=Always 
            --set host=${{ secrets.IP_ADDRESS }} 
            --set protocol=http './deploy/k8s/helm-simple/coupon'
    

    The preceding YAML defines a GitHub Action that:

    • Is triggered when a commit is pushed to the coupon service’s Helm chart in the main branch.
    • Has one job, named deploy-to-aks, that deploys new images. The job runs in an ubuntu-latest runner and has five steps:
      1. Azure Kubernetes set context sets the AKS credentials in the runner’s .kube/config file.
      2. Get code from the repository checks out the code from the repository.
      3. Helm tool installer installs Helm, an open-source package manager for Kubernetes.
      4. Azure Login logs in to Azure using the service principal credentials.
      5. Deploy executes the helm upgrade command, passing the ACR instance name as the registry parameter. This parameter tells Helm to use your ACR instance rather than the public container registry.
  3. Replace the default workflow file name of main.yml with deploy.yml.
  4. Commit the deploy.yml workflow file directly to the main branch.

These two GitHub Action definitions are stored in the repository’s .github/workflows directory. To make changes, update the appropriate file locally and push to the main branch. Alternatively, create a pull request (PR). If you create a PR, the Action is triggered when merging to main.

Trigger a deployment

To trigger a deployment, you’ll increment the appVersion in the coupon service’s Helm chart. Helm charts are used to define the service specification in a YAML template format.

  1. From the Code tab, edit the deploy/k8s/helm-simple/coupon/Chart.yaml file by clicking the edit icon. Update the appVersion property value to 1.1.0:
    apiVersion: v2
    name: coupon
    
    # YAML omitted for brevity
    
    # This is the version number of the application being deployed. This version number should be
    # incremented each time you make changes to the application.
    appVersion: 1.1.0
    

    It’s important that you update the app version in the Helm chart. This change causes the pod to be replaced when the chart is deployed to AKS with helm upgrade.

  2. Commit and push this change to the main branch.The deployment workflow is triggered automatically. The app is deployed after a few minutes.

More Article : 4 Best web hosting Reddit 2020

Monitor the deployment

  1. Select the Actions tab in your repository to monitor the deployment’s progress.
  2. Select the most recent workflow run listed for the eShop deploy workflow. The commit message used in the previous step becomes the run’s name. 
  3. Select the deploy-to-aks task:deploy-to-aks-task 

    In the preceding image, you can see details of the deploy-to-aks job for this particular workflow run. The Set up job and Complete job steps are listed. In between those two steps are the custom steps defined within the deploy-to-aks job.

  4. Select the Actions tab again. You’ll see a variation of the following screen when the deployment completes:deployment-action
  5. Back in the command shell, run the following command to monitor the coupon service pods in your AKS cluster:
    kubectl get pods --selector service=coupon --watch
    

    In the preceding command:

    • The --selector flag filters the list to only pods for the coupon service. For this reason, pods for other services in the cluster aren’t displayed.
    • The --watch flag instructs kubectl to watch for changes. When a change is detected, an additional table row is appended to the command shell output. The status of the existing and newly deployed pods is displayed in real time.

    A variation of the following output appears:

    NAME                              READY   STATUS              RESTARTS   AGE
    coupon-5b9597995-7s4hh            1/1     Running             1          31m
    coupon-74fd48bbd-rqgfd            0/1     ContainerCreating   0          22s
    

    In the preceding output, notice that a new coupon pod was created. While the old pod is still running and when the new pod is ready, the old one is terminated. This process makes the transition to the new version as smooth as possible.

  6. Once the new pod’s Ready status displays 1/1, press Ctrl+C to stop kubectl‘s watch mode.
  7. Run the following command to check the coupon service deployment history:
    helm history eshoplearn-coupon
    

    The history shows the new coupon service version has been deployed.

    REVISION        UPDATED                         STATUS          CHART           APP VERSION     DESCRIPTION
    1               Thu Sep 10 19:19:31 2020        superseded      coupon-0.1.0    1.0.0           Install complete
    2               Thu Sep 10 19:51:10 2020        deployed        coupon-0.1.0    1.1.0           Upgrade complete
    

Read more: Best managed WordPress hosting reddit 

Verify the deployment

Complete the following steps to verify your change is deployed:

  1. Run the following command in the command shell:
    cat ~/clouddrive/aspnet-learn-temp/deployment-urls.txt
    
  2. Select the Web SPA application URL to launch the app.
  3. Log in from the LOGIN page.
  4. Add your favorite products to the shopping bag by selecting the images.
  5. Select the shopping bag icon in the upper right, and select CHECKOUT.
  6. Enter the code DISC-5 in the HAVE A DISCOUNT CODE? text box for a five USD discount, and select APPLY.
  7. Select PLACE ORDER to complete the purchase.
  8. Back in the command shell, select the Centralized logging URL.
  9. In the Seq logs search text box, enter Applying coupon DISC-5 and press Enter.The logs are filtered to display the following entry:seq-logs-azure

     

Roll back the deployment

During production issues, one common mitigation is to revert a deployment to a known good deployment. Use the following command to roll back from version 1.1.0 to 1.0.0.

helm rollback eshoplearn-coupon

The deployment history confirms that everything is back to normal:

Rollback was a success! Happy Helming!

 Note: In a real-life scenario, you’ll have multiple environments to which the build’s artifacts can be deployed. For example, you might have development, testing, and staging environments. The deployment workflows can be triggered by events like merging PRs. Quality or approval gates, such as a stakeholder’s PR approval, can be added to prevent unexpected deployments to production.

In this unit, you created a GitHub Action to deploy the coupon service to AKS. To test the deployment workflow, you incremented the coupon service version in its Helm chart. Upon completion of the deployment workflow run, you searched the Seq logs to confirm the discount code redemption message was present. Finally, you reverted the coupon service in AKS to version 1.0.0.

Happy deploying!

Leave a Reply