Skip to content

Commit

Permalink
Merge pull request #326 from pkwarren/issue-325
Browse files Browse the repository at this point in the history
Fix WhenDeclaration environment condition.
  • Loading branch information
stchar committed Jan 28, 2021
2 parents b303b7a + ab51fb1 commit 4ffdfa2
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
Expand Up @@ -14,6 +14,8 @@ class WhenDeclaration extends GenericPipelineDeclaration {
String branch
String tag
Closure<Boolean> expression
String envName
String envValue

private static Pattern getPatternFromGlob(String glob) {
// from https://stackoverflow.com/a/3619098
Expand Down Expand Up @@ -44,6 +46,11 @@ class WhenDeclaration extends GenericPipelineDeclaration {
this.expression = closure
}

def environment(Map args) {
this.envName = args.name as String
this.envValue = args.value as String
}

Boolean execute(Object delegate) {
boolean expressionCheck = true
boolean branchCheck = true
Expand Down Expand Up @@ -71,11 +78,9 @@ class WhenDeclaration extends GenericPipelineDeclaration {
if (tag) {
tagCheck = delegate.env.TAG_NAME =~ tag
}
if (!(env as Map)?.isEmpty()) {
def environment = env as Map
environment.entrySet().forEach { e ->
envCheck = envCheck && (delegate.env."${e.key}" == e.value)
}
if (envName != null) {
def val = delegate?.env[envName]
envCheck = (val == envValue)
}

return expressionCheck && branchCheck && tagCheck && envCheck && anyOfCheck && notCheck
Expand Down
Expand Up @@ -20,6 +20,7 @@ class TestDeclarativePipeline extends DeclarativePipelineTest {
assertCallStackContains('pipeline unit tests completed')
assertCallStackContains('pipeline unit tests post CLEANUP')
assertCallStack().doesNotContain('pipeline unit tests UNSUCCESSFUL')
assertCallStackContains('Skipping stage Checkout')
assertJobStatusSuccess()
}

Expand Down

0 comments on commit 4ffdfa2

Please sign in to comment.