Reputation: 305
I would like to build my jenkins pipeline when I create a tag in master branch in bitbucket. I have jenkins building on commit of master branch, but I can not seem to get it to build for creation of a tag.
This is what I have so far:
stage('Update DEV ECS') {
when { branch 'dev' }
steps {
script{
withAWS(region:'us-east-1') {
def outputs = cfnUpdate(stack: 'DEV-TARGETDB-SERVICE',
params:["ImageUrl=${env.FULL_ECR_URL}"],
keepParams:[
'EnvVarName1',
'EnvVarValue1',
'EnvVarName2',
'EnvVarValue2',
'EnvVarName3',
'EnvVarValue3',
'ContainerCpu',
'ContainerMemory',
'ContainerPort',
'DatabaseHost',
'DatabaseName',
'DatabasePassword',
'DatabaseUser',
'DesiredCount',
'HttpCode',
'HTTPSListener',
'Path',
'Priority',
'Role',
'TaskRole',
'ServiceName',
'StackName',
'DataDogAPIKey',
'TaskCpu',
'TaskMemory'
],
timeoutInMinutes:15,
pollInterval:1000)
}
}
}
}
stage('Update TEST ECS') {
when { branch 'master' }
steps {
script{
withAWS(region:'us-east-1') {
def outputs = cfnUpdate(stack: 'TEST-TARGETDB-SERVICE',
params:["ImageUrl=${env.FULL_ECR_URL}"],
keepParams:[
'EnvVarName1',
'EnvVarValue1',
'EnvVarName2',
'EnvVarValue2',
'EnvVarName3',
'EnvVarValue3',
'ContainerCpu',
'ContainerMemory',
'ContainerPort',
'DatabaseHost',
'DatabaseName',
'DatabasePassword',
'DatabaseUser',
'DesiredCount',
'HttpCode',
'HTTPSListener',
'Path',
'Priority',
'Role',
'TaskRole',
'ServiceName',
'StackName',
'DataDogAPIKey',
'TaskCpu',
'TaskMemory'
],
timeoutInMinutes:15,
pollInterval:1000)
}
}
}
}
stage('Update PROD ECS') {
when {
branch 'master'
tag 'v*'
}
steps {
script{
withAWS(region:'us-east-1') {
def outputs = cfnUpdate(stack: 'PROD-TARGETDB-SERVICE',
params:["ImageUrl=${env.FULL_ECR_URL}"],
keepParams:[
'EnvVarName1',
'EnvVarValue1',
'EnvVarName2',
'EnvVarValue2',
'EnvVarName3',
'EnvVarValue3',
'ContainerCpu',
'ContainerMemory',
'ContainerPort',
'DatabaseHost',
'DatabaseName',
'DatabasePassword',
'DatabaseUser',
'DesiredCount',
'HttpCode',
'HTTPSListener',
'Path',
'Priority',
'Role',
'TaskRole',
'ServiceName',
'StackName',
'DataDogAPIKey',
'TaskCpu',
'TaskMemory'
],
timeoutInMinutes:15,
pollInterval:1000)
}
}
}
}
The dev and prod stages work, but the prod stage does not work. I don't even see a job in jenkins for the prod stage (I do for dev and master). There is a "tags" section under the project in jenkins, but it is empty.
Upvotes: 0
Views: 3047
Reputation: 4261
When you checkout a Git repository, you checkout either by branch or by tag, but not both. When Jenkins checks out a branch, it sets up BRANCH_NAME variable to the name of the checked out branch; when it checks out a tag it sets TAG_NAME and BRANCH_NAME to the name of the tag.
The documentation says:
If the when directive contains more than one condition, all the child conditions must return true for the stage to execute.
The tag condition matches TAG_NAME, while branch condition matches BRANCH_NAME so they both need to be true in order for stage to execute. Since they happen to be the same in the case of a tag checkout, this condition setup is always false.
The solution depends on what you want to achieve. If you wish to execute the stage if any of those is true, the condition should read:
when {
anyOf {
branch 'master'
tag 'v*'
}
}
If you just wish to run on v* tags, I would just drop branch condition entirely.
Upvotes: 1