Authors- Rajat Malik (Cloud Engineer), Anubhav Bisht (Associate Cloud Engineer)
Overview
This walk through explains how to perform Continuous Integration and Continuous Deployment of your spring micro service in Azure Spring Cloud using Github as the source controller. To generate a spring application and perform a manual deployment to Azure Spring Cloud, check the following link: https://ifi-prod-poc-2.azurewebsites.net/?p=9059&preview=true
We will automate the deployment of the “helloifi” micro service that we developed in the earlier blog above.
Configure GitHub
Create a git repository and commit the code from the “helloifi” microservice into that repository:
cd helloifi
git init
git add .
git commit -m 'Initial commit'
git remote add origin <GIT HTTPS URL HERE>
git push origin master
cd ..
The code section of your git repository should show the following:
You now need to allow access from your GitHub workflow to your Azure Spring Cloud cluster. Open up a powershell window and copy/paste the following commands:
Powershell:
$RESOURCE_ID=$(az group show –name "$RESOURCE_GROUP_NAME" –query id -o tsv)
$SPNAME="sp-$(az spring-cloud list –query '[].name' -o tsv)"
az ad sp create-for-rbac –name "${SPNAME}" –role contributor –scopes "$RESOURCE_ID" –sdk-auth
This will output a JSON text, that you need to copy.
Then, in your project’s GitHub repository, select Settings > Secrets
and add a new secret called “AZURE CREDENTIALS”. Paste the JSON text you just copied into that secret.
Create a GitHub Action
Inside the helloifi directory, create a new directory called “workflow” and add a file called “azure-spring-cloud.yml” in it. This file is a GitHub workflow and will use the secret we just configured to deploy the application to your Azure Spring Cloud instance.
In that file, copy/paste the following content, performing the indicated substitutions:
name: Build and deploy to Azure Spring Cloud
on: [push]
jobs:
build-and-deploy:
runs-on: ubuntu-latest
steps:
– uses: actions/checkout@v2
– name: Set up JDK 1.8
uses: actions/setup-java@v1
with:
java-version: 1.8
– name: Build with Maven
run: mvn package -DskipTests -Pcloud
– name: Login to Azure Spring Cloud
uses: azure/login@v1
with:
creds: ${{ secrets.AZURE_CREDENTIALS }}
– name: Install Azure Spring Cloud extension
run: az extension add -y –name spring-cloud
– name: Deploy to Azure Spring Cloud
run: az spring-cloud app deploy –resource-group <RESOURCE_GROUP> –service <SPRING_CLOUD_NAME> –name todo-service –jar-path target/demo-0.0.1-SNAPSHOT.jar
This workflow does the following:
- It sets up the JDK.
- It compiles and packages the application using Maven.
- It authenticates to Azure Spring Cloud using the credentials we just configured.
- It adds the Azure Spring Cloud extensions to the Azure CLI (this step should disappear when the service is in final release).
- It deploys the application to your Azure Spring Cloud instance.
This workflow is configured to be triggered whenever code is pushed to the repository.
Test the GitHub Action
You can now commit and push the azure-spring-cloud.yml
file we just created.
Now each time you perform “git push” on your code, your micro service is now automatically deployed to production.