Creating GitHub Actions to Automate PRISM Mock API server in Azure Container App Deployment
If you have read two of my previous posts, you would have successfully hosted PRISM Mock API server in Azure Container App. This way, your mock API runs on the cloud with very minimal cost.
Prerequisite
Setting Up Azure Service Principal
Creating Azure Service Principal using Azure CLI
# Create a service principal with required parameter
az ad sp create-for-rbac --scopes /subscriptions/mySubscriptionID
# Create a service principal for a resource group using a preferred name and role
az ad sp create-for-rbac --name myServicePrincipalName \
--role reader \
--scopes /subscriptions/mySubscriptionID/resourceGroups/myResourceGroupName
Creating Azure Service Principal using Azure Portal
Setting Up Git Repository
git init
node_modules
git add --all
git commit -m "initial commit"
Recommended by LinkedIn
GitHub
There are multiple ways to get existing code to GitHub either by creating a new repository and uploading the file manually or by adding GitHub as the origin for an existing repository. But, another easier way is to add the existing repository from GitHub desktop and pushing from there.
GitHub Actions
Now we have come to the interesting part of setting up the GitHub actions that will do the following:
Creating GitHub Actions Secret and Variables
{
"clientId": "<This should be the client id of the service principal created above>",
"clientSecret": "<This should be client secret generated in the step above>",
"subscriptionId": "<Your Azure subscription ID>",
"tenantId": "<Your Azure AD tenant ID>"
}
APP_NAME
AZURE_CONTAINER_APP_ENVIRONMENT
AZURE_CONTAINER_APP_NAME
AZURE_CONTAINER_APP_RESOURCE_GROUP
GitHub Actions Workflow File
name: CI/CD Prism Mock API
on:
# Triggers the workflow on push or pull request events but only for the "main" branch
push:
branches: ["master"]
workflow_dispatch:
jobs:
build_and_containerise:
name: Build & Containerise & Push to Docker Hub
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Login to Docker Hub
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Build and push
uses: docker/build-push-action@v4
with:
context: .
file: ./Dockerfile
push: true
tags: ${{ secrets.DOCKERHUB_USERNAME }}/${{ vars.APP_NAME }}:${{ github.sha }}
deploy:
name: Deploy to ACA
needs: [ build_and_containerise ]
runs-on: ubuntu-latest
steps:
- name: Log in to Azure
uses: azure/login@v1
with:
creds: ${{ secrets.AZURE_CREDENTIALS }}
- name: Deploy service
uses: azure/container-apps-deploy-action@v1
with:
registryUrl: ${{ vars.REGISTRY_URL }}
registryUsername: ${{ secrets.REGISTRY_USERNAME }}
registryPassword: ${{ secrets.REGISTRY_PASSWORD }}
resourceGroup: ${{ vars.AZURE_CONTAINER_APP_RESOURCE_GROUP }}
containerAppName: ${{ vars.AZURE_CONTAINER_APP_NAME }}
containerAppEnvironment: ${{ vars.AZURE_CONTAINER_APP_ENVIRONMENT }}
imageToDeploy: ${{ secrets.REGISTRY_USERNAME }}/${{ vars.APP_NAME }}:${{ github.sha }}
GitHub Actions Workflow Explanation
Build and Containerise
build_and_containerise:
name: Build & Containerise & Push to Docker Hub
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Login to Docker Hub
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Build and push
uses: docker/build-push-action@v4
with:
context: .
file: ./Dockerfile
push: true
tags: ${{ secrets.DOCKERHUB_USERNAME }}/${{ vars.APP_NAME }}:${{ github.sha }}
Deploy
deploy:
name: Deploy to ACA
needs: [ build_and_containerise ]
runs-on: ubuntu-latest
steps:
- name: Log in to Azure
uses: azure/login@v1
with:
creds: ${{ secrets.AZURE_CREDENTIALS }}
- name: Deploy service
uses: azure/container-apps-deploy-action@v1
with:
registryUrl: ${{ vars.REGISTRY_URL }}
registryUsername: ${{ secrets.REGISTRY_USERNAME }}
registryPassword: ${{ secrets.REGISTRY_PASSWORD }}
resourceGroup: ${{ vars.AZURE_CONTAINER_APP_RESOURCE_GROUP }}
containerAppName: ${{ vars.AZURE_CONTAINER_APP_NAME }}
containerAppEnvironment: ${{ vars.AZURE_CONTAINER_APP_ENVIRONMENT }}
imageToDeploy: ${{ secrets.REGISTRY_USERNAME }}/${{ vars.APP_NAME }}:${{ github.sha }}
Conclusion
If you managed to follow this article, you should be able to automate build and deployment of you Prism Mock API server to Azure Container App. This can also be a guide if you want to deploy another application written in any language or framework be it nodeJS, dotnet, Java, Go or Python.
The best part of using this combination of GitHub actions, Docker Hub container registry and also Azure Container Apps is that it is FREE and we all know that free is good.