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.
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.
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:
oc login --token=some_token --server=ocp_api_url
Switch the namespace and copy
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/directoryto 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.
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:
identity.key.encfile on second instance:
sh rm /var/lib/jenkins/identity.key.enc
credentials.xmlfiles from first Jenkins instance to second one.
- Restart Jenkins on the second cluster.