Skip to content

Commit

Permalink
Merge pull request #12 from abayer/jenkins-37932
Browse files Browse the repository at this point in the history
[FIXED JENKINS-37932] Add "agent any"
  • Loading branch information
abayer committed Sep 9, 2016
2 parents 38b3f7a + 0fe3ea8 commit 4604f74
Show file tree
Hide file tree
Showing 9 changed files with 86 additions and 9 deletions.
Expand Up @@ -59,8 +59,9 @@ public final class ModelASTAgent extends ModelASTElement {
public String toGroovy() {
String argStr
// TODO: Stop special-casing agent none.
if (args instanceof ModelASTSingleArgument && args.value.value.equals("none")) {
argStr = "none"
if (args instanceof ModelASTSingleArgument &&
(args.value.value.equals("none") || args.value.value.equals("any"))) {
argStr = args.value.value
} else {
argStr = args.toGroovy()
}
Expand Down
Expand Up @@ -45,6 +45,9 @@ public class Agent implements Serializable {
@Whitelisted
String label

@Whitelisted
Boolean any

@Whitelisted
public Agent(Map<String,String> args) {
if (args.containsKey("docker")) {
Expand All @@ -57,12 +60,12 @@ public class Agent implements Serializable {

@Whitelisted
public Agent(boolean b) {

this.any = b
}

@Whitelisted
public boolean hasAgent() {
return docker != null || label != null
return any || docker != null || label != null
}

// TODO: Rewrite as an extension point and get this by extension discovery, but we knew that already.
Expand Down
Expand Up @@ -460,9 +460,10 @@ class ModelParser {
if (e instanceof VariableExpression) {
if (e.name.equals("none")) {
return ModelASTValue.fromConstant("none", e) // Special casing for agent none.
} else if (e.name.equals("any")) {
return ModelASTValue.fromConstant("any", e) // Special casing for agent any.
}
}

// for other composite expressions, treat it as in-place GString
return ModelASTValue.fromGString("\${"+getSourceText(e)+"}", e)
}
Expand All @@ -479,7 +480,7 @@ class ModelParser {
if (exp instanceof ConstantExpression) {
return castOrNull(String,exp.value);
}
// TODO: This may be too broad a way to catch 'agent none'.
// TODO: This may be too broad a way to catch 'agent none' and 'agent any'.
else if (exp instanceof VariableExpression) {
return castOrNull(String,exp.name);
}
Expand Down
Expand Up @@ -363,14 +363,14 @@ class ModelValidator {
if (agent.args instanceof ModelASTSingleArgument) {
ModelASTSingleArgument singleArg = (ModelASTSingleArgument) agent.args

if (singleArg.value.getValue() != 'none') {
errorCollector.error(agent.args, "Invalid argument for agent - '${singleArg.value.getValue()}' - must be map of config options or bare none.")
if (singleArg.value.getValue() != 'none' && singleArg.value.getValue() != 'any') {
errorCollector.error(agent.args, "Invalid argument for agent - '${singleArg.value.getValue()}' - must be map of config options or bare none or any.")
valid = false
} else if (agent.args instanceof ModelASTNamedArgumentList) {
ModelASTNamedArgumentList namedArgs = (ModelASTNamedArgumentList)agent.args
namedArgs.arguments.each { k, v ->
if (!(Agent.agentConfigKeys().contains(k.key))) {
errorCollector.error(k, "Invalid config option '${k.key}'. Valid config optiosn are ${Agent.agentConfigKeys()}.")
errorCollector.error(k, "Invalid config option '${k.key}'. Valid config options are ${Agent.agentConfigKeys()}.")
valid = false
}
}
Expand Down
Expand Up @@ -45,6 +45,11 @@ public class ClosureModelTranslator implements MethodMissingWrapper, Serializabl
*/
boolean none = false

/**
* Placeholder to make sure 'agent any' works.
*/
boolean any = true

ClosureModelTranslator(Class clazz) {
actualClass = clazz
}
Expand Down
Expand Up @@ -86,6 +86,7 @@ public void setUp() throws Exception {

public static final List<String> SHOULD_PASS_CONFIGS = ImmutableList.of(
"simplePipeline",
"agentAny",
"agentDocker",
"agentLabel",
"agentNoneWithNode",
Expand Down
Expand Up @@ -28,6 +28,7 @@
import hudson.slaves.EnvironmentVariablesNodeProperty;
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
import org.junit.Test;
import org.jvnet.hudson.test.Issue;

/**
* @author Andrew Bayer
Expand All @@ -48,6 +49,17 @@ public void agentLabel() throws Exception {
j.assertLogContains("ONSLAVE=true", b);
}

@Issue("JENKINS-37932")
@Test
public void agentAny() throws Exception {
prepRepoWithJenkinsfile("agentAny");

WorkflowRun b = getAndStartBuild();
j.assertBuildStatusSuccess(j.waitForCompletion(b));
j.assertLogContains("[Pipeline] { (foo)", b);
j.assertLogContains("THIS WORKS", b);
}

@Test
public void noCheckoutScmInWrongContext() throws Exception {
DumbSlave s = j.createOnlineSlave();
Expand Down
35 changes: 35 additions & 0 deletions src/test/resources/agentAny.groovy
@@ -0,0 +1,35 @@
/*
* The MIT License
*
* Copyright (c) 2016, 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") {
sh('echo "THIS WORKS"')
}
}
}



19 changes: 19 additions & 0 deletions src/test/resources/json/agentAny.json
@@ -0,0 +1,19 @@
{"pipeline": {
"stages": [ {
"name": "foo",
"branches": [ {
"name": "default",
"steps": [ {
"name": "sh",
"arguments": {
"isConstant": true,
"value": "echo \"THIS WORKS\""
}
}]
}]
}],
"agent": {
"isConstant": true,
"value": "any"
}
}}

0 comments on commit 4604f74

Please sign in to comment.