Jenkins job as code

Automatic trigger of Generated Jobs created from Jenkins SeedJob

Sourav Atta
4 min readMay 18, 2021

In the post, Create Jenkins seedjob using Job DSL Script we learned about how to create a SeedJob and it generates a pipeline job that prints a greeting message based on the input name.

But, we have seen that we need to trigger the Generated Pipeline Job manually. It would be great if the generated job will get automatically triggered. In this post, we are going to cover this particular use case.

Getting Started

We are going to extend the same DSL script from the post, Create Jenkins seedjob using Job DSL Script. We are going to use a Post-build Actions i.e. Trigger parametrized build on other projects, which is going to trigger a job based on the build parameters. This will then automatically trigger the generated job. We will create one more input String Parameter JOB_NAME which will be the job name of the generated pipeline job.

Follow the below steps to automatic trigger the generated job:

Step 1: Modify the DSL Script and SeedJob

  • In the Jenkins Dashboard, select the job seedJob and click on Configure in the left-hand side panel.
  • In the follow-up page, under General tab, check the checkbox This project is parameterised.
  • Click on Add Parameter. We will add two input string parameter JOB_NAME (name of the generated job) and NAME (name of the person).
  • Scroll down or click on the Build tab. Now, we will modify the DSL script to resolve or leverage the above two string parameters so that we can access them in the generated pipeline job.
    The modified DSL script is:
// import the package
import hudson.model.*

// get current thread Executor
def thr = Thread.currentThread()
// get current build
def build = thr?.executable
// resolve the parameter JOB_NAME
def job_param = "JOB_NAME"
def resolver = build.buildVariableResolver
def job_name = resolver.resolve(job_param)
// resolve the parameter NAME
def hardcoded_param = "NAME"
def resolver1 = build.buildVariableResolver
def person_name = resolver1.resolve(hardcoded_param)
pipelineJob("$job_name") {

parameters {
stringParam('name', "${person_name}", 'name of the person')
}
definition {
cps {
script('''
pipeline {
agent any
stages {
stage('Greet') {
steps {
echo "Hello!! ${name}"
}
}
}
}
'''.stripIndent())
sandbox()
}
}
}

Paste the above DSL Script in the text area of Use the provided DSL script

  • Scroll down or click on the Post-build Actions. Click on Add post-build action and select Trigger parametrized build on other projects.
  • Type $JOB_NAME in Projects to build text area. Click on Add parameters and select Current build parameters.
  • Click on Apply and Save.

Step 2: Running the SeedJob

Let’s run the seedjob that will generate a pipeline job with the input job name and the generated pipeline will get automatically triggered.

  • On the Jenkins Dashboard, select the job seedJob and then click on Build with Parameters button on the left side.
  • In the follow-up page, type in the name of generated job and name of the person in JOB_NAME and NAME text area respectively. Then, click on Build.
  • You can see the job runs successfully and on refreshing the page, under the list of Generated Items, we can see the name of the generated job i.e. greetJohn
  • Click on the greetJohn under the Generated items, you’ll be redirected to the greetJohn job page. We can verify that the pipeline job has run automatically and successfully, and it should print Hello!! John
  • A build has been listed under the Build history. Hover over the #1 build, click on the drop-down arrow, and select Console Output.

You can now view the logs and console output of the build. You can verify the output of the echo command.

Conclusion

In this tutorial, we have created a seedjob that will generate a pipeline job and trigger it automatically. Try the above example or create a more complex Pipeline or Freestyle Job.

Let me know in the comments if you are facing any problem with the above example or need help with the DSL script.

--

--

Sourav Atta
Sourav Atta

Written by Sourav Atta

DevOps Engineer | Tool Agnostic | Very much into learning old-school IT concepts | Strategically lazy

No responses yet