Commit 33595eb3 authored by Yorick Peterse's avatar Yorick Peterse

Merge branch 'rs-auto-deploy-naming' into 'master'

Introduce AutoDeploy::Naming class

See merge request !601
parents 40c5c340 62e20932
......@@ -25,9 +25,9 @@ end
namespace :auto_deploy do
desc "Prepare for auto-deploy by creating branches from the latest green commit on gitlab-ee and omnibus-gitlab"
task :prepare do
pipeline_id = ENV['CI_PIPELINE_IID']
abort('CI_PIPELINE_IID must be set for this rake task'.colorize(:red)) unless pipeline_id
ReleaseTools::Services::AutoDeployBranchService.new(pipeline_id).create_auto_deploy_branches!
ReleaseTools::Services::AutoDeployBranchService
.new(ReleaseTools::AutoDeploy::Naming.branch)
.create_branches!
end
end
......
......@@ -32,6 +32,7 @@ Dotenv.load
$LOAD_PATH.unshift(__dir__)
require 'release_tools/auto_deploy/naming'
require 'release_tools/branch'
require 'release_tools/branch_creation'
require 'release_tools/version'
......
# frozen_string_literal: true
module ReleaseTools
module AutoDeploy
class Naming
BRANCH_FORMAT = '%<major>d-%<minor>d-auto-deploy-%<pipeline_id>07d'
TAG_FORMAT = '%<major>d.%<minor>d.%<pipeline_id>d+%<ee_ref>.11s.%<omnibus_ref>.11s'
def self.branch
new.branch
end
def self.tag(ee_ref:, omnibus_ref:)
new.tag(ee_ref: ee_ref, omnibus_ref: omnibus_ref)
end
def initialize
@pipeline_id = ENV.fetch('CI_PIPELINE_IID') do |key|
raise ArgumentError, "`#{key}` must be set in order to proceed"
end
end
def branch
format(
BRANCH_FORMAT,
major: version.first,
minor: version.last,
pipeline_id: @pipeline_id
)
end
def tag(ee_ref:, omnibus_ref:)
format(
TAG_FORMAT,
major: version.first,
minor: version.last,
pipeline_id: @pipeline_id,
ee_ref: ee_ref,
omnibus_ref: omnibus_ref
)
end
def version
@version ||=
begin
milestone = ReleaseTools::GitlabClient
.current_milestone
.title
unless milestone.match?(/\A\d+\.\d+\z/)
raise ArgumentError, "Invalid version from milestone: #{milestone}"
end
milestone.split('.')
end
end
end
end
end
......@@ -4,14 +4,16 @@ module ReleaseTools
module Services
class AutoDeployBranchService
include BranchCreation
PIPELINE_ID_PADDING = 7
CI_VAR_AUTO_DEPLOY = 'AUTO_DEPLOY_BRANCH'
def initialize(pipeline_id)
@pipeline_id = pipeline_id.to_s.rjust(PIPELINE_ID_PADDING, '0')
attr_reader :branch_name
def initialize(branch_name)
@branch_name = branch_name
end
def create_auto_deploy_branches!
def create_branches!
# Find passing commits before creating branches
ref_deployer = latest_successful_ref(Project::Deployer, gitlab_ops_client)
ref_ce = latest_successful_ref(Project::GitlabCe)
......@@ -28,14 +30,6 @@ module ReleaseTools
private
def version
@version ||= gitlab_client.current_milestone.title.tr('.', '-')
end
def branch_name
"#{version}-auto-deploy-#{@pipeline_id}"
end
def update_auto_deploy_ci
gitlab_client.update_variable(Project::ReleaseTools.path, CI_VAR_AUTO_DEPLOY, branch_name)
rescue Gitlab::Error::NotFound
......
# frozen_string_literal: true
require 'spec_helper'
describe ReleaseTools::AutoDeploy::Naming do
def with_pipeline(iid)
ClimateControl.modify(CI_PIPELINE_IID: iid) { yield }
end
describe 'initialize' do
it 'fails when CI_PIPELINE_IID is unset' do
with_pipeline(nil) do
expect { described_class.new }
.to raise_error(/must be set in order to proceed/)
end
end
end
describe '.branch' do
it 'returns a branch name in the appropriate format' do
allow(ReleaseTools::GitlabClient).to receive(:current_milestone)
.and_return(double(title: '4.2'))
with_pipeline('1234') do
expect(described_class.branch).to eq('4-2-auto-deploy-0001234')
end
end
end
describe '.tag' do
it 'returns a tag name in the appropriate format' do
allow(ReleaseTools::GitlabClient).to receive(:current_milestone)
.and_return(double(title: '4.2'))
ee_ref = SecureRandom.hex(20)
ob_ref = SecureRandom.hex(20)
with_pipeline('1234') do
expect(described_class.tag(ee_ref: ee_ref, omnibus_ref: ob_ref))
.to eq("4.2.1234+#{ee_ref[0...11]}.#{ob_ref[0...11]}")
end
end
end
describe '#version' do
it 'raises an error when the milestone format is unexpected' do
allow(ReleaseTools::GitlabClient).to receive(:current_milestone)
.and_return(double(title: 'Backlog'))
with_pipeline('1234') do
expect { described_class.new.version }
.to raise_error(/Invalid version from milestone/)
end
end
end
end
......@@ -7,16 +7,17 @@ describe ReleaseTools::Services::AutoDeployBranchService do
let(:internal_client_ops) { spy('ReleaseTools::GitlabOpsClient') }
let(:branch_commit) { double(latest_successful: double(id: '1234')) }
subject(:service) { described_class.new('9000') }
subject(:service) { described_class.new('branch-name') }
before do
allow(service).to receive(:gitlab_client).and_return(internal_client)
allow(service).to receive(:gitlab_ops_client).and_return(internal_client_ops)
end
describe '#create_auto_deploy_branches!', :silence_stdout do
describe '#create_branches!', :silence_stdout do
it 'creates auto-deploy branches for gitlab-ee and gitlab-ce' do
branch_name = '11-10-auto-deploy-0009000'
branch_name = 'branch-name'
expect(ReleaseTools::Commits).to receive(:new).and_return(branch_commit).exactly(4).times
expect(internal_client_ops).to receive(:create_branch).with(
branch_name,
......@@ -43,8 +44,9 @@ describe ReleaseTools::Services::AutoDeployBranchService do
'AUTO_DEPLOY_BRANCH',
branch_name
)
without_dry_run do
service.create_auto_deploy_branches!
service.create_branches!
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