Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JENKINS-52744 Showing meaningful error when an environment variable i… #309

Merged
merged 6 commits into from Jan 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -152,6 +152,7 @@ WhenConditionalValidator.changelog.missingParameter=Changelog is missing require
WhenConditionalValidator.changelog.badPattern="{0}" is not a valid regular expression. {1}

ModelInterpreter.NoNodeContext=Attempted to execute a step that requires a node context while \u2018agent none\u2019 was specified. Be sure to specify your own \u2018node '{ ... }'\u2019 blocks when using \u2018agent none\u2019.
ModelInterpreter.EnvironmentVariableFailed=One or more variables have some issues with their values: {0}

Comparator.GLOB.DisplayName=Glob
Comparator.REGEXP.DisplayName=Regular Expression
Expand Down
Expand Up @@ -139,3 +139,4 @@ ModelValidatorImpl.NestedWhenWithoutChildren=Condi\u00e7\u00f5es when aninhadas
ModelValidatorImpl.NestedWhenWrongChildrenCount==Condi\u00e7\u00f5es when aninhadas "{0}" precisam de exatamente {1} condi\u00e7\u00e3o(\u00f5es) filha(s).

ModelInterpreter.NoNodeContext=Tentou executar um step que requer um node context mas foi especificado 'agent none'. Certifique-se de especificar seus blocos 'node { ... }' quando utilizar 'agent none'.
ModelInterpreter.EnvironmentVariableFailed=Uma ou mais vari\u00e1veis t\u00eam alguns problemas com seus valores: {0}
Expand Up @@ -41,6 +41,7 @@ import org.jenkinsci.plugins.workflow.support.steps.build.RunWrapper
import javax.annotation.CheckForNull
import javax.annotation.Nonnull

import static java.lang.String.format
import static org.apache.commons.lang.exception.ExceptionUtils.getFullStackTrace

/**
Expand Down Expand Up @@ -450,8 +451,12 @@ class ModelInterpreter implements Serializable {
def withEnvBlock(Map<String,Closure> envVars, Closure body) {
if (envVars != null && !envVars.isEmpty()) {
List<String> evaledEnv = envVars.collect { k, v ->
"${k}=${v.call()}"
}
try{
"${k}=${v.call()}"
}catch (NullPointerException e) {
throw new IllegalArgumentException( Messages.ModelInterpreter_EnvironmentVariableFailed(k) )
}
}.findAll { it != null}
return {
script.withEnv(evaledEnv) {
body.call()
Expand Down
Expand Up @@ -23,6 +23,7 @@
*/
package org.jenkinsci.plugins.pipeline.modeldefinition;

import hudson.model.Result;
import hudson.model.Slave;
import hudson.slaves.EnvironmentVariablesNodeProperty;
import org.junit.BeforeClass;
Expand Down Expand Up @@ -327,4 +328,12 @@ public void notExpressionInEnvironment() throws Exception {
.logContains("expecting false, got false")
.go();
}

@Issue("JENKINS-52744")
@Test
public void improveMessageErrorWhenEnvVarNotExists() throws Exception {
expect(Result.FAILURE, "improveMessageErrorWhenEnvVarNotExists")
.logContains("IllegalArgumentException: One or more variables have some issues with their values: FOO")
.go();
}
}
@@ -0,0 +1,40 @@
/*
* 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 SOFTWARJENKINSE.
*/

pipeline {
agent none
environment {
FOO = "${BAR?.contains('banana') ? 'bar has banana' : 'no bar or no banana'}"
}
stages {
stage('echo') {
steps {
echo "FOO is ${FOO}"
}
}
}
}