Commit 7ed779bd authored by Douwe Maan's avatar Douwe Maan

Merge branch 'fix/gb/external-ci-files-use-default-branch' into 'master'

Use default branch SHA to get external files using `include`

Closes #5864

See merge request gitlab-org/gitlab-ee!6325
parents 70e46b03 16b6ee52
---
title: Fix CI/CD pipelines when repository HEAD points to an invalid branch
merge_request: 6325
author:
type: fixed
...@@ -27,7 +27,7 @@ module EE ...@@ -27,7 +27,7 @@ module EE
end end
def process_external_files(config, project, opts) def process_external_files(config, project, opts)
sha = opts.fetch(:sha, project.repository.commit.sha) sha = opts.fetch(:sha) { project.repository.root_ref_sha }
::Gitlab::Ci::External::Processor.new(config, project, sha).perform ::Gitlab::Ci::External::Processor.new(config, project, sha).perform
end end
end end
......
require 'spec_helper' require 'spec_helper'
describe EE::Gitlab::Ci::Config do describe EE::Gitlab::Ci::Config do
let(:config_class) { ::Gitlab::Ci::Config }
let(:project) { create(:project, :repository) } let(:project) { create(:project, :repository) }
let(:remote_location) { 'https://gitlab.com/gitlab-org/gitlab-ce/blob/1234/.gitlab-ci-1.yml' } let(:remote_location) { 'https://gitlab.com/gitlab-org/gitlab-ce/blob/1234/.gitlab-ci-1.yml' }
let(:local_location) { 'ee/spec/fixtures/gitlab/ci/external_files/.gitlab-ci-template-1.yml' }
let(:remote_file_content) do
<<~HEREDOC
variables:
AUTO_DEVOPS_DOMAIN: domain.example.com
POSTGRES_USER: user
POSTGRES_PASSWORD: testing-password
POSTGRES_ENABLED: "true"
POSTGRES_DB: $CI_ENVIRONMENT_SLUG
HEREDOC
end
let(:local_file_content) do
File.read(Rails.root.join(local_location))
end
let(:gitlab_ci_yml) do let(:gitlab_ci_yml) do
<<~HEREDOC <<~HEREDOC
include: include:
- /ee/spec/fixtures/gitlab/ci/external_files/.gitlab-ci-template-1.yml - #{local_location}
- #{remote_location} - #{remote_location}
image: ruby:2.2 image: ruby:2.2
HEREDOC HEREDOC
end end
let(:config) { ::Gitlab::Ci::Config.new(gitlab_ci_yml, { project: project, sha: '12345' }) }
let(:config) do
config_class.new(gitlab_ci_yml, project: project, sha: '12345')
end
before do
WebMock.stub_request(:get, remote_location)
.to_return(body: remote_file_content)
allow(project.repository)
.to receive(:blob_data_at).and_return(local_file_content)
end
context 'when the project does not have a valid license' do context 'when the project does not have a valid license' do
before do before do
allow(project).to receive(:feature_available?).with(:external_files_in_gitlab_ci).and_return(false) allow(project).to receive(:feature_available?)
.with(:external_files_in_gitlab_ci).and_return(false)
end end
it "should raise a ValidationError" do it "should raise a ValidationError" do
...@@ -28,26 +58,13 @@ describe EE::Gitlab::Ci::Config do ...@@ -28,26 +58,13 @@ describe EE::Gitlab::Ci::Config do
end end
context 'when the project has a valid license' do context 'when the project has a valid license' do
let(:remote_file_content) do
<<~HEREDOC
variables:
AUTO_DEVOPS_DOMAIN: domain.example.com
POSTGRES_USER: user
POSTGRES_PASSWORD: testing-password
POSTGRES_ENABLED: "true"
POSTGRES_DB: $CI_ENVIRONMENT_SLUG
HEREDOC
end
let(:local_file_content) { File.read(Rails.root.join('ee/spec/fixtures/gitlab/ci/external_files/.gitlab-ci-template-1.yml')) }
before do before do
allow(project).to receive(:feature_available?).with(:external_files_in_gitlab_ci).and_return(true) allow(project).to receive(:feature_available?)
.with(:external_files_in_gitlab_ci).and_return(true)
end end
context "when gitlab_ci_yml has valid 'include' defined" do context "when gitlab_ci_yml has valid 'include' defined" do
before do before do
allow_any_instance_of(Gitlab::Ci::External::File::Local).to receive(:fetch_local_content).and_return(local_file_content)
WebMock.stub_request(:get, remote_location).to_return(body: remote_file_content)
end end
it 'should return a composed hash' do it 'should return a composed hash' do
...@@ -90,6 +107,25 @@ describe EE::Gitlab::Ci::Config do ...@@ -90,6 +107,25 @@ describe EE::Gitlab::Ci::Config do
end end
end end
describe 'external file version' do
context 'when external local file SHA is defined' do
it 'is using a defined value' do
expect(project.repository).to receive(:blob_data_at)
.with('eeff1122', local_location)
config_class.new(gitlab_ci_yml, project: project, sha: 'eeff1122')
end
end
context 'when external local file SHA is not defined' do
it 'is using latest SHA on the default branch' do
expect(project.repository).to receive(:root_ref_sha)
config_class.new(gitlab_ci_yml, project: project)
end
end
end
context "when both external files and gitlab_ci.yml defined the same key" do context "when both external files and gitlab_ci.yml defined the same key" do
let(:gitlab_ci_yml) do let(:gitlab_ci_yml) do
<<~HEREDOC <<~HEREDOC
...@@ -107,7 +143,6 @@ describe EE::Gitlab::Ci::Config do ...@@ -107,7 +143,6 @@ describe EE::Gitlab::Ci::Config do
end end
it 'should take precedence' do it 'should take precedence' do
WebMock.stub_request(:get, remote_location).to_return(body: remote_file_content)
expect(config.to_hash).to eq({ image: 'ruby:2.2' }) expect(config.to_hash).to eq({ image: 'ruby:2.2' })
end end
end end
...@@ -133,7 +168,6 @@ describe EE::Gitlab::Ci::Config do ...@@ -133,7 +168,6 @@ describe EE::Gitlab::Ci::Config do
end end
it 'should merge the variables dictionaries' do it 'should merge the variables dictionaries' do
WebMock.stub_request(:get, remote_location).to_return(body: remote_file_content)
expect(config.to_hash).to eq({ variables: { A: 'alpha', B: 'beta', C: 'gamma', D: 'delta' } }) expect(config.to_hash).to eq({ variables: { A: 'alpha', B: 'beta', C: 'gamma', D: 'delta' } })
end end
end end
...@@ -160,7 +194,6 @@ describe EE::Gitlab::Ci::Config do ...@@ -160,7 +194,6 @@ describe EE::Gitlab::Ci::Config do
end end
it 'later declarations should take precedence' do it 'later declarations should take precedence' do
WebMock.stub_request(:get, remote_location).to_return(body: remote_file_content)
expect(config.to_hash).to eq({ variables: { A: 'alpha', B: 'beta', C: 'gamma', D: 'delta' } }) expect(config.to_hash).to eq({ variables: { A: 'alpha', B: 'beta', C: 'gamma', D: 'delta' } })
end end
end end
...@@ -186,7 +219,6 @@ describe EE::Gitlab::Ci::Config do ...@@ -186,7 +219,6 @@ describe EE::Gitlab::Ci::Config do
end end
it 'merges the jobs' do it 'merges the jobs' do
WebMock.stub_request(:get, remote_location).to_return(body: remote_file_content)
expect(config.to_hash).to eq({ expect(config.to_hash).to eq({
job1: { job1: {
script: ["echo 'hello from remote file'"], script: ["echo 'hello from remote file'"],
...@@ -212,7 +244,6 @@ describe EE::Gitlab::Ci::Config do ...@@ -212,7 +244,6 @@ describe EE::Gitlab::Ci::Config do
end end
it 'uses the script from the gitlab_ci.yml' do it 'uses the script from the gitlab_ci.yml' do
WebMock.stub_request(:get, remote_location).to_return(body: remote_file_content)
expect(config.to_hash).to eq({ expect(config.to_hash).to eq({
job1: { job1: {
script: ["echo 'hello from main file'"], script: ["echo 'hello from main file'"],
......
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