Commit 2029d83b authored by Dylan Griffith's avatar Dylan Griffith

Merge branch 'ci_env_action_variable' into 'master'

Add $CI_ENVIRONMENT_ACTION as a pre-defined environment variable

See merge request gitlab-org/gitlab!58786
parents 7fc53a67 735431c5
...@@ -595,6 +595,8 @@ module Ci ...@@ -595,6 +595,8 @@ module Ci
variables.concat(persisted_environment.predefined_variables) variables.concat(persisted_environment.predefined_variables)
variables.append(key: 'CI_ENVIRONMENT_ACTION', value: environment_action)
# Here we're passing unexpanded environment_url for runner to expand, # Here we're passing unexpanded environment_url for runner to expand,
# and we need to make sure that CI_ENVIRONMENT_NAME and # and we need to make sure that CI_ENVIRONMENT_NAME and
# CI_ENVIRONMENT_SLUG so on are available for the URL be expanded. # CI_ENVIRONMENT_SLUG so on are available for the URL be expanded.
......
...@@ -52,6 +52,7 @@ There are also [Kubernetes-specific deployment variables](../../user/project/clu ...@@ -52,6 +52,7 @@ There are also [Kubernetes-specific deployment variables](../../user/project/clu
| `CI_ENVIRONMENT_NAME` | 8.15 | all | The name of the environment for this job. Available if [`environment:name`](../yaml/README.md#environmentname) is set. | | `CI_ENVIRONMENT_NAME` | 8.15 | all | The name of the environment for this job. Available if [`environment:name`](../yaml/README.md#environmentname) is set. |
| `CI_ENVIRONMENT_SLUG` | 8.15 | all | The simplified version of the environment name, suitable for inclusion in DNS, URLs, Kubernetes labels, and so on. Available if [`environment:name`](../yaml/README.md#environmentname) is set. The slug is [truncated to 24 characters](https://gitlab.com/gitlab-org/gitlab/-/issues/20941). | | `CI_ENVIRONMENT_SLUG` | 8.15 | all | The simplified version of the environment name, suitable for inclusion in DNS, URLs, Kubernetes labels, and so on. Available if [`environment:name`](../yaml/README.md#environmentname) is set. The slug is [truncated to 24 characters](https://gitlab.com/gitlab-org/gitlab/-/issues/20941). |
| `CI_ENVIRONMENT_URL` | 9.3 | all | The URL of the environment for this job. Available if [`environment:url`](../yaml/README.md#environmenturl) is set. | | `CI_ENVIRONMENT_URL` | 9.3 | all | The URL of the environment for this job. Available if [`environment:url`](../yaml/README.md#environmenturl) is set. |
| `CI_ENVIRONMENT_ACTION` | 13.11 | all | The action annotation specified for this job's environment. Available if [`environment:action`](../yaml/README.md#environmentaction) is set. Can be `start`, `prepare`, or `stop`. |
| `CI_HAS_OPEN_REQUIREMENTS` | 13.1 | all | Only available if the pipeline's project has an open [requirement](../../user/project/requirements/index.md). `true` when available. | | `CI_HAS_OPEN_REQUIREMENTS` | 13.1 | all | Only available if the pipeline's project has an open [requirement](../../user/project/requirements/index.md). `true` when available. |
| `CI_JOB_ID` | 9.0 | all | The internal ID of the job, unique across all jobs in the GitLab instance. | | `CI_JOB_ID` | 9.0 | all | The internal ID of the job, unique across all jobs in the GitLab instance. |
| `CI_JOB_IMAGE` | 12.9 | 12.9 | The name of the Docker image running the job. | | `CI_JOB_IMAGE` | 12.9 | 12.9 | The name of the Docker image running the job. |
......
...@@ -2640,6 +2640,17 @@ RSpec.describe Ci::Build do ...@@ -2640,6 +2640,17 @@ RSpec.describe Ci::Build do
it { is_expected.to be_instance_of(Gitlab::Ci::Variables::Collection) } it { is_expected.to be_instance_of(Gitlab::Ci::Variables::Collection) }
it { expect(subject.to_runner_variables).to eq(predefined_variables) } it { expect(subject.to_runner_variables).to eq(predefined_variables) }
it 'excludes variables that require an environment or user' do
environment_based_variables_collection = subject.filter do |variable|
%w[
YAML_VARIABLE CI_ENVIRONMENT_NAME CI_ENVIRONMENT_SLUG
CI_ENVIRONMENT_ACTION CI_ENVIRONMENT_URL
].include?(variable[:key])
end
expect(environment_based_variables_collection).to be_empty
end
context 'when ci_job_jwt feature flag is disabled' do context 'when ci_job_jwt feature flag is disabled' do
before do before do
stub_feature_flags(ci_job_jwt: false) stub_feature_flags(ci_job_jwt: false)
...@@ -2709,7 +2720,7 @@ RSpec.describe Ci::Build do ...@@ -2709,7 +2720,7 @@ RSpec.describe Ci::Build do
let(:expected_variables) do let(:expected_variables) do
predefined_variables.map { |variable| variable.fetch(:key) } + predefined_variables.map { |variable| variable.fetch(:key) } +
%w[YAML_VARIABLE CI_ENVIRONMENT_NAME CI_ENVIRONMENT_SLUG %w[YAML_VARIABLE CI_ENVIRONMENT_NAME CI_ENVIRONMENT_SLUG
CI_ENVIRONMENT_URL] CI_ENVIRONMENT_ACTION CI_ENVIRONMENT_URL]
end end
before do before do
...@@ -2727,6 +2738,50 @@ RSpec.describe Ci::Build do ...@@ -2727,6 +2738,50 @@ RSpec.describe Ci::Build do
expect(received_variables).to eq expected_variables expect(received_variables).to eq expected_variables
end end
describe 'CI_ENVIRONMENT_ACTION' do
let(:enviroment_action_variable) { subject.find { |variable| variable[:key] == 'CI_ENVIRONMENT_ACTION' } }
shared_examples 'defaults value' do
it 'value matches start' do
expect(enviroment_action_variable[:value]).to eq('start')
end
end
it_behaves_like 'defaults value'
context 'when options is set' do
before do
build.update!(options: options)
end
context 'when options is empty' do
let(:options) { {} }
it_behaves_like 'defaults value'
end
context 'when options is nil' do
let(:options) { nil }
it_behaves_like 'defaults value'
end
context 'when options environment is specified' do
let(:options) { { environment: {} } }
it_behaves_like 'defaults value'
end
context 'when options environment action specified' do
let(:options) { { environment: { action: 'stop' } } }
it 'matches the specified action' do
expect(enviroment_action_variable[:value]).to eq('stop')
end
end
end
end
end end
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