Migrating an existing Jenkins server to a new server involves transferring all jobs, configurations, plugins, and settings. This blog outlines a practical, command-driven guide to achieve a complete migration while ensuring minimal downtime.
Pre-Migration Steps
1. Backup the Existing Jenkins Server
Before migration, back up all data from the current Jenkins server.
Locate the Jenkins Home Directory: Most installations store data in /var/lib/jenkins/. Verify this path:
echo $JENKINS_HOME
Stop the Jenkins Service: Ensure no jobs are running during the backup.
sudo systemctl stop jenkins
Create a Compressed Backup: Backup the Jenkins home directory, which contains jobs, plugins, and configurations.
tar -czvf jenkins_backup.tar.gz /var/lib/jenkins/
List Installed Plugins: Generate a list of installed plugins:
jenkins-plugin-cli --list > plugins_list.txt
Migration Steps
2. Set Up Jenkins on the New Server
Install Jenkins: Follow the official Jenkins installation guide for your OS:
sudo apt update
sudo apt install -y openjdk-11-jdk
wget -q -O - https://pkg.jenkins.io/debian-stable/jenkins.io.key | sudo tee /usr/share/keyrings/jenkins-key.gpg
echo deb [signed-by=/usr/share/keyrings/jenkins-key.gpg] https://pkg.jenkins.io/debian-stable binary/ | sudo tee /etc/apt/sources.list.d/jenkins.list > /dev/null
sudo apt update
sudo apt install -y jenkins
Stop the Jenkins Service: Prevent Jenkins from starting with the default settings.
sudo systemctl stop jenkins
Replace the Jenkins Home Directory: Transfer the backup from the old server to the new server.
scp jenkins_backup.tar.gz user@new-server:/tmp/
On the new server, extract and replace the Jenkins home directory:
sudo tar -xzvf /tmp/jenkins_backup.tar.gz -C /
sudo chown -R jenkins:jenkins /var/lib/jenkins/
3. Restore Plugins
If you’ve saved the plugin list, re-install all plugins on the new server:
jenkins-plugin-cli --plugins < plugins_list.txt
Alternatively, copy the plugin directory:
scp -r /var/lib/jenkins/plugins/ user@new-server:/var/lib/jenkins/plugins/
sudo chown -R jenkins:jenkins /var/lib/jenkins/plugins/
4. Start Jenkins on the New Server
Start Jenkins:
sudo systemctl start jenkins
Verify Jenkins is running:
sudo systemctl status jenkins
Access Jenkins from your browser:
http://<new-server-ip>:8080
Post-Migration Steps
5. Verify the Migration
- 5. Verify the Migration
- All jobs should appear on the Jenkins dashboard.
- Ensure job configurations (e.g., Git repositories, build commands, triggers) are correct.
- Check Jobs:
- All jobs should appear on the Jenkins dashboard.
- Ensure job configurations (e.g., Git repositories, build commands, triggers) are correct.
- Run Test Jobs: Trigger a few sample builds to confirm proper operation.
- Validate Plugins:
- Check if all plugins are listed under Manage Jenkins > Plugins.
- Update outdated plugins if required.
- Check Build History: Ensure the build history for all jobs is intact.
- Verify Credentials: Navigate to Manage Jenkins > Credentials and validate any SSH keys, tokens, or secrets.
6. Update Dependencies
- Update any dependencies in job configurations, such as paths or environment variables, to reflect changes on the new server.
- If you use agents, update their configurations to point to the new server:
- Update the Jenkins URL under Manage Jenkins > Configure System.
- Restart agents to reconnect.
Troubleshooting Tips
- Jenkins Won’t Start:
- Check permissions:
sudo chown -R jenkins:jenkins /var/lib/jenkins/
Review logs
tail -f /var/log/jenkins/jenkins.log
Missing Jobs or Configurations:
- Ensure the
$JENKINS_HOMEpath is correct. - Double-check the backup and restore process.
Plugin Compatibility Issues:
- Install the same Jenkins version as the old server, then upgrade after a successful migration.