Skip to content

Commit

Permalink
[FIXED JENKINS-45198] When possible, add scm vars to environment
Browse files Browse the repository at this point in the history
This will not result in any change when run on core versions <2.60 (or
with workflow-scm-step <2.6, git <3.3.1, etc), but will Just Work if
you've got the right core/etc versions installed.

BasicModelDefTest#scmEnvVars will start failing when we bump core etc
dependencies for this plugin directly.
  • Loading branch information
abayer committed Aug 4, 2017
1 parent 103bec1 commit e75cf19
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 3 deletions.
Expand Up @@ -57,6 +57,7 @@
import org.jenkinsci.plugins.workflow.pipelinegraphanalysis.GenericStatus;
import org.jenkinsci.plugins.workflow.pipelinegraphanalysis.StatusAndTiming;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.jvnet.hudson.test.Issue;

Expand Down Expand Up @@ -854,4 +855,17 @@ public void parallelStagesAgentEnvWhen() throws Exception {


}

@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")
// TODO: switch to .logNotContains("GIT_COMMIT is null") once we've moved to core 2.60+,
// workflow-scm-step 2.6+, git 3.3.1+
.logContains("GIT_COMMIT is null")
.go();
}
}
37 changes: 37 additions & 0 deletions pipeline-model-definition/src/test/resources/scmEnvVars.groovy
@@ -0,0 +1,37 @@
/*
* The MIT License
*
* Copyright (c) 2017, 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("foo") {
steps {
echo "GIT_COMMIT is ${env.GIT_COMMIT}"
}
}
}
}



Expand Up @@ -44,17 +44,24 @@ public class CheckoutScript implements Serializable {

private static Closure checkoutAndRun(CpsScript script, DeclarativeAgent agent, Closure body) {
return {
def checkoutMap
if (agent.isDoCheckout() && agent.hasScmContext(script)) {
if (!agent.inStage) {
script.stage(SyntheticStageNames.checkout()) {
script.checkout script.scm
checkoutMap = script.checkout script.scm
}
} else {
// No stage when we're in a nested stage already
script.checkout script.scm
checkoutMap = script.checkout script.scm
}
}
body.call()
if (checkoutMap != null && checkoutMap instanceof Map) {
script.withEnv(checkoutMap.collect { k, v -> "${k}=${v}" }) {
body.call()
}
} else {
body.call()
}
}
}

Expand Down

0 comments on commit e75cf19

Please sign in to comment.