Commit dfa6e574 authored by Andrei Merfu's avatar Andrei Merfu Committed by Fabio Pitino

Evaluate the variables for runner tags section

parent 983fe9da
......@@ -67,6 +67,7 @@ module Gitlab
.deep_merge(rules_attributes)
.deep_merge(allow_failure_criteria_attributes)
.deep_merge(@cache.cache_attributes)
.deep_merge(runner_tags)
end
def bridge?
......@@ -202,6 +203,16 @@ module Gitlab
end
end
def runner_tags
{ tag_list: evaluate_runner_tags }.compact
end
def evaluate_runner_tags
@seed_attributes[:tag_list]&.map do |tag|
ExpandVariables.expand_existing(tag, evaluate_context.variables)
end
end
# If a job uses `allow_failure:exit_codes` and `rules:allow_failure`
# we need to prevent the exit codes from being persisted because they
# would break the behavior defined by `rules:allow_failure`.
......
......@@ -90,6 +90,20 @@ RSpec.describe Gitlab::Ci::Pipeline::Seed::Build do
end
end
context 'with job:tags' do
let(:attributes) do
{
name: 'rspec',
ref: 'master',
job_variables: [{ key: 'VARIABLE', value: 'value', public: true }],
tag_list: ['static-tag', '$VARIABLE', '$NO_VARIABLE']
}
end
it { is_expected.to include(tag_list: ['static-tag', 'value', '$NO_VARIABLE']) }
it { is_expected.to include(yaml_variables: [{ key: 'VARIABLE', value: 'value', public: true }]) }
end
context 'with cache:key' do
let(:attributes) do
{
......
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Ci::CreatePipelineService do
let_it_be(:group) { create(:group, :private) }
let_it_be(:group_variable) { create(:ci_group_variable, group: group, key: 'RUNNER_TAG', value: 'group')}
let_it_be(:project) { create(:project, :repository, group: group) }
let_it_be(:user) { create(:user) }
let(:service) { described_class.new(project, user, ref: 'master') }
let(:pipeline) { service.execute(:push) }
let(:job) { pipeline.builds.find_by(name: 'job') }
before do
project.add_developer(user)
stub_ci_pipeline_yaml_file config
end
context 'when the variable is set' do
let(:config) do
<<~EOS
variables:
KUBERNETES_RUNNER: kubernetes
job:
tags:
- docker
- $KUBERNETES_RUNNER
script:
- echo "Hello runner selector feature"
EOS
end
it 'uses the evaluated variable' do
expect(pipeline).to be_created_successfully
expect(job.tags.pluck(:name)).to match_array(%w[docker kubernetes])
end
end
context 'when the tag is composed by two variables' do
let(:config) do
<<~EOS
variables:
CLOUD_PROVIDER: aws
KUBERNETES_RUNNER: kubernetes
ENVIRONMENT_NAME: prod
job:
tags:
- docker
- $CLOUD_PROVIDER-$KUBERNETES_RUNNER-$ENVIRONMENT_NAME
script:
- echo "Hello runner selector feature"
EOS
end
it 'uses the evaluated variables' do
expect(pipeline).to be_created_successfully
expect(job.tags.pluck(:name)).to match_array(%w[docker aws-kubernetes-prod])
end
end
context 'when the variable is not set' do
let(:config) do
<<~EOS
job:
tags:
- docker
- $KUBERNETES_RUNNER
script:
- echo "Hello runner selector feature"
EOS
end
it 'uses the variable as a regular string' do
expect(pipeline).to be_created_successfully
expect(job.tags.pluck(:name)).to match_array(%w[docker $KUBERNETES_RUNNER])
end
end
context 'when the tag uses group variables' do
let(:config) do
<<~EOS
job:
tags:
- docker
- $RUNNER_TAG
script:
- echo "Hello runner selector feature"
EOS
end
it 'uses the evaluated variables' do
expect(pipeline).to be_created_successfully
expect(job.tags.pluck(:name)).to match_array(%w[docker group])
end
end
context 'when the tag has the same variable name defined for both group and project' do
let_it_be(:project_variable) { create(:ci_variable, project: project, key: 'RUNNER_TAG', value: 'project') }
let(:config) do
<<~EOS
variables:
RUNNER_TAG: pipeline
job:
tags:
- docker
- $RUNNER_TAG
script:
- echo "Hello runner selector feature"
EOS
end
it 'uses the project variable instead of group due to variable precedence' do
expect(pipeline).to be_created_successfully
expect(job.tags.pluck(:name)).to match_array(%w[docker project])
end
end
context 'with parallel:matrix config' do
let(:tags) { pipeline.builds.map(&:tags).flatten.pluck(:name) }
let(:config) do
<<~EOS
job:
parallel:
matrix:
- PROVIDER: [aws, gcp]
STACK: [monitoring, backup, app]
tags:
- ${PROVIDER}-${STACK}
script:
- echo "Hello runner selector feature"
EOS
end
it 'uses the evaluated variables' do
expect(pipeline).to be_created_successfully
expect(tags).to match_array(%w[aws-monitoring aws-backup aws-app gcp-monitoring gcp-backup gcp-app])
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