Permalink
Browse files

[FIXED JENKINS-45198] When possible, add scm vars to environment

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 e75cf195fa6d9fb4972911284718d682e6bd3888
@@ -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;

@@ -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();
}
}
@@ -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}"
}
}
}
}



@@ -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()
}
}
}

0 comments on commit e75cf19

Please sign in to comment.