Add support to append to existing builder

Signed-off-by: Balasankar 'Balu' C <balasankar@gitlab.com>
This commit is contained in:
Balasankar 'Balu' C 2024-04-22 14:45:01 +05:30
parent af62ecd234
commit 9c04a0ed2a
No known key found for this signature in database
GPG Key ID: B77D2E2E23735427
2 changed files with 37 additions and 10 deletions

View File

@ -62,13 +62,18 @@ class DockerHelper < DockerOperations
end
def create_builder
cleanup_existing_builder
puts "Creating docker builder instance"
buildx_command = %W[docker buildx create --bootstrap --use --name omnibus-gitlab-builder-#{Build::Info::CI.commit_ref_slug}]
if builders_exist?
puts "omnibus-gitlab-builder-#{Build::Info::CI.commit_ref_slug} instance already exists. Not attempting to create again but instead appending to it."
buildx_command << "--append"
end
# TODO: For multi-arch builds, use Kubernetes driver for builder instance
_, stdout_stderr, status = Open3.popen2e(*%W[docker buildx create --bootstrap --use --name omnibus-gitlab-builder-#{Build::Info::CI.commit_ref_slug}])
raise "Using Kubernetes cluster for buildx is not currently supported." if Gitlab::Util.get_env('DISABLE_BUILDX_CLUSTER') && Gitlab::Util.get_env('DISABLE_BUILDX_CLUSTER') != 'true'
_, stdout_stderr, status = Open3.popen2e(*buildx_command)
return if status.value.success?
puts "Creating builder instance failed."
@ -76,10 +81,15 @@ class DockerHelper < DockerOperations
raise
end
def cleanup_existing_builder
puts "Cleaning any existing builder instances."
def builders_exist?
_, _, status = Open3.popen2e(*%W[docker buildx ls | grep omnibus-gitlab-builder-#{Build::Info::CI.commit_ref_slug}])
unless status.value.success?
status.value.success?
end
def cleanup_existing_builder
puts "Cleaning existing builder instances."
unless builders_exist?
puts "omnibus-gitlab-builder-#{Build::Info::CI.commit_ref_slug} instance not found. Not attempting to clean."
return
end

View File

@ -106,7 +106,6 @@ RSpec.describe DockerHelper do
describe '.create_builder' do
before do
allow(described_class).to receive(:cleanup_existing_builder).and_return(true)
stub_env_var('CI_COMMIT_REF_SLUG', 'feature-branch')
stdout_stderr_mock = double(gets: nil)
@ -114,10 +113,28 @@ RSpec.describe DockerHelper do
allow(Open3).to receive(:popen2e).with("docker", "buildx", "create", any_args).and_return([nil, stdout_stderr_mock, status_mock])
end
it 'calls docker buildx create command with correct arguments' do
expect(Open3).to receive(:popen2e).with(*%w[docker buildx create --bootstrap --use --name omnibus-gitlab-builder-feature-branch])
context 'when builder already exists' do
before do
allow(described_class).to receive(:builders_exist?).and_return(true)
end
described_class.create_builder
it 'calls docker buildx create command with append argument' do
expect(Open3).to receive(:popen2e).with(*%w[docker buildx create --bootstrap --use --name omnibus-gitlab-builder-feature-branch --append])
described_class.create_builder
end
end
context 'when builder does not already exist' do
before do
allow(described_class).to receive(:builders_exist?).and_return(false)
end
it 'calls docker buildx create command without append argument' do
expect(Open3).to receive(:popen2e).with(*%w[docker buildx create --bootstrap --use --name omnibus-gitlab-builder-feature-branch])
described_class.create_builder
end
end
end