Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set job properties for jobs within a Jenkins multi-branch pipeline project?

Does anyone know the correct method for setting job properties, specifically build triggers, from within a Jenkinsfile? (declarative pipeline script, in a multi-branch pipeline job).

For clarity I need to set specific build triggers for underlying jobs in a multibranch project. The triggers for the overarching multibranch project I can configure in the GUI.

Have tried methods listed here: Jenkins multi-branch pipeline and specifying upstream projects

Jenkins: Trigger Multi-branch pipeline on upstream change

How do I use Jenkins Pipeline properties step?

I get errors saying that since v0.8 I should be using the options step instead: https://jenkins.io/doc/book/pipeline/syntax/#declarative-pipeline

But I can't see any steps listed there that allow be to set build triggers within the options directive.

There is a config.xml in each of the branched job folders on the server, but I think this will be overwritten when I run the job again, as they sit under the multi branch job.

There is also an option to pass different properties into different branches (make exceptions for branches) but the only option I see there is to suppress SCM commits.


My overall aim with this is to try and make a single Jenkinsfile that dynamically allows all the underlying jobs in the multibranch project to be triggered by their dependent upstream builds.

Step 1: Work out how to set properties at all :)

Step 2: Populate each build dynamically with the upstream dependency properties, meaning they get kicked off when certain builds complete.

Question concerns step 1 only, step 2 is just where I'm trying to get to.

like image 670
Andrew McIntyre Avatar asked Mar 22 '17 15:03

Andrew McIntyre


People also ask

Which of the below could be the use case of multi branch pipeline?

Multibranch Pipelines can be used for validating pull/change requests with the appropriate plugin. This functionality is provided by the following plugins: GitHub Branch Source. Bitbucket Branch Source.

Which enables you to implement different Jenkinsfiles for different branches of the same project?

A multibranch pipeline is a pipeline that has multiple branches. The main advantage of using a multibranch pipeline is to build and deploy multiple branches from a single repository. Having a multibranch pipeline also allows you to have different environments for different branches.

What is multi branch job in Jenkins?

What's a Jenkins Multibranch Pipeline? A multibranch job is simply a folder of pipeline jobs. For every branch you have, Jenkins will create a folder. So instead of creating a pipeline job for each of the branches you have in a git repository, you could use a multibranch job.


1 Answers

Step 1: There are lot of properties that you could define. The ones that you are specifically looking for are listed below:

options{timestamps()}  --> Adds timestamp to console output
triggers{pollSCM('H/15 * * * *')} --> Polling SCM 
triggers{cron('H/15 * * * *')} --> Trigger build every 15 minutes. Similarly you can set the build trigger to any specific time to build it periodically.

Moreover, you can find all the properties that could be defined using the properties options in 'Pipeline Syntax' that is made available in every job. Please naviagte to PIpeline syntax(in any of the jobs) --> select proeprties: set job proerpties.

A sample declarative pipeline could be as follows:

#!groovy
pipeline{
agent any
options{timestamps()}
triggers{pollSCM('H/15 * * * *')}
parameters{
 ..........
}
environment{
............
}
stages{
stage{
steps{
..............
}
}
}
post{
always{
build job: '/foldername/job1', parameters: [string(name: 'parameter1', value: "${params.parameter1}")] , propagate: false
}
}
}

step 2: You can trigger another project from within the Jenkins file using the 'build' command. Refer the post section above to trigger the same with parameters.

Kindly let me know if you would require further information.

like image 187
Karthick Avatar answered Oct 22 '22 00:10

Karthick