6
votes

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.

1
Look into the following: stackoverflow.com/questions/16963309/… Can it help you get what you need?Eldad Assis
I haven't found the time to look closer into that. In the meantime I stepped back from the idea of implementing my build-job graph with jenkins jobs and minimized the work that is done in the jenkins script. I have a CMake base C++ project and I went over to implement the 'jobs' as CMake targets. That has the advantage that most parts of the pipeline can be run locally and the cmake scripts already have a lot of the information (file locations) that is needed to set up the job graph. The jenkins script now only builds the pipeline target on a linux slave and a windows slave.Knitschi
A disadvantage of the cmake approach is that it has no visualisation about which targets/jobs have been succesfully been worked on, and which failed. This information must be 'eye parsed' from the command line output.Knitschi

1 Answers

1
votes

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.