A simple application developed in Nest framework to demonstrate deployment to Azure in 2 ways:
- By using Azure Cli and Azure Resource manager templates.
- Though Github Actions workflows.
$ npm install
# development
$ npm run start
# watch mode
$ npm run start:dev
# build docker image
$ docker build -t hello-world:1.0.0 .
# run
$ docker run -p 80:80 hello-world:1.0.0
# verify
$ verify that the application is accessible at http://localhost/hello
This is a common step before deploying though either ways discussed below.
- Download Azure Cli and login to Azure using your tenant
$ az login --tenant <tenant_id>
- Create a resource group
$ az group create -l centralindia -n TestGroup
- Create a group scoped service principal. Save the generated JSON output securely.
$ groupId=$(az group show --name TestGroup --query id --output tsv)
$ MSYS_NO_PATHCONV=1 az ad sp create-for-rbac --name TestApp --role contributor --scope $groupId --sdk-auth
Steps in details here.
- Login with service principal
$ az login — service-principal -u <client_id> -p <client_secret> --tenant <tenant_id>
- Create a container registry
$ az acr create --resource-group TestGroup --name testgroupregistry --sku Basic
- Build and push image to the registry
$ docker build . -t hello-world:1.0.0
$ docker tag hello-world:1.0.0 testgroupregistry.azurecr.io/samples/hello-world:1.0.0
$ az acr login --name testgroupregistry
$ docker push testgroupregistry.azurecr.io/samples/hello-world:1.0.0
- Create a container instance
There is template file
infra/aci.bicep
which is a Azure Resource Manager template for creating Azure Container Instance. We will use it to create an ACI and deploy the docker image generated above.
$ az deployment group create --resource-group TestGroup --template-file infra/aci.bicep --parameters name=helloworlddev image=testgroupregistry.azurecr.io/samples/hello-world:1.0.0 registryServer=testgroupregistry.azurecr.io clientId=<client_id> clientSecret=<client_secret> dnsNameLabel=helloworlddevtest port=80
Steps in details here.
- Store service pricipal credentials in your GitHub repo's secrets:
AZURE_CREDENTIALS => <the service principal json generated above>
AZURE_USERNAME => <clientId of the service principal>
AZURE_PASSWORD => <clientSecret of the service principal>
- The
.github/workflows/workflow.yml
takes care of creating the ACI and deployment on every push.