How to setup BuildBlockerProperty on Jenkins MultiBranch pipeline - jenkins

I'm trying to prevent the run of branch_A while branch_B is running.
So I succeeded with normal pipeline with BuildBlockerProperty , job_A stay in pending while job_B job is running.
But with multibranch pipeline, it's not working.
Example for BuildBlockerProperty property for branch_A :
properties([
[
$class : 'BuildBlockerProperty',
blockLevel : 'GLOBAL',
blockingJobs : 'branch_B',
scanQueueFor : 'ALL',
useBuildBlocker: true
],
])
I saw on jenkins multibranch doc that is it possible but I don't find a way to do that. So if someone have the solution pls !

I found the solution. When you write the branch name, you should write also the multibranch job name.
Exemple :
properties([
[
$class : 'BuildBlockerProperty',
blockLevel : 'GLOBAL',
blockingJobs : 'multibranch_job_name/branch_B',
scanQueueFor : 'ALL',
useBuildBlocker: true
],
])
But I didn't find a way if you want to run the job only if no other jobs are running. If someone have an idea.

Related

How to trigger a build on commit to branch in scripted pipeline

triggers { pollSCM('H */4 * * 1-5') } will this work for merge to branch
I see we have 3 options for triggers this seems to be fo declarative
corn
pollScm
upstream
where in for scripted pipeline is that something like this
properties([pipelineTrigger([triggers('gitPush')])])
OR
properties([pipelineTriggers([githubPush()])])// With this should I also enable a option on Jenkins instance
You can also use Generic Webhook Trigger plugin.
You will need to create a webhook in github and in Jenkins Pipeline something like below.
triggers {
GenericTrigger( genericVariables: [
[defaultValue: '', key: 'commit_author', regexpFilter: '', value: '$.pusher.name'],
[defaultValue: '', key: 'ref', regexpFilter: '', value: '$.ref']
],
causeString: '$commit_author committed to $ref',
printContributedVariables: false,
printPostContent: false,
regexpFilterText: '$ref',
regexpFilterExpression: 'refs/heads/develop',
token: '12345'
}
Hope this helps.
Use something like this in you Jenkinsfile. Use Only those option which you need. Remove which you don't want.
pipeline {
agent any
triggers {
github(
triggerOnPush: true,
triggerOnMergeRequest: true,
triggerOpenMergeRequestOnPush: "source",
branchFilterType: "All",
triggerOnNoteRequest: false)
}
stages {
...
}
}
NOTE: Make sure you have done all the webhook configuration bewteen github and jenkins. and installed webhook plaugin on jenkins

How do I dynamically load a Jenkins pipeline library from Perforce? [duplicate]

In continuation to jenkins-pipeline-syntax-for-p4sync - I am not able to get the "Poll SCM" option work for my pipeline job.
Here is my configuration:
"Poll SCM" is checked and set to poll every 10 minutes
Pipeline script contains the following:
node ('some-node') // not actual value
{
stage ('checkout')
{
checkout([
$class: 'PerforceScm',
credential: '11111111-1111-1111-1111-11111111111', // not actual value
populate: [
$class: 'AutoCleanImpl',
delete: true,
modtime: false,
parallel: [
enable: false,
minbytes: '1024',
minfiles: '1',
path: '/usr/local/bin/p4',
threads: '4'
],
pin: '',
quiet: true,
replace: true
],
workspace: [
$class: 'ManualWorkspaceImpl',
charset: 'none',
name: 'jenkins-${NODE_NAME}-${JOB_NAME}',
pinHost: false,
spec: [
allwrite: false,
clobber: false,
compress: false,
line: 'LOCAL',
locked: false,
modtime: false,
rmdir: false,
streamName: '',
view: '//Depot/subfolder... //jenkins-${NODE_NAME}-${JOB_NAME}/...' // not actual value
]
]
]
)
}
stage ('now do something')
{
sh 'ls -la'
}
}
Ran the job manually once
Still, polling does not work and job does not have a "Perforce Software Polling Log" link like a non-pipelined job has when configuring the perforce source and Poll SCM in the GUI.
It's like the PerforceSCM is missing a poll: true setting - or i'm doing something wrong.
Currently I have a workaround in which I poll perforce in a non-pipelined job which triggers a pipelined job, but then I have to pass the changelists manually and I would rather the pipeline job to do everything.
edit: versions
jenkins - 2.7.4
P4 plugin - 1.4.8
Pipeline plugin - 2.4
Pipeline SCM Step plugin - 2.2
If you go to the Groovy snippet generator and check the "include in polling" checkbox, you'll see that the generated code includes a line item for it:
checkout([
poll: true,
As an aside, you may run into problems at the moment using ${NODE_NAME} in your workspace name. The polling runs on the master, so it might not properly find the change number of your previous build. If that's the case, I know a fix for it should be coming shortly.
After updating all the plugins to latest (as of this post date) and restarting the jenkins server - the polling appears to be working with the exact same configuration (job now has the poll log link).
I'm not sure what exactly resolved the issue - but I consider it resolved.

How can I promote an artifact from jenkins?

We have artifactory pro and We need to know if We can promote an artifact from Jenkins through the plugin. Currently, We are deploying on artifactory and when We need promote first, We download the artifact from artifactory to jenkins and then We publish it on artifactory with other tag.
Exists some way to promote from Jenkin’s pipeline without API, because we need a solution that not expose our credentials.
Thanks,
You can promote a build inside a jenkins script by using artifactory.promote instead of Artifactory.addInteractivePromotion as shown:
#NonCPS
def promote(artifactory, buildInfo) {
echo "currentBuild.result is ${currentBuild.result}"
def QAPromotionConfig = [
'buildName' : buildInfo.name,
'buildNumber' : buildInfo.number,
'targetRepo' : 'debian-local-qa',
'sourceRepo' : 'debian-local-debug',
'copy' : true
]
def RelPromotionConfig = [
'buildName' : buildInfo.name,
'buildNumber' : buildInfo.number,
'targetRepo' : 'debian-local-release',
'sourceRepo' : 'debian-local-debug',
'copy' : true
]
if(currentBuild.result == "SUCCESS") {
artifactory.promote(QAPromotionConfig)
} else {
Artifactory.addInteractivePromotion(server: artifactory, promotionConfig: QAPromotionConfig, displayName: "Promote to QA")
}
Artifactory.addInteractivePromotion(server: artifactory, promotionConfig: RelPromotionConfig, displayName: "Promote to Release")
}

How i can configure jobs created by Jenkins multi-branch Pipeline using declarative syntax?

For example, I want to configure Notification plugin for use in multi-branch Pipeline using declarative syntax.
There's snippet generator for that purpose providing me with code like:
properties([
[
$class:'HudsonNotificationProperty',
endpoints:[
[
urlInfo:[
urlOrId:'https://example.com/smth',
urlType:'PUBLIC'
]
]
]
],
pipelineTriggers( [
] )
])
I believe it's written in Scripted Pipeline syntax. How can I integrate this into my project using Declarative Pipeline syntax?
Paste your properties snippet before declare the pipeline:
#!/usr/bin/env groovy
properties([[$class: 'HudsonNotificationProperty',
endpoints: [[urlInfo: [urlOrId: 'https://foo.bar.com', urlType: 'PUBLIC']]]]]
)
pipeline {
...
}

how to configure 2 jenkins pipeline trigger with environment variable

I have to pass parameter to a timeTrigger function in jenkins pipelineTrigger, but it's not taking that parameter. I have to schedule 2 builds, one for Dev every 8 hours, other for staging every 12 hours.
Question1: Can I trigger 2 builds this way?
Question2: How to pass parameter to specify environment?
See Code below
environmentParam = ['dev', 'qa', 'stg']
originParam = ['Automatic', 'Manual']
if (env.BRANCH_NAME == "master") {
properties([
[$class : 'ParametersDefinitionProperty',
parameterDefinitions:
[
[$class : 'ChoiceParameterDefinition',
choices : environmentParam.join('\n'),
description: 'Environment to run the Integration tests',
name : 'environment'
],
[$class : 'ChoiceParameterDefinition',
choices : originParam.join('\n'),
description: 'The execution of this job was Automatic or Manual',
name : 'origin'
]
]
],
pipelineTriggers(
[
[
$class: 'TimerTrigger', spec: 'H */12 * * *', environment: 'stg'
],
[
$class: 'TimerTrigger', spec: 'H */8 * * *', environment: 'dev'
]
]
),
disableConcurrentBuilds()
])
}
above code is not taking up environment, it is only triggering second entry in pipelineTriggers, not both. :-(
Answer for Questions 1: No, you cannot do it now.
The properties will be set to the jenkins job (config.xml) when pipeline script is executed. Therefore if multiple time trigger is not supported in normal jenkins job, your script will no work as expected as well.
Please execute it and check the jenkins job configuraion and config.xml to understand the result.
Suggestion
Change your solution in another logic (if it is still 8 & 12 hours)
trigger your job to run every 4 hours
keep both choice parameters.
try to have some logical check in steps to run the different stage in different time.
i think you'll have a better time if you create either two or three build plans instead of one.
if you created two, there would be one for dev and one for staging, and they would each have their own trigger and do the appropriate deployment.
if you had three, there would be one (let's call it the "deployment" build plan) that did deployments and that took an TARGET_ENVIRONMENT parameter or similar (with value dev or stg). The other two would have the triggers and they would each call the deployment build plan to do the actual work. you can call another build plan from a Jenkinsfile like this:
build job: 'deployment', parameters: [string(name: 'TARGET_ENVIRONMENT', value: 'dev')]

Resources