From 0b03d070167e5f30e727f9e2d2c0216b7c78f888 Mon Sep 17 00:00:00 2001 From: Furkan Ayhan Date: Wed, 6 Apr 2022 16:28:59 +0300 Subject: [PATCH 1/2] Add pipeline schedule variables to forward:pipeline Pipeline schedule variables have the same priority as pipeline variables. We decided that we could consider pipeline schedule variables as pipeline variables. --- app/assets/javascripts/editor/schema/ci.json | 4 ++-- app/models/ci/bridge.rb | 12 +++++++++++- doc/ci/yaml/index.md | 2 +- spec/models/ci/bridge_spec.rb | 20 ++++++++++++++++++++ 4 files changed, 34 insertions(+), 4 deletions(-) diff --git a/app/assets/javascripts/editor/schema/ci.json b/app/assets/javascripts/editor/schema/ci.json index 2d222903c0c069..adfd09d695fc16 100644 --- a/app/assets/javascripts/editor/schema/ci.json +++ b/app/assets/javascripts/editor/schema/ci.json @@ -1291,7 +1291,7 @@ }, "pipeline_variables": { "type": "boolean", - "description": "Variables added for manual pipeline runs are passed to downstream pipelines.", + "description": "Variables added for manual pipeline runs and scheduled pipelines and scheduled pipelines are passed to downstream pipelines.", "default": false } } @@ -1407,7 +1407,7 @@ }, "pipeline_variables": { "type": "boolean", - "description": "Variables added for manual pipeline runs are passed to downstream pipelines.", + "description": "Variables added for manual pipeline runs and scheduled pipelines are passed to downstream pipelines.", "default": false } } diff --git a/app/models/ci/bridge.rb b/app/models/ci/bridge.rb index cfc23608124a3d..ff444ddefa39ff 100644 --- a/app/models/ci/bridge.rb +++ b/app/models/ci/bridge.rb @@ -274,7 +274,8 @@ def calculate_downstream_variables # The order of this list refers to the priority of the variables downstream_yaml_variables(expand_variables) + - downstream_pipeline_variables(expand_variables) + downstream_pipeline_variables(expand_variables) + + downstream_pipeline_schedule_variables(expand_variables) end def downstream_yaml_variables(expand_variables) @@ -293,6 +294,15 @@ def downstream_pipeline_variables(expand_variables) end end + def downstream_pipeline_schedule_variables(expand_variables) + return [] unless forward_pipeline_variables? + return [] unless pipeline.pipeline_schedule + + pipeline.pipeline_schedule.variables.to_a.map do |variable| + { key: variable.key, value: ::ExpandVariables.expand(variable.value, expand_variables) } + end + end + def forward_yaml_variables? strong_memoize(:forward_yaml_variables) do result = options&.dig(:trigger, :forward, :yaml_variables) diff --git a/doc/ci/yaml/index.md b/doc/ci/yaml/index.md index 443dbb866ee9d8..b6875568b9e03e 100644 --- a/doc/ci/yaml/index.md +++ b/doc/ci/yaml/index.md @@ -3713,7 +3713,7 @@ and [multi-project pipelines](../pipelines/multi_project_pipelines.md). - `yaml_variables`: `true` (default), or `false`. When `true`, variables defined in the trigger job are passed to downstream pipelines. -- `pipeline_variables`: `true` or `false` (default). When `true`, [manual pipeline variables](../variables/index.md#override-a-defined-cicd-variable) +- `pipeline_variables`: `true` or `false` (default). When `true`, [manual pipeline variables](../variables/index.md#override-a-defined-cicd-variable) and [scheduled pipeline variables](../pipelines/schedules.md#add-a-pipeline-schedule) are passed to downstream pipelines. **Example of `trigger:forward`**: diff --git a/spec/models/ci/bridge_spec.rb b/spec/models/ci/bridge_spec.rb index 6f25f2879a4f9b..e4f08356a38c25 100644 --- a/spec/models/ci/bridge_spec.rb +++ b/spec/models/ci/bridge_spec.rb @@ -288,6 +288,26 @@ ) end end + + context 'when the pipeline runs from a pipeline schedule' do + let(:pipeline_schedule) { create(:ci_pipeline_schedule, :nightly, project: project ) } + let(:pipeline) { create(:ci_pipeline, pipeline_schedule: pipeline_schedule) } + + let(:options) do + { trigger: { project: 'my/project', forward: { pipeline_variables: true } } } + end + + before do + pipeline_schedule.variables.create!(key: 'schedule_var_key', value: 'schedule var value') + end + + it 'adds the scheduler variable' do + expect(bridge.downstream_variables).to contain_exactly( + { key: 'BRIDGE', value: 'cross' }, + { key: 'schedule_var_key', value: 'schedule var value' } + ) + end + end end end -- GitLab From f62f03d36d7c1d5e9e718f6606bcd1f3102b0fbd Mon Sep 17 00:00:00 2001 From: Avielle Wolfe Date: Fri, 8 Apr 2022 13:08:45 +0000 Subject: [PATCH 2/2] Apply 2 suggestion(s) to 2 file(s) --- app/assets/javascripts/editor/schema/ci.json | 2 +- spec/models/ci/bridge_spec.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/assets/javascripts/editor/schema/ci.json b/app/assets/javascripts/editor/schema/ci.json index adfd09d695fc16..fe3229ac91b10c 100644 --- a/app/assets/javascripts/editor/schema/ci.json +++ b/app/assets/javascripts/editor/schema/ci.json @@ -1291,7 +1291,7 @@ }, "pipeline_variables": { "type": "boolean", - "description": "Variables added for manual pipeline runs and scheduled pipelines and scheduled pipelines are passed to downstream pipelines.", + "description": "Variables added for manual pipeline runs and scheduled pipelines are passed to downstream pipelines.", "default": false } } diff --git a/spec/models/ci/bridge_spec.rb b/spec/models/ci/bridge_spec.rb index e4f08356a38c25..5ee560c4925af7 100644 --- a/spec/models/ci/bridge_spec.rb +++ b/spec/models/ci/bridge_spec.rb @@ -301,7 +301,7 @@ pipeline_schedule.variables.create!(key: 'schedule_var_key', value: 'schedule var value') end - it 'adds the scheduler variable' do + it 'adds the schedule variable' do expect(bridge.downstream_variables).to contain_exactly( { key: 'BRIDGE', value: 'cross' }, { key: 'schedule_var_key', value: 'schedule var value' } -- GitLab