Commit ef378067 authored by Grzegorz Bizon's avatar Grzegorz Bizon Committed by Douglas Barbosa Alexandre

Expand pipeline variables passed downstream

parent 5f32de64
......@@ -134,6 +134,34 @@ staging:
The `ENVIRONMENT` variable will be passed to every job defined in a downstream
pipeline. It will be available as an environment variable when GitLab Runner picks a job.
In the following configuration, the `MY_VARIABLE` variable will be passed
downstream, because jobs inherit variables declared in top-level `variables`:
```yaml
variables:
MY_VARIABLE: my-value
my-pipeline:
variables:
ENVIRONMENT: something
trigger: my/project
```
You might want to pass some information about the upstream pipeline using, for
example, predefined variables. In order to do that, you can use interpolation
to pass any variable. For example:
```yaml
my-pipeline:
variables:
UPSTREAM_BRANCH: $CI_COMMIT_REF_NAME
trigger: my/project
```
In this scenario, the `UPSTREAM_BRANCH` variable with a value related to the
upstream pipeline will be passed to a `downstream` job, and will be available
within the context of all downstream builds.
### Limitations
Because bridge jobs are a little different to regular jobs, it is not
......
......@@ -44,7 +44,11 @@ module EE
end
def downstream_variables
yaml_variables.to_a.map { |hash| hash.except(:public) }
scoped_variables.to_runner_variables.yield_self do |all_variables|
yaml_variables.to_a.map do |hash|
{ key: hash[:key], value: ::ExpandVariables.expand(hash[:value], all_variables) }
end
end
end
end
end
......
---
title: Expand pipeline variables passed downstream
merge_request: 13197
author:
type: added
......@@ -82,6 +82,28 @@ describe Ci::Bridge do
expect(bridge.downstream_variables)
.to include(key: 'BRIDGE', value: 'cross')
end
context 'when using variables interpolation' do
before do
bridge.yaml_variables << { key: 'EXPANDED', value: '$BRIDGE-bridge', public: true }
end
it 'correctly expands variables with interpolation' do
expect(bridge.downstream_variables)
.to include(key: 'EXPANDED', value: 'cross-bridge')
end
end
context 'when recursive interpolation has been used' do
before do
bridge.yaml_variables << { key: 'EXPANDED', value: '$EXPANDED', public: true }
end
it 'does not expand variable recursively' do
expect(bridge.downstream_variables)
.to include(key: 'EXPANDED', value: '$EXPANDED')
end
end
end
describe 'metadata support' do
......
......@@ -154,5 +154,33 @@ describe Ci::CreateCrossProjectPipelineService, '#execute' do
.to have_attributes(key: 'BRIDGE', value: 'var')
end
end
context 'when pipeline variables are defined' do
before do
upstream_pipeline.variables.create(key: 'PIPELINE_VARIABLE', value: 'my-value')
end
it 'does not pass pipeline variables directly downstream' do
pipeline = service.execute(bridge)
pipeline.variables.map(&:key).tap do |variables|
expect(variables).not_to include 'PIPELINE_VARIABLE'
end
end
context 'when using YAML variables interpolation' do
before do
bridge.yaml_variables = [{ key: 'BRIDGE', value: '$PIPELINE_VARIABLE-var', public: true }]
end
it 'makes it possible to pass pipeline variable downstream' do
pipeline = service.execute(bridge)
pipeline.variables.find_by(key: 'BRIDGE').tap do |variable|
expect(variable.value).to eq 'my-value-var'
end
end
end
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment