How to copy existing 'template' job to create new build job

72 views
Skip to first unread message

Mike Craig

unread,
Jul 15, 2014, 2:43:20 PM7/15/14
to jenkins...@googlegroups.com
Hello,
 
I have previously used the Jenkins CLI to create a new job from a template. However, (known issue) I am unable to copy Promoted Builds steps as part of this. So I have gone down the path of trying to robocopy the template job to a new job name, and search/replace values within. For some reason Jenkins is not picking up the new job.
 
Here are the basic steps my script is executing
  1. Robocopy "\\$JenkinsServer\jobs\$TemplateJob" "\\$JenkinsServer\jobs\$NewBuildJobName" *.* /E /XD .git /XF *.log
  2. Then using Powershell: replace "##RELEASEVERSION##" flag with my new release version (ie. v17)
  3. Then using Powershell: replace "<disabled>true</disabled>" with "<disabled>false</disabled>"
  4. Then write the xml file back out to "\\$JenkinsServer\jobs\$NewBuildJobName\config.xml"
  5. I then look "\\$JenkinsServer\jobs\$jobname\promotions" and iterate through each promotion level
  6. I replace "<project>$templateJobName</project>" with "<project>$jobname</project>"
  7. I then try to use the CLI to reload-configuration (which doesn't work for me, so I do it manually in the UI)
    1. Doesn't work: java -jar ..\bin\jenkins-cli.jar -s "http://$JenkinsServer/" reload-configuration  --username $UserName --password $Password
 
Once I reload configuration, Jenkins does not see and pick up the new build. I have not been able to find any other config files where build jobs are stored/configuration apart from the jobs folder.
 
One thing I have noticed through a folder compare with BeyondCompare, is that the new files are Unicode, and not UTF-8, could have be causing my Jenkins issues?
 
For fun, I have attached my script, if you have any feedback/suggestions I'm all ears.
 
Assumptions:
A couple functions in the script take a parameter $JenkinsServer, which defaults to localhost, you will need to update as per your environment.
Set these values as per your needs
$ReleaseVersion = "v1.0"
$ProjectToBuild = "MyNewProject"
You have a 'template' job created in the naming format of "template-JobName"
You run Powershell as an Admin
 
Thank you.
 
Mike
 
Functions_JenkinsAutomation2.ps1

Mike Craig

unread,
Jul 16, 2014, 3:44:47 PM7/16/14
to jenkins...@googlegroups.com
For anyone interested, I followed my intuition and change the command that writes the config file back out to force UTF8 encoding. This fixed it. 
The updated file is attached.
Functions_JenkinsAutomation.ps1
Reply all
Reply to author
Forward
0 new messages