Sunday, March 6, 2016

Using Jenkins-cli to migrate your Jenkins server

Last week I spent some time to setup a new jenkins server, I need to migrate from the old server to new server, including:
 -  install all the plugins,
 -  and import all the jenkins jobs from the old server to the new server.
I found jenkins-cli is so powerful and so easy to use, it is a big helper. Now I would like to share how I use this powerful tool.

Download jenkins-cli
 You can download the JAR file for the client from the URL "/jnlpJars/jenkins-cli.jar" on your Jenkins server, e.g. http://jenkins.example.com/jnlpJars/jenkins-cli.jar

List plugins
   java -jar jenkins-cli.jar -s http://your-jenkins-server/ list-plugins

Migrate the plugins
To install plugins which are installed in the old servers, use the following command:
      java -jar jenkins-cli.jar -s http://old-jenkins-server/ list-plugins | awk '{print $1}' | xargs java -jar jenkins-cli.jar -s http://new-jenkins-server/ install-plugin

Migrate the jenkins jobs
   java -jar jenkins-cli.jar -s http://old-jenkins-server/ get-job "my job" > my_job.xml
   java -jar jenkins-cli.jar -s http://new-jenkins-server/ create-job "my job" < my_job.xml