Commit e4e952d4 authored by Thong Kuah's avatar Thong Kuah

Merge branch 'move-deploy-template-to-rules-syntax' into 'master'

Move deploy template to rules syntax

See merge request gitlab-org/gitlab!31290
parents 59006ab2 7f59d54d
---
title: Move Deploy.gitlab-ci.yml to `rules` syntax
merge_request: 31290
author:
type: changed
...@@ -18,16 +18,14 @@ review: ...@@ -18,16 +18,14 @@ review:
on_stop: stop_review on_stop: stop_review
artifacts: artifacts:
paths: [environment_url.txt] paths: [environment_url.txt]
only: rules:
refs: - if: '$CI_KUBERNETES_ACTIVE == null || $CI_KUBERNETES_ACTIVE == ""'
- branches when: never
- tags - if: '$CI_COMMIT_BRANCH == "master"'
kubernetes: active when: never
except: - if: '$REVIEW_DISABLED'
refs: when: never
- master - if: '$CI_COMMIT_TAG || $CI_COMMIT_BRANCH'
variables:
- $REVIEW_DISABLED
stop_review: stop_review:
extends: .auto-deploy extends: .auto-deploy
...@@ -41,18 +39,16 @@ stop_review: ...@@ -41,18 +39,16 @@ stop_review:
name: review/$CI_COMMIT_REF_NAME name: review/$CI_COMMIT_REF_NAME
action: stop action: stop
dependencies: [] dependencies: []
when: manual
allow_failure: true allow_failure: true
only: rules:
refs: - if: '$CI_KUBERNETES_ACTIVE == null || $CI_KUBERNETES_ACTIVE == ""'
- branches when: never
- tags - if: '$CI_COMMIT_BRANCH == "master"'
kubernetes: active when: never
except: - if: '$REVIEW_DISABLED'
refs: when: never
- master - if: '$CI_COMMIT_TAG || $CI_COMMIT_BRANCH'
variables: when: manual
- $REVIEW_DISABLED
# Staging deploys are disabled by default since # Staging deploys are disabled by default since
# continuous deployment to production is enabled by default # continuous deployment to production is enabled by default
...@@ -73,12 +69,12 @@ staging: ...@@ -73,12 +69,12 @@ staging:
environment: environment:
name: staging name: staging
url: http://$CI_PROJECT_PATH_SLUG-staging.$KUBE_INGRESS_BASE_DOMAIN url: http://$CI_PROJECT_PATH_SLUG-staging.$KUBE_INGRESS_BASE_DOMAIN
only: rules:
refs: - if: '$CI_KUBERNETES_ACTIVE == null || $CI_KUBERNETES_ACTIVE == ""'
- master when: never
kubernetes: active - if: '$CI_COMMIT_BRANCH != "master"'
variables: when: never
- $STAGING_ENABLED - if: '$STAGING_ENABLED'
# Canaries are disabled by default, but if you want them, # Canaries are disabled by default, but if you want them,
# and know what the downsides are, you can enable this by setting # and know what the downsides are, you can enable this by setting
...@@ -97,13 +93,13 @@ canary: ...@@ -97,13 +93,13 @@ canary:
environment: environment:
name: production name: production
url: http://$CI_PROJECT_PATH_SLUG.$KUBE_INGRESS_BASE_DOMAIN url: http://$CI_PROJECT_PATH_SLUG.$KUBE_INGRESS_BASE_DOMAIN
when: manual rules:
only: - if: '$CI_KUBERNETES_ACTIVE == null || $CI_KUBERNETES_ACTIVE == ""'
refs: when: never
- master - if: '$CI_COMMIT_BRANCH != "master"'
kubernetes: active when: never
variables: - if: '$CANARY_ENABLED'
- $CANARY_ENABLED when: manual
.production: &production_template .production: &production_template
extends: .auto-deploy extends: .auto-deploy
...@@ -126,32 +122,33 @@ canary: ...@@ -126,32 +122,33 @@ canary:
production: production:
<<: *production_template <<: *production_template
only: rules:
refs: - if: '$CI_KUBERNETES_ACTIVE == null || $CI_KUBERNETES_ACTIVE == ""'
- master when: never
kubernetes: active - if: '$STAGING_ENABLED'
except: when: never
variables: - if: '$CANARY_ENABLED'
- $STAGING_ENABLED when: never
- $CANARY_ENABLED - if: '$INCREMENTAL_ROLLOUT_ENABLED'
- $INCREMENTAL_ROLLOUT_ENABLED when: never
- $INCREMENTAL_ROLLOUT_MODE - if: '$INCREMENTAL_ROLLOUT_MODE'
when: never
- if: '$CI_COMMIT_BRANCH == "master"'
production_manual: production_manual:
<<: *production_template <<: *production_template
when: manual
allow_failure: false allow_failure: false
only: rules:
refs: - if: '$CI_KUBERNETES_ACTIVE == null || $CI_KUBERNETES_ACTIVE == ""'
- master when: never
kubernetes: active - if: '$INCREMENTAL_ROLLOUT_ENABLED'
variables: when: never
- $STAGING_ENABLED - if: '$INCREMENTAL_ROLLOUT_MODE'
- $CANARY_ENABLED when: never
except: - if: '$CI_COMMIT_BRANCH == "master" && $STAGING_ENABLED'
variables: when: manual
- $INCREMENTAL_ROLLOUT_ENABLED - if: '$CI_COMMIT_BRANCH == "master" && $CANARY_ENABLED'
- $INCREMENTAL_ROLLOUT_MODE when: manual
# This job implements incremental rollout on for every push to `master`. # This job implements incremental rollout on for every push to `master`.
...@@ -176,29 +173,29 @@ production_manual: ...@@ -176,29 +173,29 @@ production_manual:
.manual_rollout_template: &manual_rollout_template .manual_rollout_template: &manual_rollout_template
<<: *rollout_template <<: *rollout_template
stage: production stage: production
when: manual rules:
# This selectors are backward compatible mode with $INCREMENTAL_ROLLOUT_ENABLED (before 11.4) - if: '$CI_KUBERNETES_ACTIVE == null || $CI_KUBERNETES_ACTIVE == ""'
only: when: never
refs: - if: '$INCREMENTAL_ROLLOUT_MODE == "timed"'
- master when: never
kubernetes: active - if: '$CI_COMMIT_BRANCH != "master"'
variables: when: never
- $INCREMENTAL_ROLLOUT_MODE == "manual" # $INCREMENTAL_ROLLOUT_ENABLED is for compamtibilty with pre-GitLab 11.4 syntax
- $INCREMENTAL_ROLLOUT_ENABLED - if: '$INCREMENTAL_ROLLOUT_MODE == "manual" || $INCREMENTAL_ROLLOUT_ENABLED'
except: when: manual
variables:
- $INCREMENTAL_ROLLOUT_MODE == "timed"
.timed_rollout_template: &timed_rollout_template .timed_rollout_template: &timed_rollout_template
<<: *rollout_template <<: *rollout_template
when: delayed rules:
start_in: 5 minutes - if: '$CI_KUBERNETES_ACTIVE == null || $CI_KUBERNETES_ACTIVE == ""'
only: when: never
refs: - if: '$INCREMENTAL_ROLLOUT_MODE == "manual"'
- master when: never
kubernetes: active - if: '$CI_COMMIT_BRANCH != "master"'
variables: when: never
- $INCREMENTAL_ROLLOUT_MODE == "timed" - if: '$INCREMENTAL_ROLLOUT_MODE == "timed"'
when: delayed
start_in: 5 minutes
timed rollout 10%: timed rollout 10%:
<<: *timed_rollout_template <<: *timed_rollout_template
......
# frozen_string_literal: true
require 'spec_helper'
describe 'Jobs/Deploy.gitlab-ci.yml' do
subject(:template) do
<<~YAML
stages:
- test
- review
- staging
- canary
- production
- incremental rollout 10%
- incremental rollout 25%
- incremental rollout 50%
- incremental rollout 100%
- cleanup
include:
- template: Jobs/Deploy.gitlab-ci.yml
placeholder:
script:
- echo "Ensure at least one job to keep pipeline validator happy"
YAML
end
describe 'the created pipeline' do
let(:user) { create(:admin) }
let(:project) { create(:project, :repository) }
let(:default_branch) { 'master' }
let(:pipeline_ref) { default_branch }
let(:service) { Ci::CreatePipelineService.new(project, user, ref: pipeline_ref) }
let(:pipeline) { service.execute!(:push) }
let(:build_names) { pipeline.builds.pluck(:name) }
before do
stub_ci_pipeline_yaml_file(template)
allow_any_instance_of(Ci::BuildScheduleWorker).to receive(:perform).and_return(true)
allow(project).to receive(:default_branch).and_return(default_branch)
end
context 'with no cluster' do
it 'does not create any kubernetes deployment jobs' do
expect(build_names).to eq %w(placeholder)
end
end
context 'with only a disabled cluster' do
let!(:cluster) { create(:cluster, :project, :provided_by_gcp, enabled: false, projects: [project]) }
it 'does not create any kubernetes deployment jobs' do
expect(build_names).to eq %w(placeholder)
end
end
context 'with an active cluster' do
let!(:cluster) { create(:cluster, :project, :provided_by_gcp, projects: [project]) }
context 'on master' do
it 'by default' do
expect(build_names).to include('production')
expect(build_names).not_to include('review')
end
it 'when CANARY_ENABLED' do
create(:ci_variable, project: project, key: 'CANARY_ENABLED', value: 'true')
expect(build_names).to include('production_manual')
expect(build_names).to include('canary')
expect(build_names).not_to include('production')
end
it 'when STAGING_ENABLED' do
create(:ci_variable, project: project, key: 'STAGING_ENABLED', value: 'true')
expect(build_names).to include('production_manual')
expect(build_names).to include('staging')
expect(build_names).not_to include('production')
end
it 'when INCREMENTAL_ROLLOUT_MODE == timed' do
create(:ci_variable, project: project, key: 'INCREMENTAL_ROLLOUT_ENABLED', value: 'true')
create(:ci_variable, project: project, key: 'INCREMENTAL_ROLLOUT_MODE', value: 'timed')
expect(build_names).not_to include('production_manual')
expect(build_names).not_to include('production')
expect(build_names).not_to include(
'rollout 10%',
'rollout 25%',
'rollout 50%',
'rollout 100%'
)
expect(build_names).to include(
'timed rollout 10%',
'timed rollout 25%',
'timed rollout 50%',
'timed rollout 100%'
)
end
it 'when INCREMENTAL_ROLLOUT_ENABLED' do
create(:ci_variable, project: project, key: 'INCREMENTAL_ROLLOUT_ENABLED', value: 'true')
expect(build_names).not_to include('production_manual')
expect(build_names).not_to include('production')
expect(build_names).not_to include(
'timed rollout 10%',
'timed rollout 25%',
'timed rollout 50%',
'timed rollout 100%'
)
expect(build_names).to include(
'rollout 10%',
'rollout 25%',
'rollout 50%',
'rollout 100%'
)
end
it 'when INCREMENTAL_ROLLOUT_MODE == manual' do
create(:ci_variable, project: project, key: 'INCREMENTAL_ROLLOUT_MODE', value: 'manual')
expect(build_names).not_to include('production_manual')
expect(build_names).not_to include('production')
expect(build_names).not_to include(
'timed rollout 10%',
'timed rollout 25%',
'timed rollout 50%',
'timed rollout 100%'
)
expect(build_names).to include(
'rollout 10%',
'rollout 25%',
'rollout 50%',
'rollout 100%'
)
end
end
shared_examples_for 'review app deployment' do
it 'creates the review and stop_review jobs but no production jobs' do
expect(build_names).to include('review')
expect(build_names).to include('stop_review')
expect(build_names).not_to include('production')
expect(build_names).not_to include('production_manual')
expect(build_names).not_to include('staging')
expect(build_names).not_to include('canary')
expect(build_names).not_to include('timed rollout 10%')
expect(build_names).not_to include('timed rollout 25%')
expect(build_names).not_to include('timed rollout 50%')
expect(build_names).not_to include('timed rollout 100%')
expect(build_names).not_to include('rollout 10%')
expect(build_names).not_to include('rollout 25%')
expect(build_names).not_to include('rollout 50%')
expect(build_names).not_to include('rollout 100%')
end
it 'does not include review when REVIEW_DISABLED' do
create(:ci_variable, project: project, key: 'REVIEW_DISABLED', value: 'true')
expect(build_names).not_to include('review')
expect(build_names).not_to include('stop_review')
end
end
context 'on branch' do
let(:pipeline_ref) { 'feature' }
before do
allow_any_instance_of(Gitlab::Ci::Pipeline::Chain::Validate::Repository).to receive(:perform!).and_return(true)
end
it_behaves_like 'review app deployment'
context 'when INCREMENTAL_ROLLOUT_ENABLED' do
before do
create(:ci_variable, project: project, key: 'INCREMENTAL_ROLLOUT_ENABLED', value: 'true')
end
it_behaves_like 'review app deployment'
end
context 'when INCREMENTAL_ROLLOUT_MODE == "timed"' do
before do
create(:ci_variable, project: project, key: 'INCREMENTAL_ROLLOUT_MODE', value: 'timed')
end
it_behaves_like 'review app deployment'
end
context 'when INCREMENTAL_ROLLOUT_MODE == "manual"' do
before do
create(:ci_variable, project: project, key: 'INCREMENTAL_ROLLOUT_MODE', value: 'manual')
end
it_behaves_like 'review app deployment'
end
end
context 'on tag' do
let(:pipeline_ref) { 'v1.0.0' }
it_behaves_like 'review app deployment'
end
context 'on merge request' do
let(:service) { MergeRequests::CreatePipelineService.new(project, user) }
let(:merge_request) { create(:merge_request, :simple, source_project: project) }
let(:pipeline) { service.execute(merge_request) }
it 'has no jobs' do
expect(pipeline).to be_merge_request_event
expect(build_names).to be_empty
end
end
end
end
end
...@@ -40,11 +40,7 @@ describe 'Auto-DevOps.gitlab-ci.yml' do ...@@ -40,11 +40,7 @@ describe 'Auto-DevOps.gitlab-ci.yml' do
end end
context 'when the project has an active cluster' do context 'when the project has an active cluster' do
let(:cluster) { create(:cluster, :project, :provided_by_gcp, projects: [project]) } let!(:cluster) { create(:cluster, :project, :provided_by_gcp, projects: [project]) }
before do
allow(cluster).to receive(:active?).and_return(true)
end
describe 'deployment-related builds' do describe 'deployment-related builds' do
context 'on default branch' do context 'on default branch' 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