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

Support for template reference and parameters for pipelines. #47

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 31 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ pipelines:
group: mygroup # note that the group name can contain only of alphanumeric & underscore characters
label_template: "${mygit[:8]}"
locking: off
parameters: # list of parameters that can be configured for a pipeline
param1: value1
materials:
mygit: # this is the name of material, the name can contain only of alphanumeric & underscore characters
# keyword git says about type of material and url at once
Expand Down Expand Up @@ -110,6 +112,7 @@ Feel free to improve it!

1. [Environment](#environment)
1. [Environment variables](#environment-variables)
1. [Parameters](#parameters)
1. [Pipeline](#pipeline)
* [Tabs](#tabs)
* [Tracking tool](#tracking-tool)
Expand Down Expand Up @@ -146,7 +149,7 @@ Feel free to improve it!
A minimal [pipeline](https://docs.go.cd/current/configuration/configuration_reference.html#pipeline) configuration must contain:
* pipeline name - as a key in hash
* [materials](#materials) - a **hash**
* [stages](#stage) - a **list**
* [stages](#stage) - a **list** or a `template` - as a key in hash

```yaml
mypipe:
Expand All @@ -170,9 +173,11 @@ All elements available on a pipeline object are:
* [tracking_tool](#tracking-tool) or `mingle`
* [timer](#timer)
* [environment_variables](#environment-variables)
* [parameters](#parameters)
* `secure_variables`
* [materials](#materials)
* [stages](#stage)
* `template`

```yaml
pipe2:
Expand All @@ -193,11 +198,22 @@ pipe2:
stages:
...
```
#### Referencing an existing template in a pipeline:
```yaml
mypipe:
group: group1
label_template: "foo-1.0-${COUNT}"
locking: on
parameters:
param1: value
materials:
mygit:
git: http://example.com/mygit.git
template: template1
```

Please note:
* [templates](https://docs.go.cd/current/configuration/configuration_reference.html#templates) are not supported
* [parameters](https://docs.go.cd/current/configuration/configuration_reference.html#params) are not supported
* pipeline declares a group to which it belongs
* Pipeline declares a group to which it belongs

### Tracking tool

Expand Down Expand Up @@ -693,6 +709,17 @@ secure_variables:
MY_PASSWORD: "s&Du#@$xsSa"
```

### Parameters

[Parameters](https://docs.go.cd/current/configuration/configuration_reference.html#params) can be declared at the pipeline level.

```yaml
parameters:
param1: value1
production: no
```


#### To generate an encrypted value

**For versions of GoCD >= 17.1:**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package cd.go.plugin.config.yaml.transforms;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;

import java.util.Map;

public class ParameterTransform {

public static final String YAML_PIPELINE_PARAMETERS_FIELD = "parameters";
public static final String JSON_PARAM_NAME_FIELD = "name";
public static final String JSON_PARAM_VALUE_FIELD = "value";


public JsonArray transform(Object all) {
Map<String, Object> map = (Map<String, Object>) all;
Object parameters = map.get(YAML_PIPELINE_PARAMETERS_FIELD);
JsonArray paramArray = new JsonArray();
if (parameters != null && parameters != "") {
for (Map.Entry<String, String> param : ((Map<String, String>) parameters).entrySet()) {
JsonObject paramJson = new JsonObject();
paramJson.addProperty(JSON_PARAM_NAME_FIELD, param.getKey());
paramJson.addProperty(JSON_PARAM_VALUE_FIELD, param.getValue());
paramArray.add(paramJson);
}

}
return paramArray;
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@

import static cd.go.plugin.config.yaml.YamlUtils.*;
import static cd.go.plugin.config.yaml.transforms.EnvironmentVariablesTransform.JSON_ENV_VAR_FIELD;
import static cd.go.plugin.config.yaml.transforms.ParameterTransform.YAML_PIPELINE_PARAMETERS_FIELD;

public class PipelineTransform {
private static final String JSON_PIPELINE_NAME_FIELD = "name";
private static final String JSON_PIPELINE_GROUP_FIELD = "group";
private static final String JSON_PIPELINE_TEMPLATE_FIELD = "template";
private static final String JSON_PIPELINE_LABEL_TEMPLATE_FIELD = "label_template";
private static final String JSON_PIPELINE_PIPE_LOCKING_FIELD = "enable_pipeline_locking";
private static final String JSON_PIPELINE_MINGLE_FIELD = "mingle";
Expand All @@ -22,6 +24,7 @@ public class PipelineTransform {
private static final String JSON_PIPELINE_STAGES_FIELD = "stages";

private static final String YAML_PIPELINE_GROUP_FIELD = "group";
private static final String YAML_PIPELINE_TEMPLATE_FIELD = "template";
private static final String YAML_PIPELINE_LABEL_TEMPLATE_FIELD = "label_template";
private static final String YAML_PIPELINE_PIPE_LOCKING_FIELD = "locking";
private static final String YAML_PIPELINE_MINGLE_FIELD = "mingle";
Expand All @@ -33,11 +36,13 @@ public class PipelineTransform {
private final MaterialTransform materialTransform;
private final StageTransform stageTransform;
private final EnvironmentVariablesTransform variablesTransform;
private ParameterTransform parameterTransform;

public PipelineTransform(MaterialTransform materialTransform, StageTransform stageTransform, EnvironmentVariablesTransform variablesTransform) {
public PipelineTransform(MaterialTransform materialTransform, StageTransform stageTransform, EnvironmentVariablesTransform variablesTransform, ParameterTransform parameterTransform) {
this.materialTransform = materialTransform;
this.stageTransform = stageTransform;
this.variablesTransform = variablesTransform;
this.parameterTransform = parameterTransform;
}

public JsonObject transform(Object maybePipe) {
Expand All @@ -55,6 +60,7 @@ public JsonObject transform(Map.Entry<String, Object> entry) {
Map<String, Object> pipeMap = (Map<String, Object>) entry.getValue();

addOptionalString(pipeline, pipeMap, JSON_PIPELINE_GROUP_FIELD, YAML_PIPELINE_GROUP_FIELD);
addOptionalString(pipeline, pipeMap, JSON_PIPELINE_TEMPLATE_FIELD, YAML_PIPELINE_TEMPLATE_FIELD);
addOptionalString(pipeline, pipeMap, JSON_PIPELINE_LABEL_TEMPLATE_FIELD, YAML_PIPELINE_LABEL_TEMPLATE_FIELD);
addOptionalBoolean(pipeline, pipeMap, JSON_PIPELINE_PIPE_LOCKING_FIELD, YAML_PIPELINE_PIPE_LOCKING_FIELD);

Expand All @@ -67,7 +73,14 @@ public JsonObject transform(Map.Entry<String, Object> entry) {
pipeline.add(JSON_ENV_VAR_FIELD, jsonEnvVariables);

addMaterials(pipeline, pipeMap);
addStages(pipeline, pipeMap);
if (!pipeline.has(JSON_PIPELINE_TEMPLATE_FIELD)) {
addStages(pipeline, pipeMap);
}

JsonArray params = parameterTransform.transform(pipeMap);
if (params != null && params.size() > 0) {
pipeline.add(YAML_PIPELINE_PARAMETERS_FIELD, params);
}

return pipeline;
}
Expand All @@ -86,7 +99,7 @@ private void addTimer(JsonObject pipeline, Map<String, Object> pipeMap) {
private void addStages(JsonObject pipeline, Map<String, Object> pipeMap) {
Object stages = pipeMap.get(YAML_PIPELINE_STAGES_FIELD);
if (stages == null || !(stages instanceof List))
throw new YamlConfigException("expected a list of pipeline stages");
throw new YamlConfigException("expected a list of pipeline stages or a template reference");
List<Object> stagesList = (List<Object>) stages;
JsonArray stagesArray = transformStages(stagesList);
pipeline.add(JSON_PIPELINE_STAGES_FIELD, stagesArray);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ public class RootTransform {
public RootTransform() {
EnvironmentVariablesTransform environmentVarsTransform = new EnvironmentVariablesTransform();
MaterialTransform material = new MaterialTransform();
ParameterTransform parameterTransform = new ParameterTransform();
JobTransform job = new JobTransform(environmentVarsTransform, new TaskTransform());
StageTransform stage = new StageTransform(environmentVarsTransform, job);
this.pipelineTransform = new PipelineTransform(material, stage, environmentVarsTransform);
this.pipelineTransform = new PipelineTransform(material, stage, environmentVarsTransform, parameterTransform);
this.environmentsTransform = new EnvironmentsTransform(environmentVarsTransform);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package cd.go.plugin.config.yaml.transforms;

import com.google.gson.JsonArray;
import org.junit.Test;

import java.io.IOException;

import static cd.go.plugin.config.yaml.TestUtils.readJsonObject;
import static cd.go.plugin.config.yaml.TestUtils.readYamlObject;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.*;

public class ParameterTransformTest {

private final ParameterTransform paramterTransform;

public ParameterTransformTest() {
paramterTransform = new ParameterTransform();
}

@Test
public void shouldTransformEmpty() throws IOException {
testTransform("empty");
}

@Test
public void shouldTransformAParameter() throws IOException {
testTransform("param");
}

private void testTransform(String caseFile) throws IOException {
testTransform(caseFile, caseFile);
}

private void testTransform(String caseFile, String expectedFile) throws IOException {
JsonArray expected = (JsonArray) readJsonObject("parts/parameters/" + expectedFile + ".json");
JsonArray actual = paramterTransform.transform(readYamlObject("parts/parameters/" + caseFile + ".yaml"));
assertThat(actual, is(expected));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ public void SetUp() {
materialTransform = mock(MaterialTransform.class);
stageTransform = mock(StageTransform.class);
environmentTransform = mock(EnvironmentVariablesTransform.class);
parser = new PipelineTransform(materialTransform, stageTransform, environmentTransform);
ParameterTransform parameterTransform = mock(ParameterTransform.class);
parser = new PipelineTransform(materialTransform, stageTransform, environmentTransform, parameterTransform);
}

@Test
Expand All @@ -38,6 +39,11 @@ public void shouldTransformRichPipeline() throws IOException {
testTransform("rich.pipe");
}

@Test
public void shouldTransformAPipelineReferencingATemplate() throws IOException {
testTransform("template_ref.pipe");
}

private void testTransform(String caseFile) throws IOException {
testTransform(caseFile, caseFile);
}
Expand Down
2 changes: 2 additions & 0 deletions src/test/resources/parts/parameters/empty.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[
]
1 change: 1 addition & 0 deletions src/test/resources/parts/parameters/empty.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
parameters:
6 changes: 6 additions & 0 deletions src/test/resources/parts/parameters/param.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[
{
"name": "param1",
"value": "value1"
}
]
2 changes: 2 additions & 0 deletions src/test/resources/parts/parameters/param.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
parameters:
param1: value1
1 change: 1 addition & 0 deletions src/test/resources/parts/rich.pipe.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pipe2:
spec: "0 15 10 * * ? *"
only_on_changes: yes
environment_variables: null
parameters: null
materials:
foo:
bar:
Expand Down
19 changes: 19 additions & 0 deletions src/test/resources/parts/template_ref.pipe.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"group": "group1",
"name": "pipe2",
"label_template": "foo-1.0-${COUNT}",
"enable_pipeline_locking": true,
"tracking_tool": {
"link": "http://your-trackingtool/yourproject/${ID}",
"regex": "evo-(\\d+)"
},
"timer": {
"spec": "0 15 10 * * ? *",
"only_on_changes": true
},
"materials": [
null,
null
],
"template": "template"
}
15 changes: 15 additions & 0 deletions src/test/resources/parts/template_ref.pipe.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
pipe2:
group: group1
label_template: "foo-1.0-${COUNT}"
locking: on
tracking_tool:
link: "http://your-trackingtool/yourproject/${ID}"
regex: "evo-(\\d+)"
timer:
spec: "0 15 10 * * ? *"
only_on_changes: yes
environment_variables: null
materials:
foo:
bar:
template: template