Skip to content

Commit

Permalink
feat(3106): Allow job annotation field for pipeline template (#171)
Browse files Browse the repository at this point in the history
  • Loading branch information
y-oksaku authored Nov 1, 2024
1 parent 443be35 commit a5242e7
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 13 deletions.
10 changes: 9 additions & 1 deletion lib/phase/merge.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const Hoek = require('@hapi/hoek');
const clone = require('clone');
const TEMPLATE_NAME_REGEX_WITH_NAMESPACE =
require('screwdriver-data-schema').config.regex.FULL_TEMPLATE_NAME_WITH_NAMESPACE;
const ALLOWED_JOB_FIELDS_WITH_PIPELINE_TEMPLATE = ['settings', 'requires', 'image', 'environment'];
const ALLOWED_JOB_FIELDS_WITH_PIPELINE_TEMPLATE = ['settings', 'requires', 'image', 'environment', 'annotations'];
const { merge } = require('./flatten');

/**
Expand Down Expand Up @@ -109,6 +109,14 @@ function handlePipelineTemplateMergeForJobs(parsedDoc, newPipeline, pipelineTemp
};
}

// Merge job annotations
if (oldJob.annotations || newJob.annotations) {
newJob.annotations = {
...newJob.annotations,
...oldJob.annotations
};
}

// Merge settings
const yamlSettingsConfig = oldJob.settings || {};
const templateSettingsConfig = newJob.settings || {};
Expand Down
15 changes: 13 additions & 2 deletions test/data/pipeline-template-with-customized-job-result.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@
},
"jobs": {
"main": [{
"annotations": {},
"annotations": {
"screwdriver.cd/cpu": "MICRO"
},
"cache": {
"pipeline": [
"~/templateSetting/pipeline"
Expand Down Expand Up @@ -66,7 +68,16 @@
"requires": []
}],
"job1": [{
"annotations": {},
"annotations": {
"screwdriver.cd/buildCluster": "test-cluster",
"testAnnotation": {
"array": ["bar", "baz"],
"obj": {
"text": "this is a test field",
"boolean": false
}
}
},
"cache": {
"pipeline": [
"~/templateSetting/pipeline"
Expand Down
8 changes: 7 additions & 1 deletion test/data/pipeline-template-with-customized-job.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,11 @@ jobs:
slack: [room_b]
environment:
OTHER: foo
annotations:
testAnnotation:
array: [ bar, baz ]
obj:
text: this is a test field
boolean: false
screwdriver.cd/buildCluster: test-cluster
requires: [main]

16 changes: 13 additions & 3 deletions test/data/pipeline-template-with-new-customized-job-result.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@
},
"jobs": {
"main": [{
"annotations": {},
"annotations": {
"screwdriver.cd/cpu": "TURBO",
"screwdriver.cd/buildCluster": "test-cluster"
},
"cache": {
"pipeline": [
"~/templateSetting/pipeline"
Expand Down Expand Up @@ -66,7 +69,12 @@
"requires": []
}],
"job1": [{
"annotations": {},
"annotations": {
"screwdriver.cd/buildCluster": "initial-cluster",
"testAnnotation": {
"testKey": "This is a test field"
}
},
"cache": {
"pipeline": [
"~/templateSetting/pipeline"
Expand Down Expand Up @@ -104,7 +112,9 @@
]
}],
"job2": [{
"annotations": {},
"annotations": {
"other": "bar"
},
"cache": {
"pipeline": [
"~/templateSetting/pipeline"
Expand Down
6 changes: 5 additions & 1 deletion test/data/pipeline-template-with-new-customized-job.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@ jobs:
slack: [room_a]
environment:
BAR: baz
annotations:
screwdriver.cd/buildCluster: test-cluster
screwdriver.cd/cpu: TURBO
requires: []
job2:
image: node:10
environment:
OTHER: foo
annotations:
other: bar
requires: [main]
steps:
- echo: echo new step

11 changes: 9 additions & 2 deletions test/data/pipeline-template-with-pipeline-setting-result.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@
},
"jobs": {
"main": [{
"annotations": {},
"annotations": {
"screwdriver.cd/cpu": "MICRO"
},
"cache": {
"pipeline": [
"~/userSetting/pipeline"
Expand Down Expand Up @@ -67,7 +69,12 @@
]
}],
"job1": [{
"annotations": {},
"annotations": {
"screwdriver.cd/buildCluster": "initial-cluster",
"testAnnotation": {
"testKey": "This is a test field"
}
},
"cache": {
"pipeline": [
"~/userSetting/pipeline"
Expand Down
11 changes: 9 additions & 2 deletions test/data/pipeline-template-with-template-setting-result.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
"jobs": {
"main": [
{
"annotations": {},
"annotations": {
"screwdriver.cd/cpu": "MICRO"
},
"cache": {
"pipeline": [
"~/templateSetting/pipeline"
Expand Down Expand Up @@ -67,7 +69,12 @@
}
],
"job1": [{
"annotations": {},
"annotations": {
"screwdriver.cd/buildCluster": "initial-cluster",
"testAnnotation": {
"testKey": "This is a test field"
}
},
"cache": {
"pipeline": [
"~/templateSetting/pipeline"
Expand Down
9 changes: 9 additions & 0 deletions test/data/pipeline-template-with-template-setting.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
"FOO": "foo",
"BAR": "bar"
},
"annotations": {
"screwdriver.cd/cpu": "MICRO"
},
"secrets": [
"NPM_TOKEN"
]
Expand All @@ -44,6 +47,12 @@
"FOO": "foo",
"BAR": "bar"
},
"annotations": {
"screwdriver.cd/buildCluster": "initial-cluster",
"testAnnotation": {
"testKey": "This is a test field"
}
},
"secrets": [
"NPM_TOKEN"
]
Expand Down
2 changes: 1 addition & 1 deletion test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -774,7 +774,7 @@ describe('config parser', () => {
}).then(data => {
assert.deepEqual(
data.errors[0],
'Error: Job "main" has unsupported fields in user yaml. Can only set settings,requires,image,environment.'
'Error: Job "main" has unsupported fields in user yaml. Can only set settings,requires,image,environment,annotations.'
);
}));

Expand Down

0 comments on commit a5242e7

Please sign in to comment.