diff --git a/app/models/ci/build.rb b/app/models/ci/build.rb index c9091c19705..a59ff731954 100644 --- a/app/models/ci/build.rb +++ b/app/models/ci/build.rb @@ -750,6 +750,9 @@ module Ci variables.append(key: 'GITLAB_FEATURES', value: project.licensed_features.join(',')) variables.append(key: 'CI_SERVER_NAME', value: 'GitLab') variables.append(key: 'CI_SERVER_VERSION', value: Gitlab::VERSION) + variables.append(key: 'CI_SERVER_VERSION_MAJOR', value: gitlab_version_info.major.to_s) + variables.append(key: 'CI_SERVER_VERSION_MINOR', value: gitlab_version_info.minor.to_s) + variables.append(key: 'CI_SERVER_VERSION_PATCH', value: gitlab_version_info.patch.to_s) variables.append(key: 'CI_SERVER_REVISION', value: Gitlab.revision) variables.append(key: 'CI_JOB_NAME', value: name) variables.append(key: 'CI_JOB_STAGE', value: stage) @@ -764,6 +767,10 @@ module Ci end end + def gitlab_version_info + @gitlab_version_info ||= Gitlab::VersionInfo.parse(Gitlab::VERSION) + end + def legacy_variables Gitlab::Ci::Variables::Collection.new.tap do |variables| variables.append(key: 'CI_BUILD_REF', value: sha) diff --git a/changelogs/unreleased/46050_add_new_ci_predefined_variables_for_gitlab_version.yml b/changelogs/unreleased/46050_add_new_ci_predefined_variables_for_gitlab_version.yml new file mode 100644 index 00000000000..dd230d5f35e --- /dev/null +++ b/changelogs/unreleased/46050_add_new_ci_predefined_variables_for_gitlab_version.yml @@ -0,0 +1,5 @@ +--- +title: Add GitLab version components to CI environment variables +merge_request: 21853 +author: +type: added diff --git a/doc/ci/variables/README.md b/doc/ci/variables/README.md index f11949da64e..93e7ca7bd89 100644 --- a/doc/ci/variables/README.md +++ b/doc/ci/variables/README.md @@ -94,6 +94,9 @@ future GitLab releases.** | **CI_SERVER_NAME** | all | all | The name of CI server that is used to coordinate jobs | | **CI_SERVER_REVISION** | all | all | GitLab revision that is used to schedule jobs | | **CI_SERVER_VERSION** | all | all | GitLab version that is used to schedule jobs | +| **CI_SERVER_VERSION_MAJOR** | 11.4 | all | GitLab version major component | +| **CI_SERVER_VERSION_MINOR** | 11.4 | all | GitLab version minor component | +| **CI_SERVER_VERSION_PATCH** | 11.4 | all | GitLab version patch component | | **CI_SHARED_ENVIRONMENT** | all | 10.1 | Marks that the job is executed in a shared environment (something that is persisted across CI invocations like `shell` or `ssh` executor). If the environment is shared, it is set to true, otherwise it is not defined at all. | | **GET_SOURCES_ATTEMPTS** | 8.15 | 1.9 | Number of attempts to fetch sources running a job | | **GITLAB_CI** | all | all | Mark that job is executed in GitLab CI environment | @@ -323,6 +326,12 @@ Running on runner-8a2f473d-project-1796893-concurrent-0 via runner-8a2f473d-mach ++ CI_SERVER_NAME='GitLab CI' ++ export CI_SERVER_VERSION= ++ CI_SERVER_VERSION= +++ export CI_SERVER_VERSION_MAJOR= +++ CI_SERVER_VERSION_MAJOR= +++ export CI_SERVER_VERSION_MINOR= +++ CI_SERVER_VERSION_MINOR= +++ export CI_SERVER_VERSION_PATCH= +++ CI_SERVER_VERSION_PATCH= ++ export CI_SERVER_REVISION= ++ CI_SERVER_REVISION= ++ export GITLAB_CI=true @@ -468,6 +477,9 @@ export CI_SERVER="yes" export CI_SERVER_NAME="GitLab" export CI_SERVER_REVISION="70606bf" export CI_SERVER_VERSION="8.9.0" +export CI_SERVER_VERSION_MAJOR="8" +export CI_SERVER_VERSION_MINOR="9" +export CI_SERVER_VERSION_PATCH="0" export GITLAB_USER_ID="42" export GITLAB_USER_EMAIL="user@example.com" export CI_REGISTRY_USER="gitlab-ci-token" diff --git a/spec/lib/gitlab/version_info_spec.rb b/spec/lib/gitlab/version_info_spec.rb index c8a1e433d59..30035c79e58 100644 --- a/spec/lib/gitlab/version_info_spec.rb +++ b/spec/lib/gitlab/version_info_spec.rb @@ -57,6 +57,9 @@ describe 'Gitlab::VersionInfo' do context 'parse' do it { expect(Gitlab::VersionInfo.parse("1.0.0")).to eq(@v1_0_0) } it { expect(Gitlab::VersionInfo.parse("1.0.0.1")).to eq(@v1_0_0) } + it { expect(Gitlab::VersionInfo.parse("1.0.0-ee")).to eq(@v1_0_0) } + it { expect(Gitlab::VersionInfo.parse("1.0.0-rc1")).to eq(@v1_0_0) } + it { expect(Gitlab::VersionInfo.parse("1.0.0-rc1-ee")).to eq(@v1_0_0) } it { expect(Gitlab::VersionInfo.parse("git 1.0.0b1")).to eq(@v1_0_0) } it { expect(Gitlab::VersionInfo.parse("git 1.0b1")).not_to be_valid } end diff --git a/spec/models/ci/build_spec.rb b/spec/models/ci/build_spec.rb index 61fd079def2..70d9af2f74d 100644 --- a/spec/models/ci/build_spec.rb +++ b/spec/models/ci/build_spec.rb @@ -1691,6 +1691,7 @@ describe Ci::Build do describe '#variables' do let(:container_registry_enabled) { false } + let(:gitlab_version_info) { Gitlab::VersionInfo.parse(Gitlab::VERSION) } let(:predefined_variables) do [ { key: 'CI_PIPELINE_ID', value: pipeline.id.to_s, public: true }, @@ -1708,6 +1709,9 @@ describe Ci::Build do { key: 'GITLAB_FEATURES', value: project.licensed_features.join(','), public: true }, { key: 'CI_SERVER_NAME', value: 'GitLab', public: true }, { key: 'CI_SERVER_VERSION', value: Gitlab::VERSION, public: true }, + { key: 'CI_SERVER_VERSION_MAJOR', value: gitlab_version_info.major.to_s, public: true }, + { key: 'CI_SERVER_VERSION_MINOR', value: gitlab_version_info.minor.to_s, public: true }, + { key: 'CI_SERVER_VERSION_PATCH', value: gitlab_version_info.patch.to_s, public: true }, { key: 'CI_SERVER_REVISION', value: Gitlab.revision, public: true }, { key: 'CI_JOB_NAME', value: 'test', public: true }, { key: 'CI_JOB_STAGE', value: 'test', public: true },