Jenkins Tricks – Password Recovery and Job Exports

I’m finally getting back to Jenkins, which I started waaaay back in November (here and here). Unfortunately, I kind of forgot my password. Well, that’s embarrassing! I also want to redo the manifest using maestrodev/rvm which means starting over, so I need to back things up. The manual for Jenkins and the results on Google can be overwhelming sometimes, so I thought I’d share what I learned to hopefully save someone else.

Password Recovery

There’s a few ways I found to recover your password. One suggestion is to disable all security, delete your user, re-enable security and allow signups, and then recreate the same user and things should just “work”. Part of the reason you have to do this is that once you disable security, you can’t change the password for your user; only the user can. That’s … frustrating.

Disable security by editing $JENKINS_HOME/config.xml, /var/lib/jenkins/config.xml on my instance. I was able to get away with disabling it by changing <useSecurity>true</useSecurity> to false, though the article suggests removing two other lines. Restart the service with systemctl restart jenkins or equivalent and now you’re able to get in and recreate some users.

I did find some instructions to recover an individual user’s password by editing /var/lib/jenkins/users/$USER/config.xml (/var/lib/jenkins/users/rnelson0/config.xml) to remove the passwordHash key and restart. I couldn’t get this to work, it just spewed a stacktrace at me no matter if I deleted the key entirely or just removed the value – then I stumbled on this github gist comment, of all things. Replace the value of the passwordHash with #jbcrypt:$2a$10$razd3L1aXndFfBNHO95aj.IVrFydsxkcQCcLmujmFQzll3hcUrY7S and the user’s password is now test. Re-enable security, if you have it disabled, log in with the temporary password, and change it. This worked perfectly well for me.

Job Exports

The next task is that I wanted to save the job I have so that I can import it after a rebuild, if I can’t recreate it perfectly. I found an answer on Stack Overflow. There’s a Jenkins cli tool you can use – a JAR you provide to java, specifically – to query Jenkins for a lot of information, including job details. Save the xml, shuffle it to another Jenkins instance or manipulate it in place, and then import it. It’s not shown in the SO answer, but you can query for a list of jobs and a ton of other commands, just leave off any arguments after the servername to get the list.

[rnelson0@jenkins ~]$ java -jar /usr/lib/jenkins/jenkins-cli.jar -s http://localhost:8080 list-jobs
test
[rnelson0@jenkins ~]$ java -jar /usr/lib/jenkins/jenkins-cli.jar -s http://localhost:8080 get-job test > text.xml
[rnelson0@jenkins ~]$ java -jar /usr/lib/jenkins/jenkins-cli.jar -s http://localhost:8080 create-job newjob < text.xml
[rnelson0@jenkins ~]$ java -jar /usr/lib/jenkins/jenkins-cli.jar -s http://localhost:8080 list-jobs
test
newjob

I’m going to try and rebuild the server and recreate my job, then I’ll be back to document that process before looking at Jenkins pipelines.

4 thoughts on “Jenkins Tricks – Password Recovery and Job Exports

    • That is on my list! I’ve got some non-puppet things that will probably use jobs, and doing a conversion post may help people with existing jobs, so figured I can try to kill 3 birds with 1 stone

  1. Pingback: Automating Puppet tests with a Jenkins Job, version 1.0 | rnelson0
  2. Pingback: Automating Puppet tests with a Jenkins Job, version 1.0 | rnelson0

Leave a comment