Commit c3882eb4 authored by Kamil Trzcinski's avatar Kamil Trzcinski

Improve implementation of variables

parent cf2e09a1
...@@ -145,7 +145,15 @@ module Ci ...@@ -145,7 +145,15 @@ module Ci
end end
def variables def variables
predefined_variables + yaml_variables + project_variables + trigger_variables variables = predefined_variables
variables += project.predefined_variables
variables += pipeline.predefined_variables
variables += runner.predefined_variables if runner
variables += project.container_registry_variables
variables += yaml_variables
variables += project.secret_variables
variables += trigger_request.user_variables if trigger_request
variables
end end
def merge_request def merge_request
...@@ -430,63 +438,23 @@ module Ci ...@@ -430,63 +438,23 @@ module Ci
self.update(erased_by: user, erased_at: Time.now, artifacts_expire_at: nil) self.update(erased_by: user, erased_at: Time.now, artifacts_expire_at: nil)
end end
def project_variables
project.variables.map do |variable|
{ key: variable.key, value: variable.value, public: false }
end
end
def trigger_variables
if trigger_request && trigger_request.variables
trigger_request.variables.map do |key, value|
{ key: key, value: value, public: false }
end
else
[]
end
end
def predefined_variables def predefined_variables
variables = [] variables = [
variables << { key: 'CI', value: 'true', public: true } { key: 'CI', value: 'true', public: true },
variables << { key: 'GITLAB_CI', value: 'true', public: true } { key: 'GITLAB_CI', value: 'true', public: true },
{ key: 'CI_BUILD_ID', value: id.to_s, public: true },
variables << { key: 'CI_BUILD_ID', value: id.to_s, public: true } { key: 'CI_BUILD_TOKEN', value: token, public: false },
variables << { key: 'CI_BUILD_TOKEN', value: token, public: false } { key: 'CI_BUILD_REF', value: sha, public: true },
variables << { key: 'CI_BUILD_REF', value: sha, public: true } { key: 'CI_BUILD_BEFORE_SHA', value: before_sha, public: true },
variables << { key: 'CI_BUILD_BEFORE_SHA', value: before_sha, public: true } { key: 'CI_BUILD_REF_NAME', value: ref, public: true },
variables << { key: 'CI_BUILD_REF_NAME', value: ref, public: true } { key: 'CI_BUILD_NAME', value: name, public: true },
{ key: 'CI_BUILD_STAGE', value: stage, 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 }
]
variables << { key: 'CI_BUILD_TAG', value: ref, public: true } if tag? variables << { key: 'CI_BUILD_TAG', value: ref, public: true } if tag?
variables << { key: 'CI_BUILD_NAME', value: name, public: true }
variables << { key: 'CI_BUILD_STAGE', value: stage, public: true }
variables << { key: 'CI_BUILD_TRIGGERED', value: 'true', public: true } if trigger_request variables << { key: 'CI_BUILD_TRIGGERED', value: 'true', public: true } if trigger_request
variables << { key: 'CI_PIPELINE_ID', value: pipeline.id.to_s, public: true }
variables << { key: 'CI_PROJECT_ID', value: project.id.to_s, public: true }
variables << { key: 'CI_PROJECT_NAME', value: project.path, public: true }
variables << { key: 'CI_PROJECT_PATH', value: project.path_with_namespace, public: true }
variables << { key: 'CI_PROJECT_NAMESPACE', value: project.namespace.path, public: true }
variables << { key: 'CI_PROJECT_URL', value: project.web_url, public: true }
if Gitlab.config.registry.enabled
variables << { key: 'CI_REGISTRY', value: Gitlab.config.registry.host_port, public: true }
if project.container_registry_enabled?
variables << { key: 'CI_REGISTRY_IMAGE', value: project.container_registry_repository_url, public: true }
end
end
variables << { key: 'CI_SERVER_NAME', value: 'GitLab', public: true }
variables << { key: 'CI_SERVER_VERSION', value: Gitlab::VERSION, public: true }
variables << { key: 'CI_SERVER_REVISION', value: Gitlab::REVISION, public: true }
if runner
variables << { key: 'CI_RUNNER_ID', value: runner.id.to_s, public: true }
variables << { key: 'CI_RUNNER_DESCRIPTION', value: runner.description, public: true }
variables << { key: 'CI_RUNNER_TAGS', value: runner.tag_list.to_s, public: true }
end
variables variables
end end
......
...@@ -198,6 +198,12 @@ module Ci ...@@ -198,6 +198,12 @@ module Ci
Note.for_commit_id(sha) Note.for_commit_id(sha)
end end
def predefined_variables
[
{ key: 'CI_PIPELINE_ID', value: id.to_s, public: true }
]
end
private private
def build_builds_for_stages(stages, user, status, trigger_request) def build_builds_for_stages(stages, user, status, trigger_request)
......
...@@ -114,6 +114,14 @@ module Ci ...@@ -114,6 +114,14 @@ module Ci
tag_list.any? tag_list.any?
end end
def predefined_variables
[
{ key: 'CI_RUNNER_ID', value: id.to_s, public: true },
{ key: 'CI_RUNNER_DESCRIPTION', value: description, public: true },
{ key: 'CI_RUNNER_TAGS', value: tag_list.to_s, public: true }
]
end
private private
def tag_constraints def tag_constraints
......
...@@ -7,5 +7,13 @@ module Ci ...@@ -7,5 +7,13 @@ module Ci
has_many :builds, class_name: 'Ci::Build' has_many :builds, class_name: 'Ci::Build'
serialize :variables serialize :variables
def user_variables
return [] unless variables
variables.map do |key, value|
{ key: key, value: value, public: false }
end
end
end end
end end
...@@ -1164,4 +1164,31 @@ class Project < ActiveRecord::Base ...@@ -1164,4 +1164,31 @@ class Project < ActiveRecord::Base
def ensure_dir_exist def ensure_dir_exist
gitlab_shell.add_namespace(repository_storage_path, namespace.path) gitlab_shell.add_namespace(repository_storage_path, namespace.path)
end end
def predefined_variables
[
{ key: 'CI_PROJECT_ID', value: id.to_s, public: true },
{ key: 'CI_PROJECT_NAME', value: path, public: true },
{ key: 'CI_PROJECT_PATH', value: path_with_namespace, public: true },
{ key: 'CI_PROJECT_NAMESPACE', value: namespace.path, public: true },
{ key: 'CI_PROJECT_URL', value: web_url, public: true }
]
end
def container_registry_variables
return [] unless Gitlab.config.registry.enabled
variables = [
{ key: 'CI_REGISTRY', value: Gitlab.config.registry.host_port, public: true }
]
variables << { key: 'CI_REGISTRY_IMAGE', value: container_registry_repository_url, public: true } if container_registry_enabled?
variables
end
def secret_variables
variables.map do |variable|
{ key: variable.key, value: variable.value, public: false }
end
end
end end
...@@ -37,15 +37,15 @@ The `API_TOKEN` will take the Secure Variable value: `SECURE`. ...@@ -37,15 +37,15 @@ The `API_TOKEN` will take the Secure Variable value: `SECURE`.
| **CI_BUILD_TOKEN** | all | 1.2 | Token used for authenticating with the GitLab Container Registry | | **CI_BUILD_TOKEN** | all | 1.2 | Token used for authenticating with the GitLab Container Registry |
| **CI_PIPELINE_ID** | 8.10 | 0.5 | The unique id of the current pipeline that GitLab CI uses internally | | **CI_PIPELINE_ID** | 8.10 | 0.5 | The unique id of the current pipeline that GitLab CI uses internally |
| **CI_PROJECT_ID** | all | all | The unique id of the current project that GitLab CI uses internally | | **CI_PROJECT_ID** | all | all | The unique id of the current project that GitLab CI uses internally |
| **CI_PROJECT_NAME** | 8.10 | 0.5 | The project name that is currently build | | **CI_PROJECT_NAME** | 8.10 | 0.5 | The project name that is currently being built |
| **CI_PROJECT_NAMESPACE**| 8.10 | 0.5 | The project namespace that is currently build | | **CI_PROJECT_NAMESPACE**| 8.10 | 0.5 | The project namespace that is currently being built |
| **CI_PROJECT_PATH** | 8.10 | 0.5 | The namespace with project name | | **CI_PROJECT_PATH** | 8.10 | 0.5 | The namespace with project name |
| **CI_PROJECT_URL** | 8.10 | 0.5 | The HTTP address to access project | | **CI_PROJECT_URL** | 8.10 | 0.5 | The HTTP address to access project |
| **CI_PROJECT_DIR** | all | all | The full path where the repository is cloned and where the build is ran | | **CI_PROJECT_DIR** | all | all | The full path where the repository is cloned and where the build is run |
| **CI_REGISTRY** | 8.10 | 0.5 | If the Container Registry is enabled it returns address of GitLab's Container Registry | | **CI_REGISTRY** | 8.10 | 0.5 | If the Container Registry is enabled it returns the address of GitLab's Container Registry |
| **CI_REGISTRY_IMAGE** | 8.10 | 0.5 | If the Container Registry is for project it returns the address of registry tied to specific project | | **CI_REGISTRY_IMAGE** | 8.10 | 0.5 | If the Container Registry is enabled for the project it returnes the address of the registry tied to the specific project |
| **CI_RUNNER_ID** | 8.10 | 0.5 | The unique id of the used runner | | **CI_RUNNER_ID** | 8.10 | 0.5 | The unique id of the used runner |
| **CI_RUNNER_DESCRIPTION** | 8.10 | 0.5 | The description of runners as saved in GitLab | | **CI_RUNNER_DESCRIPTION** | 8.10 | 0.5 | The description of the runner as saved in GitLab |
| **CI_RUNNER_TAGS** | 8.10 | 0.5 | The defined runner tags | | **CI_RUNNER_TAGS** | 8.10 | 0.5 | The defined runner tags |
**Some of the variables are only available when using runner with at least defined version.** **Some of the variables are only available when using runner with at least defined version.**
...@@ -68,7 +68,7 @@ export CI_PROJECT_DIR="/builds/gitlab-org/gitlab-ce" ...@@ -68,7 +68,7 @@ export CI_PROJECT_DIR="/builds/gitlab-org/gitlab-ce"
export CI_PROJECT_NAME="gitlab-ce" export CI_PROJECT_NAME="gitlab-ce"
export CI_PROJECT_NAMESPACE="gitlab-org" export CI_PROJECT_NAMESPACE="gitlab-org"
export CI_PROJECT_PATH="gitlab-org/gitlab-ce" export CI_PROJECT_PATH="gitlab-org/gitlab-ce"
export CI_PROJECT_URL="https://gitlab.com/gitlab-org/gitlab-ce.git" export CI_PROJECT_URL="https://gitlab.com/gitlab-org/gitlab-ce"
export CI_REGISTRY="registry.gitlab.com" export CI_REGISTRY="registry.gitlab.com"
export CI_REGISTRY_IMAGE="registry.gitlab.com/gitlab-org/gitlab-ce" export CI_REGISTRY_IMAGE="registry.gitlab.com/gitlab-org/gitlab-ce"
export CI_RUNNER_ID="10" export CI_RUNNER_ID="10"
......
...@@ -203,15 +203,15 @@ describe Ci::Build, models: true do ...@@ -203,15 +203,15 @@ describe Ci::Build, models: true do
{ key: 'CI_BUILD_REF_NAME', value: 'master', public: true }, { key: 'CI_BUILD_REF_NAME', value: 'master', public: true },
{ key: 'CI_BUILD_NAME', value: 'test', public: true }, { key: 'CI_BUILD_NAME', value: 'test', public: true },
{ key: 'CI_BUILD_STAGE', value: 'test', public: true }, { key: 'CI_BUILD_STAGE', value: 'test', public: true },
{ key: 'CI_PIPELINE_ID', value: pipeline.id.to_s, 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_ID', value: project.id.to_s, public: true },
{ key: 'CI_PROJECT_NAME', value: project.path, 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_PATH', value: project.path_with_namespace, public: true },
{ key: 'CI_PROJECT_NAMESPACE', value: project.namespace.path, public: true }, { key: 'CI_PROJECT_NAMESPACE', value: project.namespace.path, public: true },
{ key: 'CI_PROJECT_URL', value: project.web_url, public: true }, { key: 'CI_PROJECT_URL', value: project.web_url, public: true },
{ key: 'CI_SERVER_NAME', value: 'GitLab', public: true }, { key: 'CI_PIPELINE_ID', value: pipeline.id.to_s, public: true }
{ key: 'CI_SERVER_VERSION', value: Gitlab::VERSION, public: true },
{ key: 'CI_SERVER_REVISION', value: Gitlab::REVISION, public: true }
] ]
end end
...@@ -362,12 +362,13 @@ describe Ci::Build, models: true do ...@@ -362,12 +362,13 @@ describe Ci::Build, models: true do
context 'returns variables in valid order' do context 'returns variables in valid order' do
before do before do
allow(build).to receive(:predefined_variables) { ['predefined'] } allow(build).to receive(:predefined_variables) { ['predefined'] }
allow(build).to receive(:yaml_variables) { ['yaml variables'] } allow(project).to receive(:predefined_variables) { ['project'] }
allow(build).to receive(:project_variables) { ['secure variables'] } allow(pipeline).to receive(:predefined_variables) { ['pipeline'] }
allow(build).to receive(:trigger_variables) { ['trigger variables'] } allow(build).to receive(:yaml_variables) { ['yaml'] }
allow(project).to receive(:secret_variables) { ['secret'] }
end end
it { is_expected.to eq(['predefined', 'yaml variables', 'secure variables', 'trigger variables']) } it { is_expected.to eq(%w[predefined project pipeline yaml secret]) }
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