Skip to content

Commit

Permalink
[JENKINS-41759] Moved getEnvVars into CPS context
Browse files Browse the repository at this point in the history
So we can call steps without barfing
  • Loading branch information
rsandell committed Feb 10, 2017
1 parent 49ebf57 commit 5ad414e
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 69 deletions.
Expand Up @@ -97,38 +97,6 @@ public class Root implements NestedModel, Serializable {
return this
}

/**
* Helper method for translating the key/value pairs in the {@link Environment} into a list of "key=value" strings
* suitable for use with the withEnv step.
*
* @return a list of "key=value" strings.
*/
List<String> getEnvVars(CpsScript script) {
List<String> e = environment.findAll{k, v -> !(v instanceof DeclarativeEnvironmentContributor)}.collect { k, v ->
"${k}=${v}"
}

environment.each {k, v ->
if (v instanceof DeclarativeEnvironmentContributor && !(v instanceof DeclarativeEnvironmentContributor.MutedGenerator)) {
List<String> ee = v.generate(script, k)
if (ee != null) {
e.addAll(ee)
}
}
}
return e
}

Map<String, Credentials> getEnvCredentials() {
Map<String, Credentials> m = [:]
environment.each {k, v ->
if (v instanceof Credentials) {
m["${k}"] = v;
}
}
return m
}

@Override
public void modelFromMap(Map<String,Object> m) {
m.each { k, v ->
Expand Down
Expand Up @@ -92,39 +92,6 @@ public class Stage implements NestedModel, Serializable {
return this
}

/**
* Helper method for translating the key/value pairs in the {@link Environment} into a list of "key=value" strings
* suitable for use with the withEnv step.
*
* @return a list of "key=value" strings.
*/
List<String> getEnvVars(CpsScript script) {
List<String> e = environment.findAll{k, v -> !(v instanceof DeclarativeEnvironmentContributor)}.collect { k, v ->
"${k}=${v}"
}
environment.each {k, v ->
if (v instanceof DeclarativeEnvironmentContributor && !(v instanceof DeclarativeEnvironmentContributor.MutedGenerator)) {
List<String> ee = v.generate(script, k)
if (ee != null) {
e.addAll(ee)
}
}
}
return e
}

@Nonnull
Map<String, Credentials> getEnvCredentials() {
Map<String, Credentials> m = [:]
environment.each {k, v ->
if (v instanceof Credentials) {
m["${k}"] = v;
}
}
return m
}


@Override
public void modelFromMap(Map<String,Object> m) {
m.each { k, v ->
Expand Down
Expand Up @@ -28,6 +28,7 @@ import com.cloudbees.groovy.cps.impl.CpsClosure
import hudson.FilePath
import hudson.Launcher
import hudson.model.Result
import org.jenkinsci.plugins.pipeline.modeldefinition.environment.DeclarativeEnvironmentContributor
import org.jenkinsci.plugins.pipeline.modeldefinition.environment.impl.Credentials
import org.jenkinsci.plugins.pipeline.modeldefinition.model.*
import org.jenkinsci.plugins.pipeline.modeldefinition.steps.CredentialWrapper
Expand Down Expand Up @@ -69,23 +70,23 @@ public class ModelInterpreter implements Serializable {
executeProperties(root)

// Entire build, including notifications, runs in the withEnv.
withEnvBlock(root.getEnvVars(script)) {
withEnvBlock(getEnvVars(root.environment)) {
inWrappers(root.options) {
// Stage execution and post-build actions run in try/catch blocks, so we still run post-build actions
// even if the build fails.
// We save the caught error, if any, for throwing at the end of the build.
inDeclarativeAgent(root, root.agent) {
withCredentialsBlock(root.getEnvCredentials()) {
withCredentialsBlock(getEnvCredentials(root.environment)) {
toolsBlock(root.agent, root.tools) {
for (int i = 0; i < root.stages.getStages().size(); i++) {
Stage thisStage = root.stages.getStages().get(i)
try {
script.stage(thisStage.name) {
if (firstError == null) {
withEnvBlock(thisStage.getEnvVars(script)) {
withEnvBlock(getEnvVars(thisStage.environment)) {
if (evaluateWhen(thisStage.when)) {
inDeclarativeAgent(thisStage, thisStage.agent) {
withCredentialsBlock(thisStage.getEnvCredentials()) {
withCredentialsBlock(getEnvCredentials(thisStage.environment)) {
toolsBlock(thisStage.agent ?: root.agent, thisStage.tools) {
// Execute the actual stage and potential post-stage actions
executeSingleStage(root, thisStage)
Expand Down Expand Up @@ -474,4 +475,49 @@ public class ModelInterpreter implements Serializable {
script.properties(jobProps)
}
}

/**
* Helper method for translating the key/value pairs in the {@link Environment} into a list of "key=value" strings
* suitable for use with the withEnv step.
*
* @return a list of "key=value" strings.
*/
List<String> getEnvVars(Environment environment) {
List<String> list = []
if (environment != null) {
List<String> entries = []
def map = environment.getMap()
entries.addAll(map.keySet())
for (int i = 0; i < entries.size(); i++) {
def key = entries.get(i)
def value = map[key]
if (!(value instanceof DeclarativeEnvironmentContributor)) {
list.add("${key}=${value}")
} else if (!(value instanceof DeclarativeEnvironmentContributor.MutedGenerator)) {
List<String> ee = value.generate(script, key)
if (ee != null) {
list.addAll(ee)
}
}
}
}
return list
}

Map<String, Credentials> getEnvCredentials(Environment environment) {
Map<String, Credentials> m = [:]
if (environment != null) {
List<String> entries = []
def map = environment.getMap()
entries.addAll(map.keySet())
for (int i = 0; i < entries.size(); i++) {
String key = entries.get(i);
def value = map[key]
if (value instanceof Credentials) {
m["${key}"] = value
}
}
}
return m
}
}

0 comments on commit 5ad414e

Please sign in to comment.