Home > other >  Jenkins to automate process on Server
Jenkins to automate process on Server

Time:01-26

Currently on a server, an application is being deployed. Structure of the project on the server:

  1. start-switch.json
  2. application_snapshot-001.jar
  3. appconfig.properties
  4. workflow.yaml
  5. 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.

  • Related