Make it possible to access static builds variables
This makes it possible to access static build variables even when an object is not persisted yet. This will allow us to evaluate build variables using only/except policies before deciding whether we actually want to persist the build in the database, or not.
This commit is contained in:
parent
0335d09e58
commit
96d6193cab
|
@ -547,16 +547,16 @@ module Ci
|
|||
variables.append(key: 'CI_SERVER_NAME', value: 'GitLab')
|
||||
variables.append(key: 'CI_SERVER_VERSION', value: Gitlab::VERSION)
|
||||
variables.append(key: 'CI_SERVER_REVISION', value: Gitlab::REVISION)
|
||||
variables.append(key: 'CI_JOB_ID', value: id.to_s)
|
||||
variables.append(key: 'CI_JOB_ID', value: id.to_s) if persisted?
|
||||
variables.append(key: 'CI_JOB_NAME', value: name)
|
||||
variables.append(key: 'CI_JOB_STAGE', value: stage)
|
||||
variables.append(key: 'CI_JOB_TOKEN', value: token, public: false)
|
||||
variables.append(key: 'CI_JOB_TOKEN', value: token, public: false) if persisted?
|
||||
variables.append(key: 'CI_COMMIT_SHA', value: sha)
|
||||
variables.append(key: 'CI_COMMIT_REF_NAME', value: ref)
|
||||
variables.append(key: 'CI_COMMIT_REF_SLUG', value: ref_slug)
|
||||
variables.append(key: 'CI_REGISTRY_USER', value: CI_REGISTRY_USER)
|
||||
variables.append(key: 'CI_REGISTRY_PASSWORD', value: token, public: false)
|
||||
variables.append(key: 'CI_REPOSITORY_URL', value: repo_url, public: false)
|
||||
variables.append(key: 'CI_REGISTRY_USER', value: CI_REGISTRY_USER) if persisted?
|
||||
variables.append(key: 'CI_REGISTRY_PASSWORD', value: token, public: false) if persisted?
|
||||
variables.append(key: 'CI_REPOSITORY_URL', value: repo_url, public: false) if persisted?
|
||||
variables.append(key: "CI_COMMIT_TAG", value: ref) if tag?
|
||||
variables.append(key: "CI_PIPELINE_TRIGGERED", value: 'true') if trigger_request
|
||||
variables.append(key: "CI_JOB_MANUAL", value: 'true') if action?
|
||||
|
@ -579,8 +579,11 @@ module Ci
|
|||
|
||||
def legacy_variables
|
||||
Gitlab::Ci::Variables::Collection.new.tap do |variables|
|
||||
variables.append(key: 'CI_BUILD_ID', value: id.to_s)
|
||||
variables.append(key: 'CI_BUILD_TOKEN', value: token, public: false)
|
||||
if persisted?
|
||||
variables.append(key: 'CI_BUILD_ID', value: id.to_s)
|
||||
variables.append(key: 'CI_BUILD_TOKEN', value: token, public: false)
|
||||
end
|
||||
|
||||
variables.append(key: 'CI_BUILD_REF', value: sha)
|
||||
variables.append(key: 'CI_BUILD_BEFORE_SHA', value: before_sha)
|
||||
variables.append(key: 'CI_BUILD_REF_NAME', value: ref)
|
||||
|
|
|
@ -1977,6 +1977,59 @@ describe Ci::Build do
|
|||
end
|
||||
end
|
||||
end
|
||||
|
||||
context 'when build has not been persisted yet' do
|
||||
let(:build) do
|
||||
described_class.new(
|
||||
name: 'rspec',
|
||||
stage: 'test',
|
||||
ref: 'feature',
|
||||
project: project,
|
||||
pipeline: pipeline
|
||||
)
|
||||
end
|
||||
|
||||
it 'does not persist the build' do
|
||||
expect(build).to be_valid
|
||||
expect(build).not_to be_persisted
|
||||
|
||||
variables = build.variables
|
||||
|
||||
expect(variables.size).to be >= 28
|
||||
expect(build).not_to be_persisted
|
||||
end
|
||||
|
||||
it 'returns static predefined variables' do
|
||||
keys = %w[CI_JOB_NAME
|
||||
CI_COMMIT_SHA
|
||||
CI_COMMIT_REF_NAME
|
||||
CI_COMMIT_REF_SLUG
|
||||
CI_JOB_STAGE]
|
||||
|
||||
build.variables.map { |var| var.fetch(:key) }.tap do |names|
|
||||
expect(names).to include(*keys)
|
||||
end
|
||||
|
||||
expect(build.variables).to include(key: 'CI_COMMIT_REF_NAME',
|
||||
value: 'feature',
|
||||
public: true)
|
||||
end
|
||||
|
||||
it 'does not return prohibited variables' do
|
||||
keys = %w[CI_JOB_ID
|
||||
CI_JOB_TOKEN
|
||||
CI_BUILD_ID
|
||||
CI_BUILD_TOKEN
|
||||
CI_REGISTRY_USER
|
||||
CI_REGISTRY_PASSWORD
|
||||
CI_REPOSITORY_URL
|
||||
CI_ENVIRONMENT_URL]
|
||||
|
||||
build.variables.map { |var| var.fetch(:key) }.tap do |names|
|
||||
expect(names).not_to include(*keys)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe 'state transition: any => [:pending]' do
|
||||
|
|
Loading…
Reference in New Issue