问题
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.
回答1:
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.
来源:https://stackoverflow.com/questions/42956598/how-to-set-job-properties-for-jobs-within-a-jenkins-multi-branch-pipeline-projec