Passing non-string parameters to Jenkins JJB builder templates - jenkins

I have a Jenkins JJB template, where I would like to customize triggers per job instance. This approach would have worked great if triggers was a string parameter, but it requires an object. How can I pass an object (in this case - an array) to the job template?
- job-template:
name: 'my-template-{proj}'
trigger_overrides: []
# The obj: syntax should have worked,
# but it doesn't handle the default from above
triggers: '{obj:trigger_overrides}'
- project:
name: my-project
jobs:
# for this job, customize triggers
- 'my-template':
proj: A
trigger_overrides:
- github
- timed: "#daily"
# this job should use the default triggers
- 'my-template':
proj: B
Note that in case triggers is always passed in, the empty triggers doesn't work. The global default overrides template parameter:
- defaults:
name: global
triggers:
- github
- job-template:
name: 'my-template-{proj}'
triggers: '{obj:trigger_overrides}'
- project:
name: my-project
jobs:
- 'my-template':
proj: A
trigger_overrides: []
Neither does this work (empty array is given as a default):
triggers: '{obj:trigger_overrides|[]}
P.S. This issue was also filed with jjb devs

You want to refer to how gerrit_release_trigger_file_paths is handled here in the releng/builder repository for passing a list:
https://github.com/opendaylight/releng-builder/blob/master/jjb/lf-infra/lf-infra-jobs.yaml#L120-L130
The original template is implemented in releng-global-jjb repository here: https://github.com/lfit/releng-global-jjb/blob/master/jjb/lf-docker-jobs.yaml#L229
JJB Best Practices:
https://docs.releng.linuxfoundation.org/projects/global-jjb/en/latest/best-practices.html
- project:
name: my-project
jobs:
- 'my-template':
proj: A
trigger_overrides:
- trigger_a
- trigger_b
- trigger_c

Related

Triggering different downstream jobs via templates in Jenkins Job Builder

I'm trying to trigger one or more downstream jobs using a job template. A summary of my definition:
- job-template:
name: something-{app}-build
project-type: freestyle
defaults: global
block-downstream: true
scm:
- git:
url: url
branches:
- 'master'
excluded-users:
- Jenkins
builders:
!include: templates/build-and-publish.yml
publishers:
- postbuildscript:
builders:
!include: templates/docker-build-and-push-to-ecr.yml
script-only-if-succeeded: True
mark-unstable-if-failed: True
- trigger-parameterized-builds:
- project: 'deploy-dev-ecs-service'
condition: SUCCESS
predefined-parameters: |
service={app}
envparams={envparams}
- project:
name: release-to-ecr
type: app
envparams: ''
app:
- app-1
- app-2:
- app-3:
envparams: 'FOO=42'
jobs:
- 'something-{app}-build'
Now this works, but I need to trigger different downstream jobs based on the app. This means triggering deploy-dev-ecs service multiple times with multiple parameters. For example:
app:
- app-1:
- project: deploy-dev-ecs-service
service: 'app-1'
envparams: 'foo=bar'
- app-2:
- project: deploy-dev-ecs-service
service: 'app-2.2'
envparams: 'x=2'
- project: deploy-dev-ecs-service
service: 'app-2.3'
envparams: 'x=3'
Essentially, I need to control which downstream job(s) get triggered based on
the project parameters. Is there a way to do this?

Jenkins Job Builder: Project Level Variables

Within JJB, you can define project-level variables like this:
- defaults:
name: global
git_url: "git#....."
- project
name: some-test
jobs:
- test-{name}
- job-template
name: test-{name}
scm:
- git:
url: "{git_url}"
branches:
- master
My question, must I hardcode the value of git_url at the default level or can I use some JJB mechanism to bring that in at job load/execution?
The reason I ask is that the yaml script that contains these JJB jobs can be used to define TEST, QA and PROD. It would be nice to just point at a properties file that contains the value for git_url and any other global variable values. I took a look at: http://docs.openstack.org/infra/jenkins-job-builder/definition.html?highlight=default#defaults and I did not see any mechanism.
If I understand your question correctly, there are two other approaches available within the context of a single yaml file
Approach 1: Set git_url at the project level
- project
name: some-test
git_url: "git#dogs.net:woof/bark.git"
jobs:
- test-{name}:
- job-template
name: test-{name}
scm:
- git:
url: "{git_url}"
branches:
- master
Here git_url is set at the project level. This approach allows you to define a second project with a different value for git_url, ie
- project
name: some-other-test
git_url: "git#cats.net:meow/meow.git"
jobs:
- test-{name}:
Approach 2: Set git_url at the job-template instance level
- project
name: some-test
jobs:
- test-{name}:
git_url: "git#....."
- job-template
name: test-{name}
scm:
- git:
url: "{git_url}"
branches:
- master
Here git_url is set on the actual instance of the job-template where it is specified. If your job-template had more than just {name} in its name, this would allow you to create multiple instances of it in the list of jobs at the project level, ie
- project
name: some-test
git_url: "git#....."
jobs:
- test-{name}-{type}:
type: 'cat'
- test-{name}-{type}:
type: 'dog'
- job-template
name: test-{name}-{type}
display-name: 'Test for {type} projects'
scm:
- git:
url: "{git_url}"
branches:
- master
Thoughts on TEST vs QA vs PROD
You also mentioned that you would like some kind of external properties file to differentiate between TEST, QA, and PROD environments. To address this let's consider four different files, project.yaml, defaults/TEST.yaml, defaults/QA.yaml, defaults/PROD.yaml whose contents are enumerated below.
project.yaml
- project
name: some-test
jobs:
- test-{name}:
defaults/TEST.yaml
- defaults:
name: global
git_url: "git#dogs.net:woof/test.git"
defaults/QA.yaml
- defaults:
name: global
git_url: "git#dogs.net:woof/qa.git"
defaults/PROD.yaml
- defaults:
name: global
git_url: "git#dogs.net:woof/prod.git"
Okay so these aren't great examples because you probably wouldn't have a different git repository for each environment, but I don't want to complicate things by straying too far from your original example.
With JJB you can specify more than one YAML file on the command line (I don't want to complicate the example or its explanation, but you can also specify directories full of JJB yaml). To differentiate between TEST, QA, and PROD deployments of your Jenkins job you can then do something like:
jenkins-jobs project.yaml:defaults/TEST.yaml
For your test environment.
jenkins-jobs project.yaml:defaults/QA.yaml
For your qa environment.
jenkins-jobs project.yaml:defaults/PROD.yaml
For your prod environment.
Hope that helps.

How to override an attribute for a specific job when working with Jenkins Job Builder (JJB)

We have got a Jenkins setup (at the moment not managed with the Jenkins Job Builder) where every web project has 5 very similar Jenkins jobs. This is a simplified Jenkins Job Builder yaml file which could work for our needs:
- defaults:
name: global
project-type: 'freestyle'
disabled: false
buildTargets: 'build'
- job-template:
name: '{jobNameFirstSegment}-{jobNameSecondSegment}'
defaults: global
builders:
- ant:
targets: '{buildTargets}'
buildfile: 'build.xml'
- project:
name: lorem
jobNameFirstSegment:
- foo:
displayNameFirstSegment: 'Foo'
urlGitRepository: 'git#bitbucket.org:mycompany/foo.git'
- bar:
displayNameFirstSegment: 'Bar'
urlGitRepository: 'git#bitbucket.org:mycompany/bar.git'
- baz:
displayNameFirstSegment: 'Baz'
urlGitRepository: 'git#bitbucket.org:mycompany/baz.git'
jobNameSecondSegment:
- lorem:
buildTargets: 'build-lorem'
- ipsum:
buildTargets: 'build-ipsum'
- dolor:
buildTargets: 'build-dolor'
- sit:
buildTargets: 'build-sit'
- amet:
buildTargets: 'build-amet'
jobs:
- '{jobNameFirstSegment}-{jobNameSecondSegment}'
This yaml file results in 15 Jenkins jobs - all are NOT disabled:
foo-lorem
foo-ipsum
foo-dolor
foo-sit
foo-amet
bar-lorem
bar-ipsum
bar-dolor
bar-sit
bar-amet
baz-lorem
baz-ipsum
baz-dolor
baz-sit
baz-amet
Is it possible with this kind of setup to disable a specific Jenkins job? I could not find out how to override the "disabled" attribute for e.g. the Jenkins job "foo-lorem". How can this be achieved?
Infact we would need to have the possibility to be able to override any attribute of any Jenkins job if needed. Is this doable? How?
In the job template, you want to add a disabled property which takes its value from a parameter. Parameters default value can be set at the defaults and project level. So in theory you could pass the disabled value at the job level:
- defaults:
name: global
disabled: false
- job-template:
name: 'build-{name}'
defaults: global
disabled: '{obj:disabled}'
- project:
name: myproject
jobs:
- 'build-{name}'
- project:
name: mydeadproject
jobs:
- 'build-{name}':
disabled: true
Note obj: prefix is to pass the boolean as is. Turns out there is an issue in JJB specific to the disabled parameter, so you need to use a different variable (I named it prevent_run below):
- defaults:
name: global
prevent_run: false
- job-template:
name: 'build-{name}'
defaults: global
disabled: '{obj:prevent_run}'
- project:
name: myproject
jobs:
- 'build-{name}'
- project:
name: mydeadproject
jobs:
- 'build-{name}':
prevent_run: true
In the global default, the variable prevent_run is set to false and is passed to projects and job templates. For the last job, a more specific value is passed to the mydeadproject. The snippet above generates two jobs:
build-mydeadproject disabled
build-myproject enabled
You can reuse the same trick in your example template:
--- reference.yaml 2015-09-08 09:27:56.000000000 +0200
+++ foo.yaml 2015-09-08 10:02:38.000000000 +0200
## -1,11 +1,13 ##
- defaults:
name: global
project-type: 'freestyle'
- disabled: false
+ prevent_run: false
buildTargets: 'build'
+
- job-template:
name: '{jobNameFirstSegment}-{jobNameSecondSegment}'
defaults: global
+ disabled: '{obj:prevent_run}'
builders:
- ant:
You can then pass prevent_run: true on a per job basis:
- project:
name: lorem
jobNameFirstSegment:
- foo:
displayNameFirstSegment: 'Foo'
urlGitRepository: 'git#bitbucket.org:mycompany/foo.git'
prevent_run: true
That disable all jobs having foo for the value of jobNameFirstSegment. I. e. it disables the jobs:
foo-amet
foo-dolor
foo-ipsum
foo-lorem
foo-sit
When a project is realized, a cartesian products of the variables is created to generate the jobs. There is no way here to pass a parameter to a specific couple of values.

Jenkins Job-Builder: How to correctly include job-templates from external file?

I am investigating using Jenkins Job-Builder (from OpenStack) as our means of managing jenkins job configurations. In doing so I am trying to figure out the right (best?) way to include a job-template from an external file using the !include custom tag.
In the current use case we will basically have one template that is going to be used by a LOT of job. Each job is going to need to exist in its own file for reason that are out of scope here.
So far I have gotten this to work:
job-template.yml
name: 'pre-build-{proj}-{repo}'
project-type: freestyle
... etc ...
job-1.yml
- job-template:
!include job-template.yml
- project:
name: job-1
proj: my-proj
repo: my-repo
jobs:
- 'build-config-{proj}-{repo}'
This seem wrong because the template definition gets split across both files and require needless duplication of the -job-template: line in every job file. I would like to get the following to work instead:
job-template.yml
- job-template:
name: 'pre-build-{proj}-{repo}'
project-type: freestyle
... etc ...
job-1.yml
!include job-template.yml
- project:
name: job-1
proj: my-proj
repo: my-repo
jobs:
- 'build-config-{proj}-{repo}'
The latter unfortunately results in a yaml parse error on the - project: line:
yaml.scanner.ScannerError: mapping values are not allowed here
in "job-1.yml", line 3, column 10
Is there way to get the entire template definition into the template file? This will become particularly annoying if ever we need to pull in multiple templates from multiple files.
Jenkins-jobs takes a path argument which can be a directory holding your files (job-template.yaml, job-1.yaml and job-2.yaml. It will assemble them as a single YAML document, so you do not need to use !include. So you can write:
job-template.yaml
- job-template:
name: 'pre-build-{proj}-{repo}'
builders:
- shell: 'echo building {proj} for {repo}'
job1.yaml
- project:
name: job-1
proj: my-proj
repo: my-repo
jobs:
- 'pre-build-{proj}-{repo}'
job2.yaml
- project:
name: job-2
proj: my-other-proj
repo: my-other-repo
jobs:
- 'pre-build-{proj}-{repo}'
That will generates two jobs with the following shell commands:
pre-build-my-other-proj-my-other-repo:
<command>echo building my-other-proj for my-other-repo</command>
pre-build-my-proj-my-repo:
<command>echo building my-proj for my-repo</command>
Assuming the files are in a directory config/ you can generate them all with:
jenkins-jobs test config/ -o /tmp/myjobs
Or use the name argument to filter the jobs that will be realized:
jenkins-jobs test config/ -o /tmp/myjobs '*my-proj*'
# Creates pre-build-my-proj-my-repo
# Skips pre-build-my-other-proj-my-other-repo

jenkins-job-builder doesn't propagate a variable value

I am using jenkins-job-builder to create my pipeline project. But I have a problem with the variables values when I am trying to reuse or propagating.
It is my project configuration:
- project:
name: myproject
git_url: git#gitlabserver.cu:demos-products/myproject.git
jobs:
- '{name}-nfr-smoke-tests':
pipeline-next: '{name}-nfr-smoke-tests'
And here is my job-template:
- job-template:
name: "{name}-nfr-smoke-tests"
node: 'slave1'
scm:
- git:
skip-tag: false
url: 'git#gitlabserver.cu:test-products/{name}-nfr-tests.git'
branches:
- master
wipe-workspace: true
builders:
- shell: |
bundle install
bundle exec cucumber features/smoke.feature
publishers:
- trigger:
project: "{pipeline-next}"
threshold: SUCCESS
Ok, now when I run this configuration in jenkins and I check the job's construction, it says:
No such project ‘{name}-nfr-smoke-tests’. Did you mean ‘myproject-nfr-smoke-tests’?
Why the line: pipeline-next: '{name}-nfr-smoke-tests'doesn't propagates the value of variable name and just used it as a literal string? I am missing something.
You are missing 'name' under 'project' section in your job-template. Append the following lines:
- project:
name: project-name
The purpose of a project is to collect related jobs together, and provide values for the variables in a Job Template.
I found out that Jenkins Job Builder version 0.9.0-0.2 does not propagate the value, but for me version 1.3.0+2015.12.15.git136.959eb4b909-1 did. Perhaps updating your version of Jenkins Job Builder might help?

Resources