summaryrefslogtreecommitdiff
path: root/spec/models
diff options
context:
space:
mode:
authorRémy Coutable <remy@rymai.me>2016-07-20 12:40:46 +0000
committerRémy Coutable <remy@rymai.me>2016-07-20 12:40:46 +0000
commitf4964116ddf04703a92f5a1c33b5eb3a6747ee58 (patch)
treed1ee29c4caec25849b461ff746b713b0783374cf /spec/models
parenta920bf3834e870dbc1afabb043c962d26a9c9664 (diff)
parent8eafdafdda25071dcad4ce143d5c65c1ca278730 (diff)
downloadgitlab-ce-f4964116ddf04703a92f5a1c33b5eb3a6747ee58.tar.gz
Merge branch 'ci-predefined-variables' into 'master'
Add predefined CI variables to GitLab ## What does this MR do? This adds predefined CI variables to GitLab for container registry, pipelines, project name, etc. It also makes sure that all currently documented variables are send from GitLab. This is added to follow up on this proposal: https://gitlab.com/gitlab-org/gitlab-ci-multi-runner/issues/185#note_11844286. To migrate almost all variables out of Runner to GitLab to simplify adding a new of variables without the need for changing the GitLab Runner. ## Why was this MR needed? Our CI variables miss a lot of crucial information that should be easily accessible. This tries to fill this gap. ## What are the relevant issue numbers? Fixes https://gitlab.com/gitlab-org/gitlab-ci-multi-runner/issues/185, https://gitlab.com/gitlab-org/gitlab-ce/issues/18164, https://gitlab.com/gitlab-org/gitlab-ce/issues/18075. ## Does this MR meet the acceptance criteria? - [ ] [CHANGELOG](https://gitlab.com/gitlab-org/gitlab-ce/blob/master/CHANGELOG) entry added - [ ] [Documentation created/updated](https://gitlab.com/gitlab-org/gitlab-ce/blob/master/doc/development/doc_styleguide.md) - [ ] API support added - Tests - [ ] Added for this feature/bug - [ ] All builds are passing - [ ] Conform by the [style guides](https://gitlab.com/gitlab-org/gitlab-ce/blob/master/CONTRIBUTING.md#style-guides) - [ ] Branch has no merge conflicts with `master` (if you do - rebase it please) - [ ] [Squashed related commits together](https://git-scm.com/book/en/Git-Tools-Rewriting-History#Squashing-Commits) See merge request !4826
Diffstat (limited to 'spec/models')
-rw-r--r--spec/models/build_spec.rb182
1 files changed, 121 insertions, 61 deletions
diff --git a/spec/models/build_spec.rb b/spec/models/build_spec.rb
index 06d984c7a40..f9c9725a23d 100644
--- a/spec/models/build_spec.rb
+++ b/spec/models/build_spec.rb
@@ -191,79 +191,87 @@ describe Ci::Build, models: true do
end
describe '#variables' do
+ let(:container_registry_enabled) { false }
let(:predefined_variables) do
[
- { key: :CI_BUILD_NAME, value: 'test', public: true },
- { key: :CI_BUILD_STAGE, value: 'test', public: true },
+ { key: 'CI', value: 'true', public: true },
+ { key: 'GITLAB_CI', value: 'true', public: true },
+ { key: 'CI_BUILD_ID', value: build.id.to_s, public: true },
+ { key: 'CI_BUILD_TOKEN', value: build.token, public: false },
+ { key: 'CI_BUILD_REF', value: build.sha, public: true },
+ { key: 'CI_BUILD_BEFORE_SHA', value: build.before_sha, public: true },
+ { key: 'CI_BUILD_REF_NAME', value: 'master', public: true },
+ { key: 'CI_BUILD_NAME', value: 'test', public: true },
+ { key: 'CI_BUILD_STAGE', value: 'test', public: true },
+ { key: 'CI_SERVER_NAME', value: 'GitLab', public: true },
+ { key: 'CI_SERVER_VERSION', value: Gitlab::VERSION, public: true },
+ { key: 'CI_SERVER_REVISION', value: Gitlab::REVISION, public: true },
+ { key: 'CI_PROJECT_ID', value: project.id.to_s, public: true },
+ { key: 'CI_PROJECT_NAME', value: project.path, public: true },
+ { key: 'CI_PROJECT_PATH', value: project.path_with_namespace, public: true },
+ { key: 'CI_PROJECT_NAMESPACE', value: project.namespace.path, public: true },
+ { key: 'CI_PROJECT_URL', value: project.web_url, public: true },
+ { key: 'CI_PIPELINE_ID', value: pipeline.id.to_s, public: true }
]
end
+ before do
+ stub_container_registry_config(enabled: container_registry_enabled, host_port: 'registry.example.com')
+ end
+
subject { build.variables }
context 'returns variables' do
- let(:yaml_variables) do
- [
- { key: :DB_NAME, value: 'postgres', public: true }
- ]
- end
-
before do
- build.yaml_variables = yaml_variables
+ build.yaml_variables = []
end
- it { is_expected.to eq(predefined_variables + yaml_variables) }
-
- context 'for tag' do
- let(:tag_variable) do
- [
- { key: :CI_BUILD_TAG, value: 'master', public: true }
- ]
- end
+ it { is_expected.to eq(predefined_variables) }
+ end
- before do
- build.update_attributes(tag: true)
- end
+ context 'when build is for tag' do
+ let(:tag_variable) do
+ { key: 'CI_BUILD_TAG', value: 'master', public: true }
+ end
- it { is_expected.to eq(tag_variable + predefined_variables + yaml_variables) }
+ before do
+ build.update_attributes(tag: true)
end
- context 'and secure variables' do
- let(:secure_variables) do
- [
- { key: 'SECRET_KEY', value: 'secret_value', public: false }
- ]
- end
+ it { is_expected.to include(tag_variable) }
+ end
- before do
- build.project.variables << Ci::Variable.new(key: 'SECRET_KEY', value: 'secret_value')
- end
+ context 'when secure variable is defined' do
+ let(:secure_variable) do
+ { key: 'SECRET_KEY', value: 'secret_value', public: false }
+ end
- it { is_expected.to eq(predefined_variables + yaml_variables + secure_variables) }
+ before do
+ build.project.variables << Ci::Variable.new(key: 'SECRET_KEY', value: 'secret_value')
+ end
- context 'and trigger variables' do
- let(:trigger) { create(:ci_trigger, project: project) }
- let(:trigger_request) { create(:ci_trigger_request_with_variables, pipeline: pipeline, trigger: trigger) }
- let(:trigger_variables) do
- [
- { key: :TRIGGER_KEY, value: 'TRIGGER_VALUE', public: false }
- ]
- end
- let(:predefined_trigger_variable) do
- [
- { key: :CI_BUILD_TRIGGERED, value: 'true', public: true }
- ]
- end
+ it { is_expected.to include(secure_variable) }
+ end
- before do
- build.trigger_request = trigger_request
- end
+ context 'when build is for triggers' do
+ let(:trigger) { create(:ci_trigger, project: project) }
+ let(:trigger_request) { create(:ci_trigger_request_with_variables, pipeline: pipeline, trigger: trigger) }
+ let(:user_trigger_variable) do
+ { key: :TRIGGER_KEY, value: 'TRIGGER_VALUE', public: false }
+ end
+ let(:predefined_trigger_variable) do
+ { key: 'CI_BUILD_TRIGGERED', value: 'true', public: true }
+ end
- it { is_expected.to eq(predefined_variables + predefined_trigger_variable + yaml_variables + secure_variables + trigger_variables) }
- end
+ before do
+ build.trigger_request = trigger_request
end
+
+ it { is_expected.to include(user_trigger_variable) }
+ it { is_expected.to include(predefined_trigger_variable) }
end
- context 'when yaml_variables is undefined' do
+ context 'when yaml_variables are undefined' do
before do
build.yaml_variables = nil
end
@@ -282,10 +290,10 @@ describe Ci::Build, models: true do
context 'if config does not have a questioned job' do
let(:config) do
YAML.dump({
- test_other: {
- script: 'Hello World'
- }
- })
+ test_other: {
+ script: 'Hello World'
+ }
+ })
end
it { is_expected.to eq(predefined_variables) }
@@ -294,13 +302,13 @@ describe Ci::Build, models: true do
context 'if config has variables' do
let(:config) do
YAML.dump({
- test: {
- script: 'Hello World',
- variables: {
- KEY: 'value'
- }
- }
- })
+ test: {
+ script: 'Hello World',
+ variables: {
+ KEY: 'value'
+ }
+ }
+ })
end
let(:variables) do
[{ key: :KEY, value: 'value', public: true }]
@@ -310,6 +318,58 @@ describe Ci::Build, models: true do
end
end
end
+
+ context 'when container registry is enabled' do
+ let(:container_registry_enabled) { true }
+ let(:ci_registry) do
+ { key: 'CI_REGISTRY', value: 'registry.example.com', public: true }
+ end
+ let(:ci_registry_image) do
+ { key: 'CI_REGISTRY_IMAGE', value: project.container_registry_repository_url, public: true }
+ end
+
+ context 'and is disabled for project' do
+ before do
+ project.update(container_registry_enabled: false)
+ end
+
+ it { is_expected.to include(ci_registry) }
+ it { is_expected.not_to include(ci_registry_image) }
+ end
+
+ context 'and is enabled for project' do
+ before do
+ project.update(container_registry_enabled: true)
+ end
+
+ it { is_expected.to include(ci_registry) }
+ it { is_expected.to include(ci_registry_image) }
+ end
+ end
+
+ context 'when runner is assigned to build' do
+ let(:runner) { create(:ci_runner, description: 'description', tag_list: ['docker', 'linux']) }
+
+ before do
+ build.update(runner: runner)
+ end
+
+ it { is_expected.to include({ key: 'CI_RUNNER_ID', value: runner.id.to_s, public: true }) }
+ it { is_expected.to include({ key: 'CI_RUNNER_DESCRIPTION', value: 'description', public: true }) }
+ it { is_expected.to include({ key: 'CI_RUNNER_TAGS', value: 'docker, linux', public: true }) }
+ end
+
+ context 'returns variables in valid order' do
+ before do
+ allow(build).to receive(:predefined_variables) { ['predefined'] }
+ allow(project).to receive(:predefined_variables) { ['project'] }
+ allow(pipeline).to receive(:predefined_variables) { ['pipeline'] }
+ allow(build).to receive(:yaml_variables) { ['yaml'] }
+ allow(project).to receive(:secret_variables) { ['secret'] }
+ end
+
+ it { is_expected.to eq(%w[predefined project pipeline yaml secret]) }
+ end
end
describe '#has_tags?' do