diff --git a/Gemfile.lock b/Gemfile.lock index 01bc3fe..219179e 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -40,7 +40,7 @@ GEM reline (>= 0.3.8) json (2.6.3) language_server-protocol (3.17.0.3) - minitest (5.19.0) + minitest (5.25.4) mutex_m (0.1.2) parallel (1.23.0) parser (3.2.2.4) diff --git a/bin/docs-preview-annotate b/bin/docs-preview-annotate index c24a3be..343b441 100755 --- a/bin/docs-preview-annotate +++ b/bin/docs-preview-annotate @@ -27,12 +27,13 @@ end json = JSON.parse(response.body) result = json["result"].first +url = result["aliases"]&.first || result["url"] plan = <<~PLAN #### :writing_hand: rails/docs-preview: -* :link: API -* :link: Guides +* :link: API +* :link: Guides PLAN puts plan diff --git a/lib/buildkite/config/build_context.rb b/lib/buildkite/config/build_context.rb index c5f7760..0caf78f 100644 --- a/lib/buildkite/config/build_context.rb +++ b/lib/buildkite/config/build_context.rb @@ -117,6 +117,18 @@ def pull_request ([ENV["BUILDKITE_PULL_REQUEST"]] - ["false"]).first end + def compute_type + ENV["BUILDKITE_COMPUTE_TYPE"] || "self-hosted" + end + + def self_hosted? + compute_type == "self-hosted" + end + + def hosted? + !self_hosted? + end + def standard_queues [nil, "default", "builder"] end @@ -189,8 +201,20 @@ def min_ruby Gem::Version.new($1 || "2.0") end + def registry + if hosted? + ENV["REGISTRY"] + else + "973266071021.dkr.ecr.us-east-1.amazonaws.com" + end + end + + def image_name + "#{"#{build_queue}-" unless standard_queues.include?(build_queue)}builds" + end + def remote_image_base - "973266071021.dkr.ecr.us-east-1.amazonaws.com/#{"#{build_queue}-" unless standard_queues.include?(build_queue)}builds" + [registry, image_name].join("/") end end end diff --git a/lib/buildkite/config/docker_build.rb b/lib/buildkite/config/docker_build.rb index b43f630..ecbde66 100644 --- a/lib/buildkite/config/docker_build.rb +++ b/lib/buildkite/config/docker_build.rb @@ -30,11 +30,16 @@ def cache_from(build_context) end def build_push(build_context) - [ - build_context.local_branch =~ /:/ ? - build_context.image_name_for("pr-#{build_context.pull_request}") : - build_context.image_name_for("br-#{build_context.local_branch}"), - ] + if build_context.hosted? + build_context.image_name_for(build_context.build_id, prefix: nil) + else + [ + build_context.local_branch =~ /:/ ? + build_context.image_name_for("pr-#{build_context.pull_request}") : + build_context.image_name_for("br-#{build_context.local_branch}"), + build_context.image_name_for(build_context.build_id) + ] + end end end @@ -66,23 +71,35 @@ def builder(ruby) compressed: ".buildkite.tgz" } - plugin :docker_compose, { - build: "base", - config: ".buildkite/docker-compose.yml", - env: %w[PRE_STEPS RACK], - "image-name" => build_context.ruby.image_name_for(build_context.build_id), - "cache-from" => cache_from(build_context), - push: build_push(build_context), - "image-repository" => build_context.image_base, - } + if build_context.hosted? + command <<~COMMAND.squish + docker build --push + --build-arg RUBY_IMAGE=#{build_context.ruby.ruby_image} + --tag #{build_push(build_context)} + --file .buildkite/Dockerfile . + COMMAND + else + plugin :docker_compose, { + build: "base", + config: ".buildkite/docker-compose.yml", + env: %w[PRE_STEPS RACK], + "cache-from" => cache_from(build_context), + push: build_push(build_context), + } + end - env({ + env_opts = { BUNDLER: build_context.bundler, RUBYGEMS: build_context.rubygems, - RUBY_IMAGE: build_context.ruby.ruby_image, encrypted_0fb9444d0374_key: nil, encrypted_0fb9444d0374_iv: nil - }) + } + + if build_context.self_hosted? + env_opts[:RUBY_IMAGE] = build_context.ruby.ruby_image + end + + env(env_opts) timeout_in_minutes 15 diff --git a/lib/buildkite/config/rake_command.rb b/lib/buildkite/config/rake_command.rb index 7c78a9f..a315b63 100644 --- a/lib/buildkite/config/rake_command.rb +++ b/lib/buildkite/config/rake_command.rb @@ -33,7 +33,7 @@ def build_env(build_context, pre_steps, env) env end - def install_plugins(service = "default", env = nil, dir = ".") + def install_plugins(service = "default", env = nil, dir = ".", build_context:) plugin :artifacts, { download: ".dockerignore" } @@ -49,14 +49,25 @@ def install_plugins(service = "default", env = nil, dir = ".") compressed: ".buildkite.tgz" } - plugin :docker_compose, { + if build_context.mainline + plugin :secrets, { + env: "main_env" + } + end + + compose_opts = { "env" => env, "run" => service, - "pull" => service, - "pull-retries" => 3, "config" => ".buildkite/docker-compose.yml", "shell" => ["runner", *dir], - }.compact + "tty" => "true", + } + + if build_context.self_hosted? + compose_opts["cli-version"] = "1" + end + + plugin :docker_compose, compose_opts.compact end end @@ -73,7 +84,7 @@ def bundle(command, label:, env: nil) depends_on "docker-image-#{build_context.ruby.image_key}" command command - install_plugins + install_plugins(build_context: build_context) env build_env(build_context, nil, env) @@ -99,7 +110,7 @@ def rake(dir, task: "test", label: nil, service: "default", pre_steps: nil, env: depends_on "docker-image-#{build_context.ruby.image_key}" command "rake #{task}" - install_plugins(service, %w[PRE_STEPS RACK], dir) + install_plugins(service, %w[PRE_STEPS RACK], dir, build_context: build_context) env build_env(build_context, pre_steps, env) diff --git a/pipelines/buildkite-config/initial.yml b/pipelines/buildkite-config/initial.yml index 0158f13..95dec14 100644 --- a/pipelines/buildkite-config/initial.yml +++ b/pipelines/buildkite-config/initial.yml @@ -57,6 +57,7 @@ steps: -v "$$PWD":/app:ro -w /app -v "$$PWD/tmp":/app/tmp:rw -e BUNDLE_FROZEN + -e BUILDKITE_BUILD_ID ruby:latest ./bin/pipeline-annotate - command: | @@ -80,6 +81,7 @@ steps: -v "$$PWD/tmp":/app/tmp:rw -e RAILS_CI_NIGHTLY -e BUNDLE_FROZEN + -e BUILDKITE_BUILD_ID ruby:latest ./bin/pipeline-annotate diff --git a/pipelines/docs-preview/pipeline.rb b/pipelines/docs-preview/pipeline.rb index b18c358..59b2d23 100644 --- a/pipelines/docs-preview/pipeline.rb +++ b/pipelines/docs-preview/pipeline.rb @@ -3,9 +3,11 @@ Buildkite::Builder.pipeline do require "buildkite_config" use Buildkite::Config::BuildContext + use Buildkite::Config::DockerBuild plugin :docker, "docker#v5.10.0" plugin :artifacts, "artifacts#v1.9.3" + plugin :secrets, "cluster-secrets#v1.0.0" build_context = context.extensions.find(Buildkite::Config::BuildContext) build_context.ruby = Buildkite::Config::RubyConfig.new(prefix: "ruby:", version: Gem::Version.new("3.3")) @@ -22,13 +24,16 @@ next end + builder build_context.ruby + command do label "build", emoji: :rails + depends_on "docker-image-#{build_context.ruby.image_key}" key "build" command "bundle install && bundle exec rake preview_docs" timeout_in_minutes 15 plugin :docker, { - image: build_context.image_name_for("br-main", prefix: nil), + image: build_context.image_name_for(build_context.build_id, prefix: nil), environment: [ "BUILDKITE_BRANCH", "BUILDKITE_BUILD_CREATOR", @@ -50,6 +55,9 @@ key "deploy" depends_on "build" timeout_in_minutes 15 + plugin :secrets, { + env: "docs_preview_env" + } plugin :docker, { environment: [ "BUILDKITE_BRANCH", @@ -83,6 +91,9 @@ download: ".buildkite/bin/docs-preview-annotate", compressed: ".buildkite.tgz" } + plugin :secrets, { + env: "docs_preview_env" + } command "sh -c \"$$ANNOTATE_COMMAND\" | buildkite-agent annotate --style info" # CLOUDFLARE_API_TOKEN is used to fetch preview URL from latest deployment env "ANNOTATE_COMMAND" => <<~ANNOTATE.gsub(/[[:space:]]+/, " ").strip diff --git a/pipelines/rails-ci/initial.yml b/pipelines/rails-ci/initial.yml index 299245d..2453f55 100644 --- a/pipelines/rails-ci/initial.yml +++ b/pipelines/rails-ci/initial.yml @@ -4,6 +4,9 @@ steps: - name: ":pipeline: rails-initial-pipeline" command: | + echo "Fetching registry details" + export REGISTRY="$$(nsc workspace describe -o json -k registry_url)" + PATH=/bin:/usr/bin set -e @@ -30,6 +33,7 @@ steps: echo "Fetching pull-request metadata:" (docker run --rm \ -v "$$PWD":/app:ro -w /app \ + -v "$$PWD/cache/bundler":/usr/local/bundle \ -e GITHUB_PUBLIC_REPO_TOKEN \ -e BUILDKITE_REPO \ -e BUILDKITE_PULL_REQUEST \ @@ -40,7 +44,6 @@ steps: sh -c "$$PIPELINE_COMMAND" ([ -f .buildkite/.dockerignore ] && cp .buildkite/.dockerignore .dockerignore) || true - plugins: - artifacts#v1.9.3: upload: ".dockerignore" @@ -58,6 +61,7 @@ steps: PIPELINE_COMMAND: >- docker run --rm -v "$$PWD":/app:ro -w /app + -v "$$PWD/cache/bundler":/usr/local/bundle -e CI -e BUILDKITE -e BUILDKITE_AGENT_META_DATA_QUEUE @@ -72,6 +76,7 @@ steps: -e DOCKER_IMAGE -e RUN_QUEUE -e QUEUE + -e REGISTRY ruby:latest .buildkite/bin/pipeline-generate rails-ci | buildkite-agent pipeline upload diff --git a/pipelines/rails-ci/pipeline.rb b/pipelines/rails-ci/pipeline.rb index fe59733..36236e3 100644 --- a/pipelines/rails-ci/pipeline.rb +++ b/pipelines/rails-ci/pipeline.rb @@ -7,8 +7,9 @@ use Buildkite::Config::RakeCommand use Buildkite::Config::RubyGroup - plugin :docker_compose, "docker-compose#v4.16.0" + plugin :docker_compose, "docker-compose#v5.6.0" plugin :artifacts, "artifacts#v1.9.3" + plugin :secrets, "cluster-secrets#v1.0.0" if build_context.nightly? build_context.rubies << Buildkite::Config::RubyConfig.master_ruby diff --git a/test/buildkite_config/test_build_context.rb b/test/buildkite_config/test_build_context.rb index 9eb3e4b..5c28c8d 100644 --- a/test/buildkite_config/test_build_context.rb +++ b/test/buildkite_config/test_build_context.rb @@ -33,6 +33,36 @@ def test_ci_env_ci ENV["CI"] = @before_env_ci end + def test_ci_env_compute_type_nil + @before_env_compute_type = ENV["BUILDKITE_COMPUTE_TYPE"] + ENV.delete("BUILDKITE_COMPUTE_TYPE") + + sub = create_build_context + assert_predicate sub, :self_hosted? + ensure + ENV["BUILDKITE_COMPUTE_TYPE"] = @before_env_compute_type + end + + def test_ci_env_compute_type_self_hosted + @before_env_compute_type = ENV["BUILDKITE_COMPUTE_TYPE"] + ENV["BUILDKITE_COMPUTE_TYPE"] = "self-hosted" + + sub = create_build_context + assert_predicate sub, :self_hosted? + ensure + ENV["BUILDKITE_COMPUTE_TYPE"] = @before_env_compute_type + end + + def test_ci_env_compute_type_hosted + @before_env_compute_type = ENV["BUILDKITE_COMPUTE_TYPE"] + ENV["BUILDKITE_COMPUTE_TYPE"] = "hosted" + + sub = create_build_context + assert_predicate sub, :hosted? + ensure + ENV["BUILDKITE_COMPUTE_TYPE"] = @before_env_compute_type + end + def test_nightly @before_env_nightly = ENV["RAILS_CI_NIGHTLY"] ENV["RAILS_CI_NIGHTLY"] = "true" @@ -218,6 +248,58 @@ def test_remote_image_base assert_equal "973266071021.dkr.ecr.us-east-1.amazonaws.com/builds", sub.send(:remote_image_base) end + def test_registry_default + @before_env_compute_type = ENV["BUILDKITE_COMPUTE_TYPE"] + ENV.delete("BUILDKITE_COMPUTE_TYPE") + @before_env_registry = ENV["REGISTRY"] + ENV.delete("REGISTRY") + + sub = create_build_context + assert_equal "973266071021.dkr.ecr.us-east-1.amazonaws.com", sub.send(:registry) + ensure + ENV["BUILDKITE_COMPUTE_TYPE"] = @before_env_compute_type + ENV["REGISTRY"] = @before_env_registry + end + + def test_registry_hosted + @before_env_compute_type = ENV["BUILDKITE_COMPUTE_TYPE"] + ENV["BUILDKITE_COMPUTE_TYPE"] = "hosted" + @before_env_registry = ENV["REGISTRY"] + ENV["REGISTRY"] = "test_registry_hosted" + + sub = create_build_context + assert_equal "test_registry_hosted", sub.send(:registry) + ensure + ENV["BUILDKITE_COMPUTE_TYPE"] = @before_env_compute_type + ENV["REGISTRY"] = @before_env_registry + end + + def test_registry_self_hosted + @before_env_compute_type = ENV["BUILDKITE_COMPUTE_TYPE"] + ENV["BUILDKITE_COMPUTE_TYPE"] = "self-hosted" + @before_env_registry = ENV["REGISTRY"] + ENV["REGISTRY"] = "test_registry_self_hosted" + + sub = create_build_context + assert_equal "973266071021.dkr.ecr.us-east-1.amazonaws.com", sub.send(:registry) + ensure + ENV["BUILDKITE_COMPUTE_TYPE"] = @before_env_compute_type + ENV["REGISTRY"] = @before_env_registry + end + + def test_registry_env + @before_env_compute_type = ENV["BUILDKITE_COMPUTE_TYPE"] + ENV.delete("BUILDKITE_COMPUTE_TYPE") + @before_env_registry = ENV["REGISTRY"] + ENV["REGISTRY"] = "test_registry_env" + + sub = create_build_context + assert_equal "973266071021.dkr.ecr.us-east-1.amazonaws.com", sub.send(:registry) + ensure + ENV["BUILDKITE_COMPUTE_TYPE"] = @before_env_compute_type + ENV["REGISTRY"] = @before_env_registry + end + def test_remote_image_base_standard_queues sub = create_build_context diff --git a/test/buildkite_config/test_docker_build.rb b/test/buildkite_config/test_docker_build.rb index e97dde3..aa99868 100644 --- a/test/buildkite_config/test_docker_build.rb +++ b/test/buildkite_config/test_docker_build.rb @@ -5,6 +5,8 @@ class TestDockerBuild < TestCase def test_builder_with_ruby_config_using_string_version + @before_env_compute_type = ENV["BUILDKITE_COMPUTE_TYPE"] + ENV["BUILDKITE_COMPUTE_TYPE"] = "self-hosted" pipeline = PipelineFixture.new do use Buildkite::Config::DockerBuild @@ -20,6 +22,30 @@ def test_builder_with_ruby_config_using_string_version assert_equal ":docker: builder:3.2", pipeline.to_h["steps"][0]["label"] assert_equal "docker-image-builder-3-2", pipeline.to_h["steps"][0]["key"] assert_equal "builder:3.2", pipeline.to_h["steps"][0]["env"]["RUBY_IMAGE"] + ensure + ENV["BUILDKITE_COMPUTE_TYPE"] = @before_env_compute_type + end + + def test_builder_with_hosted + @before_env_compute_type = ENV["BUILDKITE_COMPUTE_TYPE"] + ENV["BUILDKITE_COMPUTE_TYPE"] = "hosted" + pipeline = PipelineFixture.new do + use Buildkite::Config::DockerBuild + + build_context.stub(:rails_version, Gem::Version.new("7.1")) do + builder Buildkite::Config::RubyConfig.new(prefix: "builder:", version: "3.2") + end + end + + %w[label key env].each do |key| + assert_includes pipeline.to_h["steps"][0], key + end + + assert_equal ":docker: builder:3.2", pipeline.to_h["steps"][0]["label"] + assert_equal "docker-image-builder-3-2", pipeline.to_h["steps"][0]["key"] + assert_not_includes pipeline.to_h["steps"][0]["env"], "RUBY_IMAGE" + ensure + ENV["BUILDKITE_COMPUTE_TYPE"] = @before_env_compute_type end def test_builder_artifacts @@ -35,11 +61,11 @@ def test_builder_artifacts plugins = pipeline.to_h["steps"][0]["plugins"] artifacts = plugins.select { |plugin| - plugin.key?("artifacts#v1.0") + plugin.key?(plugins_map[:artifacts]) } - assert_equal ".dockerignore", artifacts[0]["artifacts#v1.0"]["download"] + assert_equal ".dockerignore", artifacts[0][plugins_map[:artifacts]]["download"] - download = artifacts[1]["artifacts#v1.0"] + download = artifacts[1][plugins_map[:artifacts]] assert_equal %w[ .buildkite/.empty .buildkite/docker-compose.yml @@ -51,7 +77,9 @@ def test_builder_artifacts assert_equal ".buildkite.tgz", download["compressed"] end - def test_builder_compose_plugin + def test_builder_compose_plugin_self_hosted + @before_env_compute_type = ENV["BUILDKITE_COMPUTE_TYPE"] + ENV["BUILDKITE_COMPUTE_TYPE"] = "self-hosted" pipeline = PipelineFixture.new do use Buildkite::Config::DockerBuild @@ -63,22 +91,46 @@ def test_builder_compose_plugin plugins = pipeline.to_h["steps"][0]["plugins"] compose = plugins.find { |plugin| - plugin.key?("docker-compose#v1.0") - }.fetch("docker-compose#v1.0") + plugin.key?(plugins_map[:compose]) + }.fetch(plugins_map[:compose]) - %w[image-name cache-from push build config env image-repository].each do |key| + %w[cache-from push build config env].each do |key| assert_includes compose, key end - - assert_equal "3-2-local", compose["image-name"] assert_equal ["base:buildkite-config-base:3-2-br-main"], compose["cache-from"] - assert_equal ["base:buildkite-config-base:3-2-br-"], compose["push"] + assert_equal ["base:buildkite-config-base:3-2-br-", "base:buildkite-config-base:3-2-local"], compose["push"] assert_equal "base", compose["build"] assert_equal ".buildkite/docker-compose.yml", compose["config"] assert_includes compose["env"], "PRE_STEPS" assert_includes compose["env"], "RACK" - assert_equal "buildkite-config-base", compose["image-repository"] + ensure + ENV["BUILDKITE_COMPUTE_TYPE"] = @before_env_compute_type + end + + def test_builder_compose_plugin_hosted + @before_env_compute_type = ENV["BUILDKITE_COMPUTE_TYPE"] + ENV["BUILDKITE_COMPUTE_TYPE"] = "hosted" + pipeline = PipelineFixture.new do + use Buildkite::Config::DockerBuild + + build_context.stub(:rails_version, Gem::Version.new("7.1")) do + builder Buildkite::Config::RubyConfig.new(version: "3.2") + end + end + + command = pipeline.to_h["steps"][0]["command"].first + + expected = <<~COMMAND.squish + docker build --push + --build-arg RUBY_IMAGE=3.2 + --tag buildkite-config-base:3-2-local + --file .buildkite/Dockerfile . + COMMAND + + assert_equal expected.strip, command + ensure + ENV["BUILDKITE_COMPUTE_TYPE"] = @before_env_compute_type end def test_builder_timeout_default @@ -134,7 +186,34 @@ def test_builder_skip assert_equal({}, pipeline.to_h) end - def test_builder_gem_version + def test_builder_gem_version_hosted + @before_env_compute_type = ENV["BUILDKITE_COMPUTE_TYPE"] + ENV["BUILDKITE_COMPUTE_TYPE"] = "hosted" + pipeline = PipelineFixture.new do + use Buildkite::Config::DockerBuild + + build_context.stub(:rails_version, Gem::Version.new("7.1")) do + builder Buildkite::Config::RubyConfig.new(prefix: "ruby:", version: Gem::Version.new("1.9.3")) + end + end + + command = pipeline.to_h["steps"][0]["command"].first + + expected = <<~COMMAND.squish + docker build --push + --build-arg RUBY_IMAGE=ruby:1.9.3 + --tag buildkite-config-base:ruby-1-9-3-local + --file .buildkite/Dockerfile . + COMMAND + + assert_equal expected.strip, command + ensure + ENV["BUILDKITE_COMPUTE_TYPE"] = @before_env_compute_type + end + + def test_builder_gem_version_self_hosted + @before_env_compute_type = ENV["BUILDKITE_COMPUTE_TYPE"] + ENV["BUILDKITE_COMPUTE_TYPE"] = "self-hosted" pipeline = PipelineFixture.new do use Buildkite::Config::DockerBuild @@ -146,11 +225,12 @@ def test_builder_gem_version plugins = pipeline.to_h["steps"][0]["plugins"] compose = plugins.find { |plugin| - plugin.key?("docker-compose#v1.0") - }.fetch("docker-compose#v1.0") + plugin.key?(plugins_map[:compose]) + }.fetch(plugins_map[:compose]) - assert_equal "ruby-1-9-3-local", compose["image-name"] assert_equal ["base:buildkite-config-base:ruby-1-9-3-br-main"], compose["cache-from"] - assert_equal ["base:buildkite-config-base:ruby-1-9-3-br-"], compose["push"] + assert_equal ["base:buildkite-config-base:ruby-1-9-3-br-", "base:buildkite-config-base:ruby-1-9-3-local"], compose["push"] + ensure + ENV["BUILDKITE_COMPUTE_TYPE"] = @before_env_compute_type end end diff --git a/test/buildkite_config/test_rake_command.rb b/test/buildkite_config/test_rake_command.rb index 7792453..0311c1e 100644 --- a/test/buildkite_config/test_rake_command.rb +++ b/test/buildkite_config/test_rake_command.rb @@ -175,11 +175,11 @@ def test_artifacts plugins = pipeline.to_h["steps"][0]["plugins"] artifacts = plugins.select { |plugin| - plugin.key?("artifacts#v1.0") + plugin.key?(plugins_map[:artifacts]) } - assert_equal ".dockerignore", artifacts[0]["artifacts#v1.0"]["download"] + assert_equal ".dockerignore", artifacts[0][plugins_map[:artifacts]]["download"] - download = artifacts[1]["artifacts#v1.0"] + download = artifacts[1][plugins_map[:artifacts]] assert_equal %w[ .buildkite/.empty .buildkite/docker-compose.yml @@ -191,7 +191,44 @@ def test_artifacts assert_equal ".buildkite.tgz", download["compressed"] end - def test_compose + def test_compose_hosted + @before_env_compute_type = ENV["BUILDKITE_COMPUTE_TYPE"] + ENV["BUILDKITE_COMPUTE_TYPE"] = "hosted" + + pipeline = PipelineFixture.new do + build_context.ruby = Buildkite::Config::RubyConfig.new(prefix: "ruby:", version: Gem::Version.new("3.2")) + use Buildkite::Config::RakeCommand + + build_context.stub(:rails_version, Gem::Version.new("7.1")) do + rake "test", task: "test:all" + end + end + + plugins = pipeline.to_h["steps"][0]["plugins"] + + compose = plugins.find { |plugin| + plugin.key?(plugins_map[:compose]) + }.fetch(plugins_map[:compose]) + + %w[env run config shell tty].each do |key| + assert_includes compose, key + end + + assert_includes compose["env"], "PRE_STEPS" + assert_includes compose["env"], "RACK" + + assert_equal "default", compose["run"] + assert_equal "true", compose["tty"] + assert_equal ".buildkite/docker-compose.yml", compose["config"] + assert_equal ["runner", "test"], compose["shell"] + ensure + ENV["BUILDKITE_COMPUTE_TYPE"] = @before_env_compute_type + end + + def test_compose_self_hosted + @before_env_compute_type = ENV["BUILDKITE_COMPUTE_TYPE"] + ENV["BUILDKITE_COMPUTE_TYPE"] = "self-hosted" + pipeline = PipelineFixture.new do build_context.ruby = Buildkite::Config::RubyConfig.new(prefix: "ruby:", version: Gem::Version.new("3.2")) use Buildkite::Config::RakeCommand @@ -204,10 +241,10 @@ def test_compose plugins = pipeline.to_h["steps"][0]["plugins"] compose = plugins.find { |plugin| - plugin.key?("docker-compose#v1.0") - }.fetch("docker-compose#v1.0") + plugin.key?(plugins_map[:compose]) + }.fetch(plugins_map[:compose]) - %w[env run pull config shell].each do |key| + %w[env run cli-version config shell tty].each do |key| assert_includes compose, key end @@ -215,9 +252,12 @@ def test_compose assert_includes compose["env"], "RACK" assert_equal "default", compose["run"] - assert_equal "default", compose["pull"] + assert_equal "1", compose["cli-version"] + assert_equal "true", compose["tty"] assert_equal ".buildkite/docker-compose.yml", compose["config"] assert_equal ["runner", "test"], compose["shell"] + ensure + ENV["BUILDKITE_COMPUTE_TYPE"] = @before_env_compute_type end def test_multiple @@ -240,8 +280,8 @@ def test_multiple plugins = pipeline.to_h["steps"][index]["plugins"] compose = plugins.find { |plugin| - plugin.key?("docker-compose#v1.0") - }.fetch("docker-compose#v1.0") + plugin.key?(plugins_map[:compose]) + }.fetch(plugins_map[:compose]) assert_equal "default", compose["run"] end @@ -260,15 +300,14 @@ def test_docker_compose_plugin_service plugins = pipeline.to_h["steps"][0]["plugins"] compose = plugins.find { |plugin| - plugin.key?("docker-compose#v1.0") - }.fetch("docker-compose#v1.0") + plugin.key?(plugins_map[:compose]) + }.fetch(plugins_map[:compose]) - %w[run pull].each do |key| + %w[run].each do |key| assert_includes compose, key end assert_equal "myservice", compose["run"] - assert_equal "myservice", compose["pull"] end def test_env_yjit @@ -311,8 +350,8 @@ def test_env_pre_steps plugins = pipeline.to_h["steps"][0]["plugins"] compose = plugins.find { |plugin| - plugin.key?("docker-compose#v1.0") - }.fetch("docker-compose#v1.0") + plugin.key?(plugins_map[:compose]) + }.fetch(plugins_map[:compose]) assert_includes compose["env"], "PRE_STEPS" end @@ -390,8 +429,8 @@ def test_rake_env_kwarg plugins = pipeline.to_h["steps"][0]["plugins"] compose = plugins.find { |plugin| - plugin.key?("docker-compose#v1.0") - }.fetch("docker-compose#v1.0") + plugin.key?(plugins_map[:compose]) + }.fetch(plugins_map[:compose]) assert_includes compose["env"], "RACK" end @@ -433,12 +472,12 @@ def test_bundle_command artifacts = plugins[0] - assert_equal "artifacts#v1.0", artifacts.keys.first - assert_equal ".dockerignore", artifacts["artifacts#v1.0"]["download"] + assert_equal plugins_map[:artifacts], artifacts.keys.first + assert_equal ".dockerignore", artifacts[plugins_map[:artifacts]]["download"] artifacts = plugins[1] - assert_equal "artifacts#v1.0", artifacts.keys.first + assert_equal plugins_map[:artifacts], artifacts.keys.first assert_equal %w[ .buildkite/.empty .buildkite/docker-compose.yml @@ -446,14 +485,13 @@ def test_bundle_command .buildkite/Dockerfile.beanstalkd .buildkite/mysql-initdb.d .buildkite/runner - ], artifacts["artifacts#v1.0"]["download"] - assert_equal ".buildkite.tgz", artifacts["artifacts#v1.0"]["compressed"] + ], artifacts[plugins_map[:artifacts]]["download"] + assert_equal ".buildkite.tgz", artifacts[plugins_map[:artifacts]]["compressed"] - compose = plugins[2].fetch("docker-compose#v1.0") + compose = plugins[2].fetch(plugins_map[:compose]) assert_not_includes compose, "env" assert_equal "default", compose["run"] - assert_equal "default", compose["pull"] assert_equal ".buildkite/docker-compose.yml", compose["config"] assert_equal ["runner", "."], compose["shell"] diff --git a/test/test_helper.rb b/test/test_helper.rb index c59f12b..07738ae 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -9,19 +9,31 @@ require "pathname" BUILDKITE_CONFIG_ROOT = Pathname.new(File.expand_path("../..", __dir__)) +module TestHelpers + def plugins_map + { + compose: "docker-compose#v5.6.0", + artifacts: "artifacts#v1.9.3" + } + end +end + class PipelineFixture < Buildkite::Builder::Pipeline + include TestHelpers + def initialize(root = BUILDKITE_CONFIG_ROOT, logger: nil, &block) @pipeline_definition = Proc.new(&block) super(root, logger: logger) use(Buildkite::Config::BuildContext) plugins = extensions.find(Buildkite::Builder::Extensions::Plugins) - plugins.manager.add :docker_compose, "docker-compose#v1.0" - plugins.manager.add :artifacts, "artifacts#v1.0" + plugins.manager.add :docker_compose, plugins_map[:compose] + plugins.manager.add :artifacts, plugins_map[:artifacts] end end class TestCase < ActiveSupport::TestCase + include TestHelpers make_my_diffs_pretty! def setup