Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge branch 'master' into JENKINS-40370
Conflicts:
	pipeline-model-definition/src/main/resources/org/jenkinsci/plugins/pipeline/modeldefinition/Messages.properties
	pipeline-model-definition/src/main/resources/org/jenkinsci/plugins/pipeline/modeldefinition/agent/impl/DockerPipelineFromDockerfileScript.groovy
	pipeline-model-definition/src/main/resources/org/jenkinsci/plugins/pipeline/modeldefinition/agent/impl/DockerPipelineScript.groovy
  • Loading branch information
abayer committed Jan 9, 2017
2 parents 73fade1 + 1e14524 commit 528818f
Show file tree
Hide file tree
Showing 135 changed files with 1,368 additions and 677 deletions.
44 changes: 32 additions & 12 deletions SYNTAX.md
Expand Up @@ -13,30 +13,48 @@ These are sections that are specified directly within the `pipeline` argument cl
* *Description*: Specifies where the build or stage will run.
* *Required*: Yes for the top-level `pipeline` closure, optional for individual `stage` closures.
* *Allowed In*: Top-level `pipeline` closure and individual `stage` closures.
* *Parameters*: Either a `Map` of one or more arguments or one of two constants - `none` or `any`.
* *Parameters*: Either a `Closure` of one key and either a single value or a `Closure` of multiple configuration pairs, or one of two constants - `none` or `any`.
* *Map Keys*:
* Note that this will be an `ExtensionPoint`, so plugins will be able to add to the available image providers.
* `docker`
* *Type*: `String`
* *Description*: If given, uses this Docker image for the container the build will run in. If no `label` is
given, the container will be run within a simple `node { ... }` block.
* *Example*: `docker:'ubuntu'`
* `dockerfile`
* *Description*: If given, builds from the Dockerfile in the repository and runs in a container using the resulting image.
* `label`
* *Type*: `String`
* *Description*: If given, uses this label for the node the build will run on - if `docker` is also
specified, the container will run on that node.
* *Example*: `label:'hi-speed'`
* `none`
* *Type*: bareword
* *Description*: If given, node/image management will need to be specified explicitly in stages and no
automatic `checkout scm` call will occur.
* *Takes a Closure*: No
* `any`
* *Type*: bareword
* *Description*: If given, any available agent will be used.
* *Takes a Closure*: yes
* *Examples*:
* `agent label:'has-docker', docker:'ubuntu:lts'`
* `agent docker:'ubuntu:lts'`
* `agent label:'hi-speed'`
* `agent none`
* `agent any`

```groovy
agent {
label 'hi-speed'
}
agent {
docker 'ubuntu:lts'
}
agent {
docker {
image 'ubuntu:lts'
label 'has-docker'
args "-v /tmp:/tmp -p 80:80"
}
}
agent none
agent any
```

### environment
* *Description*: A sequence of `key = value` pairs, which will be passed to the `withEnv` step the build will be
Expand Down Expand Up @@ -109,7 +127,9 @@ stages {
}
stage('second') {
agent label:'some-node'
agent {
label 'some-node'
}
when {
branch "master"
}
Expand Down
@@ -1,5 +1,6 @@
package org.jenkinsci.plugins.pipeline.modeldefinition.ast;

import net.sf.json.JSONObject;
import org.jenkinsci.plugins.pipeline.modeldefinition.validator.ModelValidator;

/**
Expand All @@ -9,57 +10,87 @@
* @author Andrew Bayer
*/
public final class ModelASTAgent extends ModelASTElement {
private ModelASTArgumentList args;
private ModelASTMethodArg variables;
private ModelASTKey agentType;

public ModelASTAgent(Object sourceLocation) {
super(sourceLocation);
}

@Override
public Object toJSON() {
return args.toJSON();
public JSONObject toJSON() {
final JSONObject j = new JSONObject();
j.accumulate("type", agentType.toJSON());

if (variables != null) {
if (variables instanceof ModelASTClosureMap &&
!((ModelASTClosureMap) variables).getVariables().isEmpty()) {
j.accumulate("arguments", variables.toJSON());
} else if (variables instanceof ModelASTValue) {
j.accumulate("argument", variables.toJSON());
}
}
return j;
}

@Override
public void validate(ModelValidator validator) {
validator.validateElement(this);

args.validate(validator);
if (variables != null) {
variables.validate(validator);
}
}

@Override
public String toGroovy() {
String argStr;
// TODO: Stop special-casing agent none.
if (args instanceof ModelASTSingleArgument && (
((ModelASTSingleArgument) args).getValue().getValue().equals("none") || ((ModelASTSingleArgument) args)
.getValue().getValue().equals("any"))) {
argStr = (String)((ModelASTSingleArgument) args).getValue().getValue();
StringBuilder argStr = new StringBuilder();
if (variables == null ||
(variables instanceof ModelASTClosureMap &&
((ModelASTClosureMap)variables).getVariables().isEmpty())) {
argStr.append(agentType.toGroovy());
} else {
argStr = args.toGroovy();
argStr.append("{\n");
argStr.append(agentType.toGroovy());
argStr.append(" ");
argStr.append(variables.toGroovy());
argStr.append("}");
}

return "agent " + argStr + "\n";
return "agent " + argStr.toString() + "\n";
}

@Override
public void removeSourceLocation() {
super.removeSourceLocation();
args.removeSourceLocation();
if (agentType != null) {
agentType.removeSourceLocation();
}
if (variables != null) {
variables.removeSourceLocation();
}
}

public ModelASTKey getAgentType() {
return agentType;
}

public void setAgentType(ModelASTKey k) {
this.agentType = k;
}

public ModelASTArgumentList getArgs() {
return args;
public ModelASTMethodArg getVariables() {
return variables;
}

public void setArgs(ModelASTArgumentList args) {
this.args = args;
public void setVariables(ModelASTMethodArg variables) {
this.variables = variables;
}

@Override
public String toString() {
return "ModelASTAgent{" +
"args=" + args +
"agentType=" + agentType +
"variables=" + variables +
"}";
}

Expand All @@ -77,14 +108,19 @@ public boolean equals(Object o) {

ModelASTAgent that = (ModelASTAgent) o;

return getArgs() != null ? getArgs().equals(that.getArgs()) : that.getArgs() == null;
if (getAgentType() != null ? !getAgentType().equals(that.getAgentType()) : that.getAgentType() != null) {
return false;
}

return getVariables() != null ? getVariables().equals(that.getVariables()) : that.getVariables() == null;

}

@Override
public int hashCode() {
int result = super.hashCode();
result = 31 * result + (getArgs() != null ? getArgs().hashCode() : 0);
result = 31 * result + (getAgentType() != null ? getAgentType().hashCode() : 0);
result = 31 * result + (getVariables() != null ? getVariables().hashCode() : 0);
return result;
}

Expand Down
@@ -0,0 +1,135 @@
/*
* 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.
*/

package org.jenkinsci.plugins.pipeline.modeldefinition.ast;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.jenkinsci.plugins.pipeline.modeldefinition.validator.ModelValidator;

import java.util.LinkedHashMap;
import java.util.Map;

/**
* Represents a map of names to possible method arguments, in closure form in Groovy
*
* @author Andrew Bayer
*/
public final class ModelASTClosureMap extends ModelASTElement implements ModelASTMethodArg {
private Map<ModelASTKey, ModelASTMethodArg> variables = new LinkedHashMap<>();

public ModelASTClosureMap(Object sourceLocation) {
super(sourceLocation);
}

@Override
public JSONArray toJSON() {
final JSONArray a = new JSONArray();
for (Map.Entry<ModelASTKey, ModelASTMethodArg> entry: variables.entrySet()) {
JSONObject o = new JSONObject();
o.accumulate("key", entry.getKey().toJSON());
o.accumulate("value", entry.getValue().toJSON());
a.add(o);
}
return a;

}

@Override
public void validate(final ModelValidator validator) {
for (Map.Entry<ModelASTKey, ModelASTMethodArg> entry : variables.entrySet()) {
entry.getKey().validate(validator);
entry.getValue().validate(validator);
}
}

@Override
public String toGroovy() {
StringBuilder result = new StringBuilder("{\n");
for (Map.Entry<ModelASTKey, ModelASTMethodArg> entry : variables.entrySet()) {
result.append(entry.getKey().toGroovy()).append(" ").append(entry.getValue().toGroovy()).append('\n');
}
result.append("}\n");
return result.toString();
}

@Override
public void removeSourceLocation() {
super.removeSourceLocation();
for (Map.Entry<ModelASTKey, ModelASTMethodArg> entry : variables.entrySet()) {
entry.getKey().removeSourceLocation();
entry.getValue().removeSourceLocation();
}
}

public Map<ModelASTKey, ModelASTMethodArg> getVariables() {
return variables;
}

public void setVariables(Map<ModelASTKey, ModelASTMethodArg> variables) {
this.variables = variables;
}

public boolean containsKey(String k) {
for (ModelASTKey key : variables.keySet()) {
if (key.getKey().equals(k)) {
return true;
}
}

return false;
}

@Override
public String toString() {
return "ModelASTClosureMap{" +
"variables=" + variables +
"}";
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
if (!super.equals(o)) {
return false;
}

ModelASTClosureMap that = (ModelASTClosureMap) o;

return getVariables() != null ? getVariables().equals(that.getVariables()) : that.getVariables() == null;

}

@Override
public int hashCode() {
int result = super.hashCode();
result = 31 * result + (getVariables() != null ? getVariables().hashCode() : 0);
return result;
}
}
Expand Up @@ -31,6 +31,7 @@
import org.jenkinsci.plugins.pipeline.modeldefinition.ast.ModelASTBuildConditionsContainer;
import org.jenkinsci.plugins.pipeline.modeldefinition.ast.ModelASTBuildParameter;
import org.jenkinsci.plugins.pipeline.modeldefinition.ast.ModelASTBuildParameters;
import org.jenkinsci.plugins.pipeline.modeldefinition.ast.ModelASTClosureMap;
import org.jenkinsci.plugins.pipeline.modeldefinition.ast.ModelASTEnvironment;
import org.jenkinsci.plugins.pipeline.modeldefinition.ast.ModelASTOptions;
import org.jenkinsci.plugins.pipeline.modeldefinition.ast.ModelASTOption;
Expand Down

0 comments on commit 528818f

Please sign in to comment.