Commit a032c296 authored by Grzegorz Bizon's avatar Grzegorz Bizon

Move pipeline-level variables from build to pipeline

parent 1d27bf92
...@@ -35,6 +35,7 @@ module Ci ...@@ -35,6 +35,7 @@ module Ci
serialize :yaml_variables, Gitlab::Serializer::Ci::Variables # rubocop:disable Cop/ActiveRecordSerialize serialize :yaml_variables, Gitlab::Serializer::Ci::Variables # rubocop:disable Cop/ActiveRecordSerialize
delegate :name, to: :project, prefix: true delegate :name, to: :project, prefix: true
delegate :ref_slug, to: :pipeline
validates :coverage, numericality: true, allow_blank: true validates :coverage, numericality: true, allow_blank: true
validates :ref, presence: true validates :ref, presence: true
...@@ -233,17 +234,6 @@ module Ci ...@@ -233,17 +234,6 @@ module Ci
user == current_user user == current_user
end end
# A slugified version of the build ref, suitable for inclusion in URLs and
# domain names. Rules:
#
# * Lowercased
# * Anything not matching [a-z0-9-] is replaced with a -
# * Maximum length is 63 bytes
# * First/Last Character is not a hyphen
def ref_slug
Gitlab::Utils.slugify(ref.to_s)
end
# Variables whose value does not depend on environment # Variables whose value does not depend on environment
def simple_variables def simple_variables
variables(environment: nil) variables(environment: nil)
...@@ -541,15 +531,11 @@ module Ci ...@@ -541,15 +531,11 @@ module Ci
{ key: 'CI_JOB_NAME', value: name, public: true }, { key: 'CI_JOB_NAME', value: name, public: true },
{ key: 'CI_JOB_STAGE', value: stage, public: true }, { key: 'CI_JOB_STAGE', value: stage, public: true },
{ key: 'CI_JOB_TOKEN', value: token, public: false }, { key: 'CI_JOB_TOKEN', value: token, public: false },
{ key: 'CI_COMMIT_SHA', value: sha, public: true },
{ key: 'CI_COMMIT_REF_NAME', value: ref, public: true },
{ key: 'CI_COMMIT_REF_SLUG', value: ref_slug, public: true },
{ key: 'CI_REGISTRY_USER', value: CI_REGISTRY_USER, public: true }, { key: 'CI_REGISTRY_USER', value: CI_REGISTRY_USER, public: true },
{ key: 'CI_REGISTRY_PASSWORD', value: token, public: false }, { key: 'CI_REGISTRY_PASSWORD', value: token, public: false },
{ key: 'CI_REPOSITORY_URL', value: repo_url, public: false } { key: 'CI_REPOSITORY_URL', value: repo_url, public: false }
] ]
variables << { key: "CI_COMMIT_TAG", value: ref, public: true } if tag?
variables << { key: "CI_PIPELINE_TRIGGERED", value: 'true', public: true } if trigger_request variables << { key: "CI_PIPELINE_TRIGGERED", value: 'true', public: true } if trigger_request
variables << { key: "CI_JOB_MANUAL", value: 'true', public: true } if action? variables << { key: "CI_JOB_MANUAL", value: 'true', public: true } if action?
variables.concat(legacy_variables) variables.concat(legacy_variables)
......
...@@ -473,7 +473,7 @@ module Ci ...@@ -473,7 +473,7 @@ module Ci
end end
def predefined_variables def predefined_variables
pipeline_predefined = [ predefined = [
{ key: 'CI', value: 'true', public: true }, { key: 'CI', value: 'true', public: true },
{ key: 'GITLAB_CI', value: 'true', public: true }, { key: 'GITLAB_CI', value: 'true', public: true },
{ key: 'CI_SERVER_NAME', value: 'GitLab', public: true }, { key: 'CI_SERVER_NAME', value: 'GitLab', public: true },
...@@ -481,10 +481,16 @@ module Ci ...@@ -481,10 +481,16 @@ module Ci
{ key: 'CI_SERVER_REVISION', value: Gitlab::REVISION, public: true }, { key: 'CI_SERVER_REVISION', value: Gitlab::REVISION, public: true },
{ key: 'CI_PIPELINE_ID', value: id.to_s, public: true }, { key: 'CI_PIPELINE_ID', value: id.to_s, public: true },
{ key: 'CI_CONFIG_PATH', value: ci_yaml_file_path, public: true }, { key: 'CI_CONFIG_PATH', value: ci_yaml_file_path, public: true },
{ key: 'CI_PIPELINE_SOURCE', value: source.to_s, public: true } { key: 'CI_PIPELINE_SOURCE', value: source.to_s, public: true },
{ key: 'CI_COMMIT_SHA', value: sha, public: true },
{ key: 'CI_COMMIT_REF_NAME', value: ref, public: true },
{ key: 'CI_COMMIT_REF_SLUG', value: ref_slug, public: true }
] ]
Array(project.predefined_variables) + pipeline_predefined predefined.push(key: 'CI_COMMIT_TAG', value: ref, public: true) if tag?
predefined.push(key: 'CI_PIPELINE_TRIGGERED', value: 'true', public: true) if variables.any?
Array(project.predefined_variables) + predefined
end end
def priority_variables def priority_variables
...@@ -533,6 +539,18 @@ module Ci ...@@ -533,6 +539,18 @@ module Ci
@latest_builds_with_artifacts ||= builds.latest.with_artifacts.to_a @latest_builds_with_artifacts ||= builds.latest.with_artifacts.to_a
end end
# A slugified version of the ref, suitable for inclusion in URLs and
# domain names. Rules:
#
# * Lowercased
# * Anything not matching [a-z0-9-] is replaced with a -
# * Maximum length is 63 bytes
# * First/Last Character is not a hyphen
#
def ref_slug
Gitlab::Utils.slugify(ref.to_s)
end
private private
def ci_yaml_from_repo def ci_yaml_from_repo
......
...@@ -1245,24 +1245,8 @@ describe Ci::Build do ...@@ -1245,24 +1245,8 @@ describe Ci::Build do
end end
describe '#ref_slug' do describe '#ref_slug' do
{ it 'delegates ref_slug method to the pipeline' do
'master' => 'master', expect(build).to delegate_method(:ref_slug).to(:pipeline)
'1-foo' => '1-foo',
'fix/1-foo' => 'fix-1-foo',
'fix-1-foo' => 'fix-1-foo',
'a' * 63 => 'a' * 63,
'a' * 64 => 'a' * 63,
'FOO' => 'foo',
'-' + 'a' * 61 + '-' => 'a' * 61,
'-' + 'a' * 62 + '-' => 'a' * 62,
'-' + 'a' * 63 + '-' => 'a' * 62,
'a' * 62 + ' ' => 'a' * 62
}.each do |ref, slug|
it "transforms #{ref} to #{slug}" do
build.ref = ref
expect(build.ref_slug).to eq(slug)
end
end end
end end
......
...@@ -1280,6 +1280,28 @@ describe Ci::Pipeline, :mailer do ...@@ -1280,6 +1280,28 @@ describe Ci::Pipeline, :mailer do
end end
end end
describe '#ref_slug' do
{
'master' => 'master',
'1-foo' => '1-foo',
'fix/1-foo' => 'fix-1-foo',
'fix-1-foo' => 'fix-1-foo',
'a' * 63 => 'a' * 63,
'a' * 64 => 'a' * 63,
'FOO' => 'foo',
'-' + 'a' * 61 + '-' => 'a' * 61,
'-' + 'a' * 62 + '-' => 'a' * 62,
'-' + 'a' * 63 + '-' => 'a' * 62,
'a' * 62 + ' ' => 'a' * 62
}.each do |ref, slug|
it "transforms #{ref} to #{slug}" do
pipeline.ref = ref
expect(pipeline.ref_slug).to eq(slug)
end
end
end
describe '#execute_hooks' do describe '#execute_hooks' do
let!(:build_a) { create_build('a', 0) } let!(:build_a) { create_build('a', 0) }
let!(:build_b) { create_build('b', 0) } let!(:build_b) { create_build('b', 0) }
......
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