Closed tsboris closed 3 years ago
This is a multibranch Pipeline
The readme contains:
...
regexpFilterText: '$ref',
regexpFilterExpression: 'refs/heads/' + BRANCH_NAME
...
Which makes the job trigger a build when $ref
resolved to the branch corresponding to the job created by Multibranch.
Perhaps this will work for you:
pipeline {
agent { ecs { ... } }
triggers {
GenericTrigger(
causeString: 'devops_test_repo Trigger',
genericVariables: [
[key: 'TRIGGER_BRANCH', value: '$.push.changes[0].new.name'],
],
printContributedVariables: true,
printPostContent: false,
regexpFilterText: '$TRIGGER_BRANCH $TRIGGER_BRANCH',
regexpFilterExpression: '^' + BRANCH_NAME + ' master$|develop$',
token: 'devops_test_repo',
)
}
stages {
stage('TEST') {
steps {
echo "TEST"
}
}
}
}
Hi Tomas, Your suggestion did not resolve the issue :-(
For the purpose of further investigation, I've reduced the BitBucket Cloud repository to just two branches: master
and develop-test-12
After several more testing using a very simple filter, my conclusions are as follows.
triggers {
GenericTrigger(
causeString: 'devops_test_repo Trigger',
genericVariables: [
[key: 'TRIGGER_BRANCH', value: '$.push.changes[0].new.name'],
],
printContributedVariables: true,
printPostContent: false,
regexpFilterText: '$TRIGGER_BRANCH',
regexpFilterExpression: 'master',
token: 'devops_test_repo',
)
}
Based on the above basic and simple setup, only branch master should trigger a Jenkins build and the build should only run in master branch pipeline
develop-test-12
, no jobs are triggered in Jenkins. This is the desired behavior!master
, all jobs are triggered in Jenkins. Both the master
and develop-test-12
pipelines are triggered and build. This is NOT the desired behavior! develop-test-12
{
"jobs": {
"test_job_Pipeline/master": {
"regexpFilterExpression": "master",
"triggered": false,
"resolvedVariables": {
"TRIGGER_BRANCH": "develop-test-12"
},
"regexpFilterText": "develop-test-12",
"id": 0,
"url": ""
},
"test_job_Pipeline/develop-test-12": {
"regexpFilterExpression": "master",
"triggered": false,
"resolvedVariables": {
"TRIGGER_BRANCH": "develop-test-12"
},
"regexpFilterText": "develop-test-12",
"id": 0,
"url": ""
}
},
"message": "Triggered jobs."
}
Please notice that when the trigger is develop-test-12
branch, the value of "triggered": false
is set as false
for both test_job_Pipeline/master
and test_job_Pipeline/develop-test-12
.
This is expected because the trigger is NOT master
, so all branches should be set as "triggered": false
, "id": 0
and "url": ""
master
{
"jobs": {
"test_job_Pipeline/master": {
"regexpFilterExpression": "master",
"triggered": true,
"resolvedVariables": {
"TRIGGER_BRANCH": "master"
},
"regexpFilterText": "master",
"id": 1818,
"url": "queue/item/1818/"
},
"test_job_Pipeline/develop-test-12": {
"regexpFilterExpression": "master",
"triggered": true,
"resolvedVariables": {
"TRIGGER_BRANCH": "master"
},
"regexpFilterText": "master",
"id": 1817,
"url": "queue/item/1817/"
}
},
"message": "Triggered jobs."
}
Please notice that when the trigger is master
branch, the value of "triggered": true
is set as true
for both test_job_Pipeline/master
and test_job_Pipeline/develop-test-12
.
I would expect it to be:
"triggered": true
, "id": 1818
and "url": "queue/item/1818/"
for test_job_Pipeline/master
and "triggered": false
, "id": 0
and "url": ""
for test_job_Pipeline/develop-test-12
This is expected with that filter. And the fix is what I suggested in previous response:
...
regexpFilterText: '$TRIGGER_BRANCH $TRIGGER_BRANCH',
regexpFilterExpression: '^' + BRANCH_NAME + ' master$|develop$',
...
Because the multibranch
will create 2 jobs. The first part '^' + BRANCH_NAME
will make sure these jobs only trigger on changes to the branch they were created for. The second part + ' master$|develop$'
makes sure it only triggers for the branch that was changed.
You can view the applied configuration of the jobs created by multibranch
and you will see that BRANCH_NAME
is resolved upon creation of the jobs.
The master
job will have expression '^master master$|develop$'
.
The develop
job will have expression '^develop master$|develop$'
.
Hi Tomas, Your fix is working. Thanks!
how to pass the branch to branch field to clone
Plugin version used: 1.72 Jenkins version used: 2.249.1 Git Repo: BitBucket Cloud
Git Structure
BitBucket Cloud repository with four branches:
Configuration
Build job log
Below, there are two build job logs.
The build logs are for when I triggered the
master
branch.The first is for branch
develop-test
.The second is for branch
master
.Build job log for branch
develop-test
Build job log for branch
master
Response from Jenkins to BitBucket
Below, there are two responses.
The first is when triggering branch
develop-test
.The second is when triggering branch
master
.Response when triggering branch
develop-test
Response when triggering branch
master
Expected result and actual result
Case 1: Pushing a commit into branch
develop-test
. Expected: Jenkins shouldn't run any build jobs at all. Actual: Jenkins is not running any build jobsCase 2: Pushing a commit into branch
master
. Expected: Jenkins should run a single build job only for branchmaster
. Actual: Jenkins is running build jobs for each branch, even those that are not themaster
branch (that’s four simultaneous job runs).