Skip to content

Commit

Permalink
Merge pull request #334 from dduportal/bugfix/GH-330-buildingTag
Browse files Browse the repository at this point in the history
Fix the buildingTag() method's support
  • Loading branch information
stchar authored Jan 28, 2021
2 parents ea14345 + e970d12 commit 380522d
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class WhenDeclaration extends GenericPipelineDeclaration {

AnyOfDeclaration anyOf
NotDeclaration not
Boolean buildingTag = false
Boolean buildingTag
String branch
String tag
Closure<Boolean> expression
Expand Down Expand Up @@ -66,7 +66,7 @@ class WhenDeclaration extends GenericPipelineDeclaration {
branchCheck = antPathMatcher.match(branch, delegate.env.BRANCH_NAME)
}
if (buildingTag) {
tagCheck = delegate.env.containsKey(TAG_NAME)
tagCheck = delegate?.env?.containsKey("TAG_NAME")
}
if (tag) {
tagCheck = delegate.env.TAG_NAME =~ tag
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,23 @@ class TestDeclarativePipeline extends DeclarativePipelineTest {
assertJobStatusSuccess()
}

@Test void when_buildingTag() throws Exception {
addEnvVar('TAG_NAME', 'release-1.0.0')
runScript('Tag_Jenkinsfile')
printCallStack()
assertCallStack().contains('Generating Release Notes')
assertJobStatusSuccess()
}

@Test void when_buildingTag_not() throws Exception {
// no TAG_NAME variable defined
runScript('Tag_Jenkinsfile')
printCallStack()
assertCallStack().contains('Skipping stage Example Release Notes') // No stage bound to a "buildingTag()" condition
assertCallStack().contains('Skipping stage Example Deploy') // No stage bound to a "tag <arg>" condition
assertJobStatusSuccess()
}

@Test void when_tag() throws Exception {
addEnvVar('TAG_NAME', 'v1.1.1')
runScript('Tag_Jenkinsfile')
Expand Down
10 changes: 9 additions & 1 deletion src/test/jenkins/jenkinsfiles/Tag_Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,15 @@ pipeline {
tag 'v*'
}
steps {
echo 'Deploying'
echo 'Deploying only tags matching the pattern v*'
}
}
stage('Example Release Notes') {
when {
buildingTag()
}
steps {
echo 'Generating Release Notes for any tag'
}
}
}
Expand Down

0 comments on commit 380522d

Please sign in to comment.