Jenkins – create new jobs in a Jenkins pipeline script

Jenkinsjenkins-pipeline

I am wondering if it is possible to create new Jenkins build-jobs from within the pipeline groovy script. I know that it is possible to start existing jobs, but I would also like to create new jobs in the script and then start them.

This would allow me to create a "job-graph" in my script. With job-graph I mean a collection of build-jobs that may rely on each others results and the graph determines which jobs can be run in parallel. Something like

         /- WindowsBuild---------- WindowsRunTests -------------\
        /                                                        \
JobRoot --- LinuxBuild------------ LinuxRunTests ----------------/-- AllDone     
       \                       \                                / 
        \                       \- LinuxRunDynamicAnalysis ----/
         \- StaticCodeAnalysis -------------------------------/

Before I switched to the Pipeline job I did this by manually creating the jobs and setting their dependencies. Now I would like to create the necessary jobs in the script.

With the current model for parallalism in the jenkins pipeline I have a build-stage, a test-stage, etc but the stages slow down the overall execution because for example the WindowsRunTests-step will not be started before all build-steps on all platforms have finished although it only needs the results from the WindowsBuild-step.

It may also improve visualizing the pipeline and separating the console ouput which gets mixed together with the parallel() command.

Best Answer

I don't know exactly if it can help you but you can do it with a workaround. You can run a python script and within that script you can create new jobs by copying an existing job template:

jen_conn = connect_to_jenkins(jenkins_url, jenkins_user, jenkins_password)

    jen_conn.copy_job('copy-job-dev', jenkins_job_name)

With the function as below:

def connect_to_jenkins(jenkins_url, jenkins_user, jenkins_password):
server = jenkins.Jenkins(jenkins_url, username=jenkins_user, password=jenkins_password)
try:
    user = server.get_whoami()
except BaseException as error:
    print error
    print "Could not connect to Jenkins."
    exit()
return server

You would have to use the jenkins plugin for python.

Related Topic