diff --git a/lib/phase/merge.js b/lib/phase/merge.js index b619f9c..8bbfe24 100644 --- a/lib/phase/merge.js +++ b/lib/phase/merge.js @@ -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'); /** @@ -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 || {}; diff --git a/test/data/pipeline-template-with-customized-job-result.json b/test/data/pipeline-template-with-customized-job-result.json index 4918f38..5381ff1 100644 --- a/test/data/pipeline-template-with-customized-job-result.json +++ b/test/data/pipeline-template-with-customized-job-result.json @@ -24,7 +24,9 @@ }, "jobs": { "main": [{ - "annotations": {}, + "annotations": { + "screwdriver.cd/cpu": "MICRO" + }, "cache": { "pipeline": [ "~/templateSetting/pipeline" @@ -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" diff --git a/test/data/pipeline-template-with-customized-job.yaml b/test/data/pipeline-template-with-customized-job.yaml index 91cb377..8d9da64 100644 --- a/test/data/pipeline-template-with-customized-job.yaml +++ b/test/data/pipeline-template-with-customized-job.yaml @@ -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] - \ No newline at end of file diff --git a/test/data/pipeline-template-with-new-customized-job-result.json b/test/data/pipeline-template-with-new-customized-job-result.json index b9551b9..56939a6 100644 --- a/test/data/pipeline-template-with-new-customized-job-result.json +++ b/test/data/pipeline-template-with-new-customized-job-result.json @@ -24,7 +24,10 @@ }, "jobs": { "main": [{ - "annotations": {}, + "annotations": { + "screwdriver.cd/cpu": "TURBO", + "screwdriver.cd/buildCluster": "test-cluster" + }, "cache": { "pipeline": [ "~/templateSetting/pipeline" @@ -66,7 +69,12 @@ "requires": [] }], "job1": [{ - "annotations": {}, + "annotations": { + "screwdriver.cd/buildCluster": "initial-cluster", + "testAnnotation": { + "testKey": "This is a test field" + } + }, "cache": { "pipeline": [ "~/templateSetting/pipeline" @@ -104,7 +112,9 @@ ] }], "job2": [{ - "annotations": {}, + "annotations": { + "other": "bar" + }, "cache": { "pipeline": [ "~/templateSetting/pipeline" diff --git a/test/data/pipeline-template-with-new-customized-job.yaml b/test/data/pipeline-template-with-new-customized-job.yaml index 185ce18..4b49395 100644 --- a/test/data/pipeline-template-with-new-customized-job.yaml +++ b/test/data/pipeline-template-with-new-customized-job.yaml @@ -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 - \ No newline at end of file diff --git a/test/data/pipeline-template-with-pipeline-setting-result.json b/test/data/pipeline-template-with-pipeline-setting-result.json index a83884f..c1f9000 100644 --- a/test/data/pipeline-template-with-pipeline-setting-result.json +++ b/test/data/pipeline-template-with-pipeline-setting-result.json @@ -26,7 +26,9 @@ }, "jobs": { "main": [{ - "annotations": {}, + "annotations": { + "screwdriver.cd/cpu": "MICRO" + }, "cache": { "pipeline": [ "~/userSetting/pipeline" @@ -67,7 +69,12 @@ ] }], "job1": [{ - "annotations": {}, + "annotations": { + "screwdriver.cd/buildCluster": "initial-cluster", + "testAnnotation": { + "testKey": "This is a test field" + } + }, "cache": { "pipeline": [ "~/userSetting/pipeline" diff --git a/test/data/pipeline-template-with-template-setting-result.json b/test/data/pipeline-template-with-template-setting-result.json index edb5b68..155bbee 100644 --- a/test/data/pipeline-template-with-template-setting-result.json +++ b/test/data/pipeline-template-with-template-setting-result.json @@ -25,7 +25,9 @@ "jobs": { "main": [ { - "annotations": {}, + "annotations": { + "screwdriver.cd/cpu": "MICRO" + }, "cache": { "pipeline": [ "~/templateSetting/pipeline" @@ -67,7 +69,12 @@ } ], "job1": [{ - "annotations": {}, + "annotations": { + "screwdriver.cd/buildCluster": "initial-cluster", + "testAnnotation": { + "testKey": "This is a test field" + } + }, "cache": { "pipeline": [ "~/templateSetting/pipeline" diff --git a/test/data/pipeline-template-with-template-setting.json b/test/data/pipeline-template-with-template-setting.json index 9bfc272..513afe3 100644 --- a/test/data/pipeline-template-with-template-setting.json +++ b/test/data/pipeline-template-with-template-setting.json @@ -23,6 +23,9 @@ "FOO": "foo", "BAR": "bar" }, + "annotations": { + "screwdriver.cd/cpu": "MICRO" + }, "secrets": [ "NPM_TOKEN" ] @@ -44,6 +47,12 @@ "FOO": "foo", "BAR": "bar" }, + "annotations": { + "screwdriver.cd/buildCluster": "initial-cluster", + "testAnnotation": { + "testKey": "This is a test field" + } + }, "secrets": [ "NPM_TOKEN" ] diff --git a/test/index.test.js b/test/index.test.js index 3a887a1..9384e95 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -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.' ); }));