Currently on a server, an application is being deployed. Structure of the project on the server:
- start-switch.json
- application_snapshot-001.jar
- appconfig.properties
- workflow.yaml
- other folders
Every time there is a change, we have to manually stop the server (pm2 stop APP_NAME), delete/replace the jar file and restart server (pm2 start start-switch.JSON) which is really cumbersome.
I want to automate this process using Jenkins to stop the server, replace the jar file which is pulled from GitHub repository and restart the server.
This will also help to keep the version of each deployment on the server.
How can I do these steps on the Jenkins file?
Thanks in advance.
CodePudding user response:
This is a fairly normal implementation you can do with Jenkins. First, switch to your project directory and use sh
to stop the server. Then use the GitSCM plugin to pull your jar. Delete the old jar, paste in the new one, and use sh
again to start the server. Something like this:
dir("path/to/proj_dir"){
sh "pm2 stop APP_NAME"
sh "rm application_snapshot-001.jar"
checkout scmGit(
branches: [[name: 'master']],
userRemoteConfigs: [[url: '<repo>']])
sh "mv path/to/new/jar path/to/old/jar application_snapshot-001.jar"
sh "pm2 start start-switch.JSON"
}
If you are using a Windows machine then you will have to use bat
instead of sh
.