0

I was searching for a way to copy jenkins job with builds using API.

I tried few Python Wrappers:

  from api4jenkins import Jenkins
  j = Jenkins('http://127.0.0.1:8080/', auth=('admin', 'admin'))
  j.copy_job('freestylejob', 'newjob')

but the copy methods in all of them copies the job without the builds.

Parth Jeet
  • 129
  • 2
  • 15

1 Answers1

0

Why does it have to use the API? Just copy the folder from the "Jenkins\Jobs" folder and rename it, restart your Jenkins instance and the new job will be there with all children in tact.

Justin Nimmo
  • 125
  • 12
  • API because I've to copy of more than 200 pipeline jobs and I need to save the builds because I'm versioning my artifacts using the build number. – Parth Jeet Nov 24 '20 at 19:19
  • Yup, done that here too. Should be able to be scripted, the only thing you need to save in the folder structure is the config.xml files. The "builds" folder and everything under it can be deleted and the build number is kept in the "nextBuildNumber" file. It will be automatically recreated if not found so you can go ahead and just kill it. https://stackoverflow.com/questions/23767489/how-can-one-delete-files-in-a-folder-matching-a-regular-expression-using-powersh/23768332 – Justin Nimmo Nov 24 '20 at 20:07
  • Oh you said save the builds, that will all be maintained if you copy the folder as it's all maintained in either the directory structure or the nextbuidlnumber file. Try copying just one and restart the service. You should see an exact duplicate with builds etc. – Justin Nimmo Nov 24 '20 at 20:11
  • Nope, doesn't work. I tried with API as well as CLI. After copying, I get a brand new pipeline job with same configuration but 0 builds. – Parth Jeet Nov 25 '20 at 06:07
  • I just tried myself and was able to do it and it maintained the builds and numbers. Are you running on a Windows environment? – Justin Nimmo Nov 25 '20 at 11:49