Commit bf99d5e3 authored by Thong Kuah's avatar Thong Kuah

Merge branch 'auto-devops-default-branch-deploy' into 'master'

Fix Auto DevOps deploys that use a default branch that's not `master`

See merge request gitlab-org/gitlab!53280
parents fb130c7e 43844925
---
title: Fix Auto DevOps deploys that use a default branch that's not named 'master'
merge_request: 53280
author: Mitchell Cash @MitchellCash
type: fixed
...@@ -23,7 +23,7 @@ review: ...@@ -23,7 +23,7 @@ review:
rules: rules:
- if: '$CI_KUBERNETES_ACTIVE == null || $CI_KUBERNETES_ACTIVE == ""' - if: '$CI_KUBERNETES_ACTIVE == null || $CI_KUBERNETES_ACTIVE == ""'
when: never when: never
- if: '$CI_COMMIT_BRANCH == "master"' - if: '$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH'
when: never when: never
- if: '$REVIEW_DISABLED' - if: '$REVIEW_DISABLED'
when: never when: never
...@@ -44,7 +44,7 @@ stop_review: ...@@ -44,7 +44,7 @@ stop_review:
rules: rules:
- if: '$CI_KUBERNETES_ACTIVE == null || $CI_KUBERNETES_ACTIVE == ""' - if: '$CI_KUBERNETES_ACTIVE == null || $CI_KUBERNETES_ACTIVE == ""'
when: never when: never
- if: '$CI_COMMIT_BRANCH == "master"' - if: '$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH'
when: never when: never
- if: '$REVIEW_DISABLED' - if: '$REVIEW_DISABLED'
when: never when: never
...@@ -73,7 +73,7 @@ staging: ...@@ -73,7 +73,7 @@ staging:
rules: rules:
- if: '$CI_KUBERNETES_ACTIVE == null || $CI_KUBERNETES_ACTIVE == ""' - if: '$CI_KUBERNETES_ACTIVE == null || $CI_KUBERNETES_ACTIVE == ""'
when: never when: never
- if: '$CI_COMMIT_BRANCH != "master"' - if: '$CI_COMMIT_BRANCH != $CI_DEFAULT_BRANCH'
when: never when: never
- if: '$STAGING_ENABLED' - if: '$STAGING_ENABLED'
...@@ -98,7 +98,7 @@ canary: ...@@ -98,7 +98,7 @@ canary:
rules: rules:
- if: '$CI_KUBERNETES_ACTIVE == null || $CI_KUBERNETES_ACTIVE == ""' - if: '$CI_KUBERNETES_ACTIVE == null || $CI_KUBERNETES_ACTIVE == ""'
when: never when: never
- if: '$CI_COMMIT_BRANCH != "master"' - if: '$CI_COMMIT_BRANCH != $CI_DEFAULT_BRANCH'
when: never when: never
- if: '$CANARY_ENABLED' - if: '$CANARY_ENABLED'
when: manual when: manual
...@@ -136,7 +136,7 @@ production: ...@@ -136,7 +136,7 @@ production:
when: never when: never
- if: '$INCREMENTAL_ROLLOUT_MODE' - if: '$INCREMENTAL_ROLLOUT_MODE'
when: never when: never
- if: '$CI_COMMIT_BRANCH == "master"' - if: '$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH'
production_manual: production_manual:
<<: *production_template <<: *production_template
...@@ -148,9 +148,9 @@ production_manual: ...@@ -148,9 +148,9 @@ production_manual:
when: never when: never
- if: '$INCREMENTAL_ROLLOUT_MODE' - if: '$INCREMENTAL_ROLLOUT_MODE'
when: never when: never
- if: '$CI_COMMIT_BRANCH == "master" && $STAGING_ENABLED' - if: '$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH && $STAGING_ENABLED'
when: manual when: manual
- if: '$CI_COMMIT_BRANCH == "master" && $CANARY_ENABLED' - if: '$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH && $CANARY_ENABLED'
when: manual when: manual
# This job implements incremental rollout on for every push to `master`. # This job implements incremental rollout on for every push to `master`.
...@@ -184,7 +184,7 @@ production_manual: ...@@ -184,7 +184,7 @@ production_manual:
when: never when: never
- if: '$INCREMENTAL_ROLLOUT_MODE == "timed"' - if: '$INCREMENTAL_ROLLOUT_MODE == "timed"'
when: never when: never
- if: '$CI_COMMIT_BRANCH != "master"' - if: '$CI_COMMIT_BRANCH != $CI_DEFAULT_BRANCH'
when: never when: never
# $INCREMENTAL_ROLLOUT_ENABLED is for compatibility with pre-GitLab 11.4 syntax # $INCREMENTAL_ROLLOUT_ENABLED is for compatibility with pre-GitLab 11.4 syntax
- if: '$INCREMENTAL_ROLLOUT_MODE == "manual" || $INCREMENTAL_ROLLOUT_ENABLED' - if: '$INCREMENTAL_ROLLOUT_MODE == "manual" || $INCREMENTAL_ROLLOUT_ENABLED'
...@@ -197,7 +197,7 @@ production_manual: ...@@ -197,7 +197,7 @@ production_manual:
when: never when: never
- if: '$INCREMENTAL_ROLLOUT_MODE == "manual"' - if: '$INCREMENTAL_ROLLOUT_MODE == "manual"'
when: never when: never
- if: '$CI_COMMIT_BRANCH != "master"' - if: '$CI_COMMIT_BRANCH != $CI_DEFAULT_BRANCH'
when: never when: never
- if: '$INCREMENTAL_ROLLOUT_MODE == "timed"' - if: '$INCREMENTAL_ROLLOUT_MODE == "timed"'
when: delayed when: delayed
......
...@@ -23,7 +23,7 @@ review: ...@@ -23,7 +23,7 @@ review:
rules: rules:
- if: '$CI_KUBERNETES_ACTIVE == null || $CI_KUBERNETES_ACTIVE == ""' - if: '$CI_KUBERNETES_ACTIVE == null || $CI_KUBERNETES_ACTIVE == ""'
when: never when: never
- if: '$CI_COMMIT_BRANCH == "master"' - if: '$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH'
when: never when: never
- if: '$REVIEW_DISABLED' - if: '$REVIEW_DISABLED'
when: never when: never
...@@ -44,7 +44,7 @@ stop_review: ...@@ -44,7 +44,7 @@ stop_review:
rules: rules:
- if: '$CI_KUBERNETES_ACTIVE == null || $CI_KUBERNETES_ACTIVE == ""' - if: '$CI_KUBERNETES_ACTIVE == null || $CI_KUBERNETES_ACTIVE == ""'
when: never when: never
- if: '$CI_COMMIT_BRANCH == "master"' - if: '$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH'
when: never when: never
- if: '$REVIEW_DISABLED' - if: '$REVIEW_DISABLED'
when: never when: never
...@@ -73,7 +73,7 @@ staging: ...@@ -73,7 +73,7 @@ staging:
rules: rules:
- if: '$CI_KUBERNETES_ACTIVE == null || $CI_KUBERNETES_ACTIVE == ""' - if: '$CI_KUBERNETES_ACTIVE == null || $CI_KUBERNETES_ACTIVE == ""'
when: never when: never
- if: '$CI_COMMIT_BRANCH != "master"' - if: '$CI_COMMIT_BRANCH != $CI_DEFAULT_BRANCH'
when: never when: never
- if: '$STAGING_ENABLED' - if: '$STAGING_ENABLED'
...@@ -98,7 +98,7 @@ canary: ...@@ -98,7 +98,7 @@ canary:
rules: rules:
- if: '$CI_KUBERNETES_ACTIVE == null || $CI_KUBERNETES_ACTIVE == ""' - if: '$CI_KUBERNETES_ACTIVE == null || $CI_KUBERNETES_ACTIVE == ""'
when: never when: never
- if: '$CI_COMMIT_BRANCH != "master"' - if: '$CI_COMMIT_BRANCH != $CI_DEFAULT_BRANCH'
when: never when: never
- if: '$CANARY_ENABLED' - if: '$CANARY_ENABLED'
when: manual when: manual
...@@ -135,7 +135,7 @@ production: ...@@ -135,7 +135,7 @@ production:
when: never when: never
- if: '$INCREMENTAL_ROLLOUT_MODE' - if: '$INCREMENTAL_ROLLOUT_MODE'
when: never when: never
- if: '$CI_COMMIT_BRANCH == "master"' - if: '$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH'
production_manual: production_manual:
<<: *production_template <<: *production_template
...@@ -147,9 +147,9 @@ production_manual: ...@@ -147,9 +147,9 @@ production_manual:
when: never when: never
- if: '$INCREMENTAL_ROLLOUT_MODE' - if: '$INCREMENTAL_ROLLOUT_MODE'
when: never when: never
- if: '$CI_COMMIT_BRANCH == "master" && $STAGING_ENABLED' - if: '$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH && $STAGING_ENABLED'
when: manual when: manual
- if: '$CI_COMMIT_BRANCH == "master" && $CANARY_ENABLED' - if: '$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH && $CANARY_ENABLED'
when: manual when: manual
# This job implements incremental rollout on for every push to `master`. # This job implements incremental rollout on for every push to `master`.
...@@ -181,7 +181,7 @@ production_manual: ...@@ -181,7 +181,7 @@ production_manual:
when: never when: never
- if: '$INCREMENTAL_ROLLOUT_MODE == "timed"' - if: '$INCREMENTAL_ROLLOUT_MODE == "timed"'
when: never when: never
- if: '$CI_COMMIT_BRANCH != "master"' - if: '$CI_COMMIT_BRANCH != $CI_DEFAULT_BRANCH'
when: never when: never
# $INCREMENTAL_ROLLOUT_ENABLED is for compatibility with pre-GitLab 11.4 syntax # $INCREMENTAL_ROLLOUT_ENABLED is for compatibility with pre-GitLab 11.4 syntax
- if: '$INCREMENTAL_ROLLOUT_MODE == "manual" || $INCREMENTAL_ROLLOUT_ENABLED' - if: '$INCREMENTAL_ROLLOUT_MODE == "manual" || $INCREMENTAL_ROLLOUT_ENABLED'
...@@ -194,7 +194,7 @@ production_manual: ...@@ -194,7 +194,7 @@ production_manual:
when: never when: never
- if: '$INCREMENTAL_ROLLOUT_MODE == "manual"' - if: '$INCREMENTAL_ROLLOUT_MODE == "manual"'
when: never when: never
- if: '$CI_COMMIT_BRANCH != "master"' - if: '$CI_COMMIT_BRANCH != $CI_DEFAULT_BRANCH'
when: never when: never
- if: '$INCREMENTAL_ROLLOUT_MODE == "timed"' - if: '$INCREMENTAL_ROLLOUT_MODE == "timed"'
when: delayed when: delayed
......
...@@ -20,7 +20,7 @@ review_ec2: ...@@ -20,7 +20,7 @@ review_ec2:
when: never when: never
- if: '$REVIEW_DISABLED' - if: '$REVIEW_DISABLED'
when: never when: never
- if: '$CI_COMMIT_BRANCH == "master"' - if: '$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH'
when: never when: never
- if: '$CI_COMMIT_TAG || $CI_COMMIT_BRANCH' - if: '$CI_COMMIT_TAG || $CI_COMMIT_BRANCH'
...@@ -34,6 +34,6 @@ production_ec2: ...@@ -34,6 +34,6 @@ production_ec2:
when: never when: never
- if: '$CI_KUBERNETES_ACTIVE' - if: '$CI_KUBERNETES_ACTIVE'
when: never when: never
- if: '$CI_COMMIT_BRANCH != "master"' - if: '$CI_COMMIT_BRANCH != $CI_DEFAULT_BRANCH'
when: never when: never
- if: '$CI_COMMIT_TAG || $CI_COMMIT_BRANCH' - if: '$CI_COMMIT_TAG || $CI_COMMIT_BRANCH'
...@@ -46,7 +46,7 @@ review_ecs: ...@@ -46,7 +46,7 @@ review_ecs:
when: never when: never
- if: '$REVIEW_DISABLED' - if: '$REVIEW_DISABLED'
when: never when: never
- if: '$CI_COMMIT_BRANCH == "master"' - if: '$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH'
when: never when: never
- if: '$CI_COMMIT_TAG || $CI_COMMIT_BRANCH' - if: '$CI_COMMIT_TAG || $CI_COMMIT_BRANCH'
...@@ -62,7 +62,7 @@ stop_review_ecs: ...@@ -62,7 +62,7 @@ stop_review_ecs:
when: never when: never
- if: '$REVIEW_DISABLED' - if: '$REVIEW_DISABLED'
when: never when: never
- if: '$CI_COMMIT_BRANCH == "master"' - if: '$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH'
when: never when: never
- if: '$CI_COMMIT_TAG || $CI_COMMIT_BRANCH' - if: '$CI_COMMIT_TAG || $CI_COMMIT_BRANCH'
when: manual when: manual
...@@ -81,7 +81,7 @@ review_fargate: ...@@ -81,7 +81,7 @@ review_fargate:
when: never when: never
- if: '$REVIEW_DISABLED' - if: '$REVIEW_DISABLED'
when: never when: never
- if: '$CI_COMMIT_BRANCH == "master"' - if: '$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH'
when: never when: never
- if: '$CI_COMMIT_TAG || $CI_COMMIT_BRANCH' - if: '$CI_COMMIT_TAG || $CI_COMMIT_BRANCH'
...@@ -97,7 +97,7 @@ stop_review_fargate: ...@@ -97,7 +97,7 @@ stop_review_fargate:
when: never when: never
- if: '$REVIEW_DISABLED' - if: '$REVIEW_DISABLED'
when: never when: never
- if: '$CI_COMMIT_BRANCH == "master"' - if: '$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH'
when: never when: never
- if: '$CI_COMMIT_TAG || $CI_COMMIT_BRANCH' - if: '$CI_COMMIT_TAG || $CI_COMMIT_BRANCH'
when: manual when: manual
...@@ -109,7 +109,7 @@ production_ecs: ...@@ -109,7 +109,7 @@ production_ecs:
when: never when: never
- if: '$CI_KUBERNETES_ACTIVE' - if: '$CI_KUBERNETES_ACTIVE'
when: never when: never
- if: '$CI_COMMIT_BRANCH != "master"' - if: '$CI_COMMIT_BRANCH != $CI_DEFAULT_BRANCH'
when: never when: never
- if: '$CI_COMMIT_TAG || $CI_COMMIT_BRANCH' - if: '$CI_COMMIT_TAG || $CI_COMMIT_BRANCH'
...@@ -120,6 +120,6 @@ production_fargate: ...@@ -120,6 +120,6 @@ production_fargate:
when: never when: never
- if: '$CI_KUBERNETES_ACTIVE' - if: '$CI_KUBERNETES_ACTIVE'
when: never when: never
- if: '$CI_COMMIT_BRANCH != "master"' - if: '$CI_COMMIT_BRANCH != $CI_DEFAULT_BRANCH'
when: never when: never
- if: '$CI_COMMIT_TAG || $CI_COMMIT_BRANCH' - if: '$CI_COMMIT_TAG || $CI_COMMIT_BRANCH'
...@@ -194,7 +194,7 @@ FactoryBot.define do ...@@ -194,7 +194,7 @@ FactoryBot.define do
filename, filename,
content, content,
message: "Automatically created file #{filename}", message: "Automatically created file #{filename}",
branch_name: 'master' branch_name: project.default_branch_or_master
) )
end end
end end
......
...@@ -3,252 +3,260 @@ ...@@ -3,252 +3,260 @@
require 'spec_helper' require 'spec_helper'
RSpec.describe 'Auto-DevOps.gitlab-ci.yml' do RSpec.describe 'Auto-DevOps.gitlab-ci.yml' do
using RSpec::Parameterized::TableSyntax
subject(:template) { Gitlab::Template::GitlabCiYmlTemplate.find('Auto-DevOps') } subject(:template) { Gitlab::Template::GitlabCiYmlTemplate.find('Auto-DevOps') }
describe 'the created pipeline' do where(:default_branch) do
let(:default_branch) { 'master' } %w[master main]
let(:pipeline_branch) { default_branch } end
let(:project) { create(:project, :auto_devops, :custom_repo, files: { 'README.md' => '' }) }
let(:user) { project.owner }
let(:service) { Ci::CreatePipelineService.new(project, user, ref: pipeline_branch ) }
let(:pipeline) { service.execute!(:push) }
let(:build_names) { pipeline.builds.pluck(:name) }
before do
stub_ci_pipeline_yaml_file(template.content)
allow_any_instance_of(Ci::BuildScheduleWorker).to receive(:perform).and_return(true)
allow(project).to receive(:default_branch).and_return(default_branch)
end
shared_examples 'no Kubernetes deployment job' do with_them do
it 'does not create any Kubernetes deployment-related builds' do describe 'the created pipeline' do
expect(build_names).not_to include('production') let(:pipeline_branch) { default_branch }
expect(build_names).not_to include('production_manual') let(:project) { create(:project, :auto_devops, :custom_repo, files: { 'README.md' => '' }) }
expect(build_names).not_to include('staging') let(:user) { project.owner }
expect(build_names).not_to include('canary') let(:service) { Ci::CreatePipelineService.new(project, user, ref: pipeline_branch ) }
expect(build_names).not_to include('review') let(:pipeline) { service.execute!(:push) }
expect(build_names).not_to include(a_string_matching(/rollout \d+%/)) let(:build_names) { pipeline.builds.pluck(:name) }
end
end
it 'creates a build and a test job' do before do
expect(build_names).to include('build', 'test') stub_application_setting(default_branch_name: default_branch)
end stub_ci_pipeline_yaml_file(template.content)
allow_any_instance_of(Ci::BuildScheduleWorker).to receive(:perform).and_return(true)
end
context 'when the project is set for deployment to AWS' do shared_examples 'no Kubernetes deployment job' do
let(:platform_value) { 'ECS' } it 'does not create any Kubernetes deployment-related builds' do
let(:review_prod_build_names) { build_names.select {|n| n.include?('review') || n.include?('production')} } 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('review')
expect(build_names).not_to include(a_string_matching(/rollout \d+%/))
end
end
before do it 'creates a build and a test job' do
create(:ci_variable, project: project, key: 'AUTO_DEVOPS_PLATFORM_TARGET', value: platform_value) expect(build_names).to include('build', 'test')
end end
shared_examples 'no ECS job when AUTO_DEVOPS_PLATFORM_TARGET is not present' do |job_name| context 'when the project is set for deployment to AWS' do
context 'when AUTO_DEVOPS_PLATFORM_TARGET is nil' do let(:platform_value) { 'ECS' }
let(:platform_value) { nil } let(:review_prod_build_names) { build_names.select {|n| n.include?('review') || n.include?('production')} }
it 'does not trigger the job' do before do
expect(build_names).not_to include(job_name) create(:ci_variable, project: project, key: 'AUTO_DEVOPS_PLATFORM_TARGET', value: platform_value)
end
end end
context 'when AUTO_DEVOPS_PLATFORM_TARGET is empty' do shared_examples 'no ECS job when AUTO_DEVOPS_PLATFORM_TARGET is not present' do |job_name|
let(:platform_value) { '' } context 'when AUTO_DEVOPS_PLATFORM_TARGET is nil' do
let(:platform_value) { nil }
it 'does not trigger the job' do it 'does not trigger the job' do
expect(build_names).not_to include(job_name) expect(build_names).not_to include(job_name)
end
end end
end
end
it_behaves_like 'no Kubernetes deployment job' context 'when AUTO_DEVOPS_PLATFORM_TARGET is empty' do
let(:platform_value) { '' }
it_behaves_like 'no ECS job when AUTO_DEVOPS_PLATFORM_TARGET is not present' do it 'does not trigger the job' do
let(:job_name) { 'production_ecs' } expect(build_names).not_to include(job_name)
end end
end
end
it 'creates an ECS deployment job for production only' do it_behaves_like 'no Kubernetes deployment job'
expect(review_prod_build_names).to contain_exactly('production_ecs')
end
context 'with FARGATE as a launch type' do it_behaves_like 'no ECS job when AUTO_DEVOPS_PLATFORM_TARGET is not present' do
let(:platform_value) { 'FARGATE' } let(:job_name) { 'production_ecs' }
end
it 'creates a FARGATE deployment job for production only' do it 'creates an ECS deployment job for production only' do
expect(review_prod_build_names).to contain_exactly('production_fargate') expect(review_prod_build_names).to contain_exactly('production_ecs')
end end
end
context 'and we are not on the default branch' do context 'with FARGATE as a launch type' do
let(:platform_value) { 'ECS' } let(:platform_value) { 'FARGATE' }
let(:pipeline_branch) { 'patch-1' }
before do it 'creates a FARGATE deployment job for production only' do
project.repository.create_branch(pipeline_branch) expect(review_prod_build_names).to contain_exactly('production_fargate')
end
end end
%w(review_ecs review_fargate).each do |job| context 'and we are not on the default branch' do
it_behaves_like 'no ECS job when AUTO_DEVOPS_PLATFORM_TARGET is not present' do let(:platform_value) { 'ECS' }
let(:job_name) { job } let(:pipeline_branch) { 'patch-1' }
before do
project.repository.create_branch(pipeline_branch, default_branch)
end end
end
it 'creates an ECS deployment job for review only' do %w(review_ecs review_fargate).each do |job|
expect(review_prod_build_names).to contain_exactly('review_ecs', 'stop_review_ecs') it_behaves_like 'no ECS job when AUTO_DEVOPS_PLATFORM_TARGET is not present' do
end let(:job_name) { job }
end
end
context 'with FARGATE as a launch type' do it 'creates an ECS deployment job for review only' do
let(:platform_value) { 'FARGATE' } expect(review_prod_build_names).to contain_exactly('review_ecs', 'stop_review_ecs')
end
context 'with FARGATE as a launch type' do
let(:platform_value) { 'FARGATE' }
it 'creates an FARGATE deployment job for review only' do it 'creates an FARGATE deployment job for review only' do
expect(review_prod_build_names).to contain_exactly('review_fargate', 'stop_review_fargate') expect(review_prod_build_names).to contain_exactly('review_fargate', 'stop_review_fargate')
end
end end
end end
end
context 'and when the project has an active cluster' do context 'and 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 before do
allow(cluster).to receive(:active?).and_return(true) allow(cluster).to receive(:active?).and_return(true)
end end
context 'on default branch' do context 'on default branch' do
it 'triggers the deployment to Kubernetes, not to ECS' do it 'triggers the deployment to Kubernetes, not to ECS' do
expect(build_names).not_to include('review') expect(build_names).not_to include('review')
expect(build_names).to include('production') expect(build_names).to include('production')
expect(build_names).not_to include('production_ecs') expect(build_names).not_to include('production_ecs')
expect(build_names).not_to include('review_ecs') expect(build_names).not_to include('review_ecs')
end
end end
end end
end
context 'when the platform target is EC2' do context 'when the platform target is EC2' do
let(:platform_value) { 'EC2' } let(:platform_value) { 'EC2' }
it 'contains the build_artifact job, not the build job' do it 'contains the build_artifact job, not the build job' do
expect(build_names).to include('build_artifact') expect(build_names).to include('build_artifact')
expect(build_names).not_to include('build') expect(build_names).not_to include('build')
end
end end
end end
end
context 'when the project has no active cluster' do
it 'only creates a build and a test stage' do
expect(pipeline.stages_names).to eq(%w(build test))
end
it_behaves_like 'no Kubernetes deployment job' context 'when the project has no active cluster' do
end it 'only creates a build and a test stage' do
expect(pipeline.stages_names).to eq(%w(build test))
end
context 'when the project has an active cluster' do it_behaves_like 'no Kubernetes deployment job'
let!(:cluster) { create(:cluster, :project, :provided_by_gcp, projects: [project]) } end
describe 'deployment-related builds' do
context 'on default branch' do
it 'does not include rollout jobs besides production' do
expect(build_names).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('review')
expect(build_names).not_to include(a_string_matching(/rollout \d+%/))
end
context 'when STAGING_ENABLED=1' do context 'when the project has an active cluster' do
before do let!(:cluster) { create(:cluster, :project, :provided_by_gcp, projects: [project]) }
create(:ci_variable, project: project, key: 'STAGING_ENABLED', value: '1')
end
it 'includes a staging job and a production_manual job' do describe 'deployment-related builds' do
expect(build_names).not_to include('production') context 'on default branch' do
expect(build_names).to include('production_manual') it 'does not include rollout jobs besides production' do
expect(build_names).to include('staging') expect(build_names).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('canary')
expect(build_names).not_to include('review') expect(build_names).not_to include('review')
expect(build_names).not_to include(a_string_matching(/rollout \d+%/)) expect(build_names).not_to include(a_string_matching(/rollout \d+%/))
end end
context 'when STAGING_ENABLED=1' do
before do
create(:ci_variable, project: project, key: 'STAGING_ENABLED', value: '1')
end
it 'includes a staging job and a production_manual job' do
expect(build_names).not_to include('production')
expect(build_names).to include('production_manual')
expect(build_names).to include('staging')
expect(build_names).not_to include('canary')
expect(build_names).not_to include('review')
expect(build_names).not_to include(a_string_matching(/rollout \d+%/))
end
end
context 'when CANARY_ENABLED=1' do
before do
create(:ci_variable, project: project, key: 'CANARY_ENABLED', value: '1')
end
it 'includes a canary job and a production_manual job' do
expect(build_names).not_to include('production')
expect(build_names).to include('production_manual')
expect(build_names).not_to include('staging')
expect(build_names).to include('canary')
expect(build_names).not_to include('review')
expect(build_names).not_to include(a_string_matching(/rollout \d+%/))
end
end
end end
context 'when CANARY_ENABLED=1' do context 'outside of default branch' do
let(:pipeline_branch) { 'patch-1' }
before do before do
create(:ci_variable, project: project, key: 'CANARY_ENABLED', value: '1') project.repository.create_branch(pipeline_branch, default_branch)
end end
it 'includes a canary job and a production_manual job' do it 'does not include rollout jobs besides review' do
expect(build_names).not_to include('production') expect(build_names).not_to include('production')
expect(build_names).to include('production_manual') expect(build_names).not_to include('production_manual')
expect(build_names).not_to include('staging') expect(build_names).not_to include('staging')
expect(build_names).to include('canary') expect(build_names).not_to include('canary')
expect(build_names).not_to include('review') expect(build_names).to include('review')
expect(build_names).not_to include(a_string_matching(/rollout \d+%/)) expect(build_names).not_to include(a_string_matching(/rollout \d+%/))
end end
end end
end end
context 'outside of default branch' do
let(:pipeline_branch) { 'patch-1' }
before do
project.repository.create_branch(pipeline_branch)
end
it 'does not include rollout jobs besides review' do
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).to include('review')
expect(build_names).not_to include(a_string_matching(/rollout \d+%/))
end
end
end end
end end
end
describe 'build-pack detection' do describe 'build-pack detection' do
using RSpec::Parameterized::TableSyntax using RSpec::Parameterized::TableSyntax
where(:case_name, :files, :variables, :include_build_names, :not_include_build_names) do where(:case_name, :files, :variables, :include_build_names, :not_include_build_names) do
'No match' | { 'README.md' => '' } | {} | %w() | %w(build test) 'No match' | { 'README.md' => '' } | {} | %w() | %w(build test)
'Buildpack' | { 'README.md' => '' } | { 'BUILDPACK_URL' => 'http://example.com' } | %w(build test) | %w() 'Buildpack' | { 'README.md' => '' } | { 'BUILDPACK_URL' => 'http://example.com' } | %w(build test) | %w()
'Explicit set' | { 'README.md' => '' } | { 'AUTO_DEVOPS_EXPLICITLY_ENABLED' => '1' } | %w(build test) | %w() 'Explicit set' | { 'README.md' => '' } | { 'AUTO_DEVOPS_EXPLICITLY_ENABLED' => '1' } | %w(build test) | %w()
'Explicit unset' | { 'README.md' => '' } | { 'AUTO_DEVOPS_EXPLICITLY_ENABLED' => '0' } | %w() | %w(build test) 'Explicit unset' | { 'README.md' => '' } | { 'AUTO_DEVOPS_EXPLICITLY_ENABLED' => '0' } | %w() | %w(build test)
'DOCKERFILE_PATH' | { 'README.md' => '' } | { 'DOCKERFILE_PATH' => 'Docker.file' } | %w(build test) | %w() 'DOCKERFILE_PATH' | { 'README.md' => '' } | { 'DOCKERFILE_PATH' => 'Docker.file' } | %w(build test) | %w()
'Dockerfile' | { 'Dockerfile' => '' } | {} | %w(build test) | %w() 'Dockerfile' | { 'Dockerfile' => '' } | {} | %w(build test) | %w()
'Clojure' | { 'project.clj' => '' } | {} | %w(build test) | %w() 'Clojure' | { 'project.clj' => '' } | {} | %w(build test) | %w()
'Go modules' | { 'go.mod' => '' } | {} | %w(build test) | %w() 'Go modules' | { 'go.mod' => '' } | {} | %w(build test) | %w()
'Go gb' | { 'src/gitlab.com/gopackage.go' => '' } | {} | %w(build test) | %w() 'Go gb' | { 'src/gitlab.com/gopackage.go' => '' } | {} | %w(build test) | %w()
'Gradle' | { 'gradlew' => '' } | {} | %w(build test) | %w() 'Gradle' | { 'gradlew' => '' } | {} | %w(build test) | %w()
'Java' | { 'pom.xml' => '' } | {} | %w(build test) | %w() 'Java' | { 'pom.xml' => '' } | {} | %w(build test) | %w()
'Multi-buildpack' | { '.buildpacks' => '' } | {} | %w(build test) | %w() 'Multi-buildpack' | { '.buildpacks' => '' } | {} | %w(build test) | %w()
'NodeJS' | { 'package.json' => '' } | {} | %w(build test) | %w() 'NodeJS' | { 'package.json' => '' } | {} | %w(build test) | %w()
'PHP' | { 'composer.json' => '' } | {} | %w(build test) | %w() 'PHP' | { 'composer.json' => '' } | {} | %w(build test) | %w()
'Play' | { 'conf/application.conf' => '' } | {} | %w(build test) | %w() 'Play' | { 'conf/application.conf' => '' } | {} | %w(build test) | %w()
'Python' | { 'Pipfile' => '' } | {} | %w(build test) | %w() 'Python' | { 'Pipfile' => '' } | {} | %w(build test) | %w()
'Ruby' | { 'Gemfile' => '' } | {} | %w(build test) | %w() 'Ruby' | { 'Gemfile' => '' } | {} | %w(build test) | %w()
'Scala' | { 'build.sbt' => '' } | {} | %w(build test) | %w() 'Scala' | { 'build.sbt' => '' } | {} | %w(build test) | %w()
'Static' | { '.static' => '' } | {} | %w(build test) | %w() 'Static' | { '.static' => '' } | {} | %w(build test) | %w()
end end
with_them do with_them do
let(:project) { create(:project, :custom_repo, files: files) } let(:project) { create(:project, :custom_repo, files: files) }
let(:user) { project.owner } let(:user) { project.owner }
let(:service) { Ci::CreatePipelineService.new(project, user, ref: 'master' ) } let(:service) { Ci::CreatePipelineService.new(project, user, ref: default_branch ) }
let(:pipeline) { service.execute(:push) } let(:pipeline) { service.execute(:push) }
let(:build_names) { pipeline.builds.pluck(:name) } let(:build_names) { pipeline.builds.pluck(:name) }
before do before do
stub_ci_pipeline_yaml_file(template.content) stub_application_setting(default_branch_name: default_branch)
allow_any_instance_of(Ci::BuildScheduleWorker).to receive(:perform).and_return(true) stub_ci_pipeline_yaml_file(template.content)
variables.each do |(key, value)| allow_any_instance_of(Ci::BuildScheduleWorker).to receive(:perform).and_return(true)
create(:ci_variable, project: project, key: key, value: value) variables.each do |(key, value)|
create(:ci_variable, project: project, key: key, value: value)
end
end end
end
it 'creates a pipeline with the expected jobs' do it 'creates a pipeline with the expected jobs' do
expect(build_names).to include(*include_build_names) expect(build_names).to include(*include_build_names)
expect(build_names).not_to include(*not_include_build_names) expect(build_names).not_to include(*not_include_build_names)
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