…
|
||
---|---|---|
.. | ||
media | ||
README.md |
README.md
11 - Configure CI/CD
This guide is part of the Azure Spring Apps training
In this section, we will use GitHub Actions to implement continuous deployment to Azure Spring Apps. For simplicity, we will not implement blue-green deployments in this section, but don't hesitate to come back and add blue-green deployments after completing the remainder of the tutorial.
Our microservices and gateway are easy to deploy manually, but it is of course better to automate all those tasks! We are going to use GitHub actions as a Continuous Integration / Continuous Deployment platform (or CI/CD for short). This configuration is rather simple, so it should be trivial to port it to another CI/CD platform.
We are going to automate the deployment of the weather-service
microservice that was developed in 07 - Build a Spring Boot microservice using MySQL. It is exactly the same configuration that would need to be done for the city-service
microservice and the gateway, so if you want to automate them too, you can just copy/paste what is being done in the current guide.
Configure GitHub
Create a new GitHub repository and commit the code from the weather-service
microservice into that repository:
🛑 Make sure you substitute the Git URL from your own github repository (make sure you use the HTTPS URL, not the SSH URL). This should be a different repository than the one you used to store configuration in section 4. If a login dialog appears, log in with your regular GitHub credentials.
cd weather-service
git init
git add .
git commit -m 'Initial commit'
git remote add origin <GIT HTTPS URL HERE>
git push origin main
cd ..
You now need to allow access from your GitHub workflow to your Azure Spring Apps instance. Open up a terminal and type the following command, replacing $AZ_RESOURCE_GROUP
with the name of your resource group.
🛑 Make sure you assign the name of your resource group to the variable AZ_RESOURCE_GROUP
or substitute the value for it in the commands below.
# Prevents a Git bash issue. Not necessary outside of Windows:
export MSYS_NO_PATHCONV=1
# Get the ARM resource ID of the resource group
export RESOURCE_ID=$(az group show --name $AZ_RESOURCE_GROUP --query id -o tsv)
# Create a service principal with a Contributor role to the resource group.
export SPNAME=sp-$AZ_SPRING_APPS_NAME
az ad sp create-for-rbac --name $SPNAME --role contributor --scopes $RESOURCE_ID --sdk-auth
This should output a JSON text, that you need to copy.
Then, in your GitHub project, 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 weather-service
directory, create a new directory called .github/workflows
and add a file called azure-spring-apps.yml
in it. This file is a GitHub workflow and will use the secret we just configured above to deploy the application to your Azure Spring Apps instance.
In that file, copy/paste the following content, performing the indicated substitutions:
🛑 You must substitute the name of your Azure Spring Apps instance for
<AZ_SPRING_APPS_NAME>
in the YAML below.
name: Build and deploy to Azure Spring Apps
on: [push]
jobs:
build-and-deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up Java
uses: actions/setup-java@v2
with:
distribution: 'temurin'
java-version: 17
check-latest: false
cache: 'maven'
- name: Build with Maven
run: mvn package -DskipTests
- name: Login to Azure Spring Apps
uses: azure/login@v1
with:
creds: ${{ secrets.AZURE_CREDENTIALS }}
- name: Get Subscription ID
run: |
echo "SUBSCRIPTION_ID=$(az account show --query id --output tsv --only-show-errors)" >> $GITHUB_ENV
shell: bash
- name: Deploy to Azure Spring Apps
uses: azure/spring-cloud-deploy@v1
with:
action: deploy
azure-subscription: ${{ env.SUBSCRIPTION_ID }}
service-name: <AZ_SPRING_APPS_NAME>
app-name: weather-service
use-staging-deployment: false
package: 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 Apps using the credentials we just configured
- It fetches your current subscription ID
- It deploys the application to your Azure Spring Apps instance, using the Azure Spring Apps GitHub Action.
This workflow is configured to be triggered whenever code is pushed to the repository. There are many other events that trigger GitHub actions. You could, for example, deploy each time a new tag is created on the project.
Test the GitHub Action
You can now commit and push the azure-spring-apps.yml
file we just created.
Going to the Actions
tab of your GitHub project, you should see that your project is automatically built and deployed to your Azure Spring Apps instance:
Congratulations! Each time you git push
your code, your microservice is now automatically deployed to production.
⬅️ Previous guide: 10 - Blue/Green deployment
➡️ Next guide: 12 - Making Microservices Talk To Each Other