In the last post, I introduced about how to deploy a Spring Boot app to a GCP Compute engine using Jenkins. This procedure automated some tasks to simplify the deployment process such as build Jar file, upload Jar file to VM, run the Jar file. However, it is not a fully CI-CD solution.
Requirement for this time:
- Engineer commits code to Github, Jenkins automatically detect the code changes and deploy to VM
- As running the Jar file directly using java -jar is not an elegant way, this time we run the Spring Boot as a Linux service.
Let's modify the previous Jenkins job:
Create a webhook
- Go to Github repo's Setting > Webhooks > Add webhook
- Fill Payload URL with: http://<jenkins-server-IP>:8080/github-webhook/
The purpose of this webhook is that whenever there is an event of Pushing code, Github will call the URL to trigger Jenkins.
Integrate webhook into Jenkins
- We need install Github Integration Plugin
- Change the Configure of previous Jenkins job by adding one more step Build Triggers
That's it, now we can test by commit and push to Github, Jenkins will automatically trigger the Job.
Run Spring Boot as a service
- ssh to the Application VM, create a file named demo-cors.service
cd /etc/systemd/system/ vim demo-cors.service
with following content
[Unit] Description=A Spring Boot application After=syslog.target [Service] User=jenkins ExecStart=/usr/bin/java -jar demo-cors.jar SuccessExitStatus=143 WorkingDirectory=/home/jenkins [Install] WantedBy=multi-user.target
- Reload to notify system about the new service
sudo systemctl daemon-reload
- Now we can run the Spring Boot app by
sudo systemctl start demo-cors
sudo systemctl stop demo-cors sudo systemctl restart demo-cors sudo systemctl status demo-cors
- Change the Build step in Jenkins job:
scp -Cq -o StrictHostKeyChecking=no "build/libs/demo-cors.jar" "<App-VM-ip>:/home/jenkins/demo-cors.jar" ssh jenkins@<App-VM-IP> -f 'sudo systemctl restart demo-cors;'
We are done.