Continuous deployment of spring boot Apps on Azure App service using GIT lab CI
Continuous deployment is an automated process of releasing production code to the desired infrastructure, Only after Continuous Integration pipelines are satisfied.
CI (Continuous Integration) is making sure every piece of code is tested & validated in an automated fashion whenever code pushed to the shared repository.
The process of CI & CD provides agility in release cycles. And for the hobby projects, I try to incorporate CI and stop my pipeline to continues delivery. Continuous delivery is Act where the production-ready code is waiting to take action manually to deploy with very minimal effort.
For my hobby projects, I usually write micro RESTFUL applications that have a single purpose like Authentication and Authorization, managing the profile of the user's which totally depends on the project's need. This micro Rest app may be reused. the consumer of these services knows how to consumes the resources.
As I tend to maintain minimum code quality, a major piece of code will be unit tested along with integration tests that will be checked.
I usually don't keep any quality gate criteria, which can be achieved by tools like Sonar Qube.
My CI tool of choice for Spring boot App is GITLab's CI. And my delivery mode for deployment will be in the form of a docker image.
These are the CI pipeline which I follow
Build
Every commit of the code irrespective of any branch is always tested by running Junit test cases.
Stage
On every bit of code merged to the master branch. I generate the artifact like Jar or War depending on the project along with the build task.
Package
The feature which needs to be deployed, A versioning tag is generated, this action triggers the package task, which generates the docker image of the application. forming the pipeline of tasks containing build, stage, and package.
- Now let us create a `Dockerfile` for the spring app.
FROM maven:3-jdk-11
VOLUME /tmp
ADD /target/*.jar app.jar
EXPOSE 8080/tcp
EXPOSE 10250/tcp
ENTRYPOINT ["java","-jar","/app.jar"]
2. Now let's create a pipeline in `.gitlab-ci.yml` file
image: docker:latest
services:
- docker:dind
variables:
DOCKER_DRIVER: overlay
SPRING_PROFILES_ACTIVE: gitlab-ci
stages:
- build
- stage
- package
maven-test:
image: maven:3-jdk-11
stage: build
script:
- mvn test
- mvn verify
- mvn jacoco:report
artifacts:
reports:
junit:
- target/surefire-reports/TEST-*.xml
- target/failsafe-reports/TEST-*.xml
- target/site/jacoco/index.html
- target/site/jacoco/jacoco.xml
maven-satge:
image: maven:3-jdk-11
stage: stage
when: on_success
only:
- tags
script: "mvn package"
artifacts:
paths:
- target/*.jar
docker-build:
stage: package
when: on_success
only:
- tags
script:
- docker build -t registry.gitlab.com/<repoendpoint> .
- docker login -u gitlab-ci-token -p $CI_BUILD_TOKEN registry.gitlab.com
- docker push registry.gitlab.com/<repoendpoint>
after these steps push the code to repo and create a tag to generate the docker image.
Now for the first time, we shall create an Azure app space where we need our Docker image need to be deployed by following these steps.
Login to azure portal by using this link : https://portal.azure.com/#home
- Create the App service by referring the screen shots
Step 1: Initial Screen.
Step 2: Creating the resource group, Naming the app, Selecting the docker publish method And Selecting the plan
Step 3 : Configuring the docker registry.
3. Final configuration
Just stop and start when ever you need to deploy the app service. the latest docker image will be pulled. also you can create CD(continues deployment) by enabling the web hook in GitLab.