Skip to content

Commit

Permalink
[FIXED JENKINS-50815] Fallback to GIT_LOCAL_BRANCH for when branch
Browse files Browse the repository at this point in the history
If we're not on multibranch (meaning BRANCH_NAME is null), fallback to
GIT_LOCAL_BRANCH for when branch condition. We can theoretically add
other SCMs here too if needed. We're using GIT_LOCAL_BRANCH because it
most closely matches BRANCH_NAME - i.e., GIT_BRANCH includes the
remote, GIT_LOCAL_BRANCH does not.
  • Loading branch information
abayer committed Apr 17, 2018
1 parent 2249ecb commit 7fb4f69
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 8 deletions.
Expand Up @@ -36,7 +36,11 @@ class BranchConditionalScript extends DeclarativeStageConditionalScript<BranchCo

@Override
boolean evaluate() {
return describable.branchMatches(describable.compare,
(String)script.getProperty("env").getProperty("BRANCH_NAME"))
String branchName = (String)script.getProperty("env").getProperty("BRANCH_NAME")
if (branchName == null) {
// fall back to GIT_LOCAL_BRANCH - note that GIT_BRANCH will have the remote on it, while BRANCH_NAME doesn't.
branchName = (String)script.getProperty("env").getProperty("GIT_LOCAL_BRANCH")
}
return describable.branchMatches(describable.compare, branchName)
}
}
Expand Up @@ -1051,13 +1051,9 @@ public void libraryObjectImportInWhenExpr() throws Exception {
.go();
}

@Ignore("This breaks on PCT, so re-enable when we depend on newer core than 2.60")
@Issue("JENKINS-45198")
@Test
public void scmEnvVars() throws Exception {
// The change to support checkout scm returning a map and that map being added to the environment works fine with
// older core etc, but just doesn't do anything, since checkout scm isn't returning anything yet. But with newer
// core, etc, it'll Just Work.
expect("scmEnvVars")
// workflow-scm-step 2.6+, git 3.3.1+
.logNotContains("GIT_COMMIT is null")
Expand Down
Expand Up @@ -350,6 +350,15 @@ public void whenEquals() throws Exception {
expect.resetForNewRun(Result.SUCCESS).logContains("One", "Hello", "Two", "World").go();
}

@Issue("JENKINS-50815")
@Test
public void whenBranchNotMultibranch() throws Exception {
expect("whenBranchNotMultibranch")
.logContains("[Pipeline] { (One)", "[Pipeline] { (Two)", "World")
.go();
}


public static void waitFor(Queue.Item item) throws InterruptedException, ExecutionException {
while (item != null && item.getFuture() == null) {
Thread.sleep(200);
Expand Down
@@ -0,0 +1,46 @@
/*
* The MIT License
*
* Copyright (c) 2018, CloudBees, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

pipeline {
agent any
stages {
stage("One") {
steps {
echo "Hello"
}
}
stage("Two") {
when {
branch "master"
}
steps {
script {
echo "World"
echo "Heal it"
}

}
}
}
}
4 changes: 2 additions & 2 deletions pom.xml
Expand Up @@ -116,12 +116,12 @@
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-scm-step</artifactId>
<version>2.5</version>
<version>2.6</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-scm-step</artifactId>
<version>2.5</version>
<version>2.6</version>
<classifier>tests</classifier>
</dependency>
<dependency>
Expand Down

0 comments on commit 7fb4f69

Please sign in to comment.