Commit 7c211396 authored by Michael Kozono's avatar Michael Kozono

Merge branch 'env-auto-stop' into 'master'

Resolve "Set Auto-Stop Environment Expiry Period when an environment is created"

See merge request gitlab-org/gitlab!47239
parents 5051ff2a 5b308d31
---
name: environment_auto_stop_start_on_create
introduced_by_url: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/47239
rollout_issue_url: https://gitlab.com/gitlab-org/gitlab/-/issues/281449
milestone: '13.6'
type: development
group: group::release
default_enabled: false
......@@ -2558,7 +2558,7 @@ In the example above, if the configuration is not identical:
> [Introduced](https://gitlab.com/gitlab-org/gitlab/-/issues/20956) in GitLab 12.8.
The `auto_stop_in` keyword is for specifying life period of the environment,
The `auto_stop_in` keyword is for specifying the lifetime of the environment,
that when expired, GitLab automatically stops them.
For example,
......@@ -2571,8 +2571,8 @@ review_app:
auto_stop_in: 1 day
```
When `review_app` job is executed and a review app is created, a life period of
the environment is set to `1 day`.
When the environment for `review_app` is created, the environment's lifetime is set to `1 day`.
Every time the review app is deployed, that lifetime is also reset to `1 day`.
For more information, see
[the environments auto-stop documentation](../environments/index.md#environments-auto-stop)
......
......@@ -12,12 +12,23 @@ module Gitlab
end
def to_resource
job.project.environments
.safe_find_or_create_by(name: expanded_environment_name)
environments.safe_find_or_create_by(name: expanded_environment_name) do |environment|
environment.auto_stop_in = auto_stop_in
end
end
private
def environments
job.project.environments
end
def auto_stop_in
if Feature.enabled?(:environment_auto_stop_start_on_create)
job.environment_auto_stop_in
end
end
def expanded_environment_name
job.expanded_environment_name
end
......
......@@ -16,20 +16,37 @@ RSpec.describe Gitlab::Ci::Pipeline::Seed::Environment do
subject { seed.to_resource }
shared_examples_for 'returning a correct environment' do
let(:expected_auto_stop_in_seconds) do
if expected_auto_stop_in
ChronicDuration.parse(expected_auto_stop_in).seconds
end
end
it 'returns a persisted environment object' do
expect { subject }.to change { Environment.count }.by(1)
freeze_time do
expect { subject }.to change { Environment.count }.by(1)
expect(subject).to be_a(Environment)
expect(subject).to be_persisted
expect(subject.project).to eq(project)
expect(subject.name).to eq(expected_environment_name)
expect(subject).to be_a(Environment)
expect(subject).to be_persisted
expect(subject.project).to eq(project)
expect(subject.name).to eq(expected_environment_name)
expect(subject.auto_stop_in).to eq(expected_auto_stop_in_seconds)
end
end
context 'when environment has already existed' do
let!(:environment) { create(:environment, project: project, name: expected_environment_name) }
let!(:environment) do
create(:environment,
project: project,
name: expected_environment_name
).tap do |env|
env.auto_stop_in = expected_auto_stop_in
end
end
it 'returns the existing environment object' do
expect { subject }.not_to change { Environment.count }
expect { subject }.not_to change { environment.auto_stop_at }
expect(subject).to be_persisted
expect(subject).to eq(environment)
......@@ -37,9 +54,10 @@ RSpec.describe Gitlab::Ci::Pipeline::Seed::Environment do
end
end
context 'when job has environment attribute' do
context 'when job has environment name attribute' do
let(:environment_name) { 'production' }
let(:expected_environment_name) { 'production' }
let(:expected_auto_stop_in) { nil }
let(:attributes) do
{
......@@ -49,11 +67,41 @@ RSpec.describe Gitlab::Ci::Pipeline::Seed::Environment do
end
it_behaves_like 'returning a correct environment'
context 'and job environment also has an auto_stop_in attribute' do
let(:environment_auto_stop_in) { '5 minutes' }
let(:expected_auto_stop_in) { '5 minutes' }
let(:attributes) do
{
environment: environment_name,
options: {
environment: {
name: environment_name,
auto_stop_in: environment_auto_stop_in
}
}
}
end
it_behaves_like 'returning a correct environment'
context 'but the environment auto_stop_in on create flag is disabled' do
let(:expected_auto_stop_in) { nil }
before do
stub_feature_flags(environment_auto_stop_start_on_create: false)
end
it_behaves_like 'returning a correct environment'
end
end
end
context 'when job starts a review app' do
let(:environment_name) { 'review/$CI_COMMIT_REF_NAME' }
let(:expected_environment_name) { "review/#{job.ref}" }
let(:expected_auto_stop_in) { nil }
let(:attributes) do
{
......@@ -68,6 +116,7 @@ RSpec.describe Gitlab::Ci::Pipeline::Seed::Environment do
context 'when job stops a review app' do
let(:environment_name) { 'review/$CI_COMMIT_REF_NAME' }
let(:expected_environment_name) { "review/#{job.ref}" }
let(:expected_auto_stop_in) { nil }
let(:attributes) do
{
......
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