Migrating Jenkins jobs to new instance
You might know Why I don't like Jenkins but I still have to use it in my job. Recently I was need to migrate Jenkins jobs to new instance and it was easier than I expected.
Prerequisites
I had two Jenkins instances on different OpenShift clusters. Both instances have mounted persistent
volumes where the content of /var/lib/jenkins
directories is stored. I got a task to migrate all
jobs, configs and secrets to the new instance. I've never done that before so I went the dumbest
way I could figure out.
Jobs migration
Jenkins jobs are stored in /var/lib/jenkins/job
so I decided just to copy it to some intermediate
storage. We use regular OpenShift Jenkins deployment so rsync
was installed into the image. In
order to copy files between an OpenShift pod and the local machine oc rsync
command should be
used. So here is my step by step guide:
- Use a machine with enough free space and fast connection.
- Login to first OpenShift cluster:
sh
oc login --token=some_token --server=ocp_api_url
-
Switch the namespace and copy
/var/lib/jenkins/jobs
:sh oc project your_jenkins_project oc rsync jenkins-pod-name:/var/lib/jenkins/jobs/ /some/local/directory
-
Login to another OpenShift cluster.
-
Switch namespace and copy
/some/local/directory
to the jenkins pod:sh oc project your_jenkins_project oc rsync /some/local/directory jenkins-pod-name:/var/lib/jenkins/jobs/
-
Restart Jenkins on the second cluster.
An that's it. You even don't need to stop first Jenkins instance. Actually if it was need to do it would make the process more complicated. You can access data stored in PVCs only via pods that mount them somewhere in the filesystem.
Secrets migration
All of our jobs use Jenkins secrets engine. Without secrets from the first instance jobs wouldn't work. Fortunately, secrets migration was just about copying files. I found a helpful guide on itsecureadmin.com:
-
Remove the
identity.key.enc
file on second instance:sh rm /var/lib/jenkins/identity.key.enc
-
Using
oc rsync
replacesecret*
credentials.xml
files from first Jenkins instance to second one. - Restart Jenkins on the second cluster.
- ...
- PROFIT!