Skip to content

Commit 680bd04

Browse files
authored
Merge pull request #142 from zzak/buildkit-cluster-secrets
Cluster Secrets and Buildkit builds
2 parents 6199e91 + 89ddf20 commit 680bd04

File tree

13 files changed

+358
-74
lines changed

13 files changed

+358
-74
lines changed

Gemfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ GEM
4040
reline (>= 0.3.8)
4141
json (2.6.3)
4242
language_server-protocol (3.17.0.3)
43-
minitest (5.19.0)
43+
minitest (5.25.4)
4444
mutex_m (0.1.2)
4545
parallel (1.23.0)
4646
parser (3.2.2.4)

bin/docs-preview-annotate

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,13 @@ end
2727

2828
json = JSON.parse(response.body)
2929
result = json["result"].first
30+
url = result["aliases"]&.first || result["url"]
3031

3132
plan = <<~PLAN
3233
#### :writing_hand: rails/docs-preview:
3334
34-
* <a href="#{result["url"]}/api">:link: API</a>
35-
* <a href="#{result["url"]}/guides">:link: Guides</a>
35+
* <a href="#{url}/api">:link: API</a>
36+
* <a href="#{url}/guides">:link: Guides</a>
3637
PLAN
3738

3839
puts plan

lib/buildkite/config/build_context.rb

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,18 @@ def pull_request
128128
([ENV["BUILDKITE_PULL_REQUEST"]] - ["false"]).first
129129
end
130130

131+
def compute_type
132+
ENV["BUILDKITE_COMPUTE_TYPE"] || "self-hosted"
133+
end
134+
135+
def self_hosted?
136+
compute_type == "self-hosted"
137+
end
138+
139+
def hosted?
140+
!self_hosted?
141+
end
142+
131143
def standard_queues
132144
[nil, "default", "builder"]
133145
end
@@ -200,8 +212,20 @@ def min_ruby
200212
Gem::Version.new($1 || "2.0")
201213
end
202214

215+
def registry
216+
if hosted?
217+
ENV["REGISTRY"]
218+
else
219+
"973266071021.dkr.ecr.us-east-1.amazonaws.com"
220+
end
221+
end
222+
223+
def image_name
224+
"#{"#{build_queue}-" unless standard_queues.include?(build_queue)}builds"
225+
end
226+
203227
def remote_image_base
204-
"973266071021.dkr.ecr.us-east-1.amazonaws.com/#{"#{build_queue}-" unless standard_queues.include?(build_queue)}builds"
228+
[registry, image_name].join("/")
205229
end
206230
end
207231
end

lib/buildkite/config/docker_build.rb

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,16 @@ def cache_from(build_context)
3030
end
3131

3232
def build_push(build_context)
33-
[
34-
build_context.local_branch =~ /:/ ?
35-
build_context.image_name_for("pr-#{build_context.pull_request}") :
36-
build_context.image_name_for("br-#{build_context.local_branch}"),
37-
]
33+
if build_context.hosted?
34+
build_context.image_name_for(build_context.build_id, prefix: nil)
35+
else
36+
[
37+
build_context.local_branch =~ /:/ ?
38+
build_context.image_name_for("pr-#{build_context.pull_request}") :
39+
build_context.image_name_for("br-#{build_context.local_branch}"),
40+
build_context.image_name_for(build_context.build_id)
41+
]
42+
end
3843
end
3944
end
4045

@@ -66,23 +71,35 @@ def builder(ruby)
6671
compressed: ".buildkite.tgz"
6772
}
6873

69-
plugin :docker_compose, {
70-
build: "base",
71-
config: ".buildkite/docker-compose.yml",
72-
env: %w[PRE_STEPS RACK],
73-
"image-name" => build_context.ruby.image_name_for(build_context.build_id),
74-
"cache-from" => cache_from(build_context),
75-
push: build_push(build_context),
76-
"image-repository" => build_context.image_base,
77-
}
74+
if build_context.hosted?
75+
command <<~COMMAND.squish
76+
docker build --push
77+
--build-arg RUBY_IMAGE=#{build_context.ruby.ruby_image}
78+
--tag #{build_push(build_context)}
79+
--file .buildkite/Dockerfile .
80+
COMMAND
81+
else
82+
plugin :docker_compose, {
83+
build: "base",
84+
config: ".buildkite/docker-compose.yml",
85+
env: %w[PRE_STEPS RACK],
86+
"cache-from" => cache_from(build_context),
87+
push: build_push(build_context),
88+
}
89+
end
7890

79-
env({
91+
env_opts = {
8092
BUNDLER: build_context.bundler,
8193
RUBYGEMS: build_context.rubygems,
82-
RUBY_IMAGE: build_context.ruby.ruby_image,
8394
encrypted_0fb9444d0374_key: nil,
8495
encrypted_0fb9444d0374_iv: nil
85-
})
96+
}
97+
98+
if build_context.self_hosted?
99+
env_opts[:RUBY_IMAGE] = build_context.ruby.ruby_image
100+
end
101+
102+
env(env_opts)
86103

87104
timeout_in_minutes 15
88105

lib/buildkite/config/rake_command.rb

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def build_env(build_context, pre_steps, env)
3333
env
3434
end
3535

36-
def install_plugins(service = "default", env = nil, dir = ".")
36+
def install_plugins(service = "default", env = nil, dir = ".", build_context:)
3737
plugin :artifacts, {
3838
download: ".dockerignore"
3939
}
@@ -49,14 +49,25 @@ def install_plugins(service = "default", env = nil, dir = ".")
4949
compressed: ".buildkite.tgz"
5050
}
5151

52-
plugin :docker_compose, {
52+
if build_context.mainline
53+
plugin :secrets, {
54+
env: "main_env"
55+
}
56+
end
57+
58+
compose_opts = {
5359
"env" => env,
5460
"run" => service,
55-
"pull" => service,
56-
"pull-retries" => 3,
5761
"config" => ".buildkite/docker-compose.yml",
5862
"shell" => ["runner", *dir],
59-
}.compact
63+
"tty" => "true",
64+
}
65+
66+
if build_context.self_hosted?
67+
compose_opts["cli-version"] = "1"
68+
end
69+
70+
plugin :docker_compose, compose_opts.compact
6071
end
6172
end
6273

@@ -73,7 +84,7 @@ def bundle(command, label:, env: nil)
7384
depends_on "docker-image-#{build_context.ruby.image_key}"
7485
command command
7586

76-
install_plugins
87+
install_plugins(build_context: build_context)
7788

7889
env build_env(build_context, nil, env)
7990

@@ -99,7 +110,7 @@ def rake(dir, task: "test", label: nil, service: "default", pre_steps: nil, env:
99110
depends_on "docker-image-#{build_context.ruby.image_key}"
100111
command "rake #{task}"
101112

102-
install_plugins(service, %w[PRE_STEPS RACK], dir)
113+
install_plugins(service, %w[PRE_STEPS RACK], dir, build_context: build_context)
103114

104115
env build_env(build_context, pre_steps, env)
105116

pipelines/buildkite-config/initial.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ steps:
5757
-v "$$PWD":/app:ro -w /app
5858
-v "$$PWD/tmp":/app/tmp:rw
5959
-e BUNDLE_FROZEN
60+
-e BUILDKITE_BUILD_ID
6061
ruby:latest
6162
./bin/pipeline-annotate
6263
- command: |
@@ -80,6 +81,7 @@ steps:
8081
-v "$$PWD/tmp":/app/tmp:rw
8182
-e RAILS_CI_NIGHTLY
8283
-e BUNDLE_FROZEN
84+
-e BUILDKITE_BUILD_ID
8385
ruby:latest
8486
./bin/pipeline-annotate
8587

pipelines/docs-preview/pipeline.rb

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
Buildkite::Builder.pipeline do
44
require "buildkite_config"
55
use Buildkite::Config::BuildContext
6+
use Buildkite::Config::DockerBuild
67

78
plugin :docker, "docker#v5.10.0"
89
plugin :artifacts, "artifacts#v1.9.3"
10+
plugin :secrets, "cluster-secrets#v1.0.0"
911

1012
build_context = context.extensions.find(Buildkite::Config::BuildContext)
1113
build_context.ruby = Buildkite::Config::RubyConfig.new(prefix: "ruby:", version: Gem::Version.new("3.3"))
@@ -22,13 +24,16 @@
2224
next
2325
end
2426

27+
builder build_context.ruby
28+
2529
command do
2630
label "build", emoji: :rails
31+
depends_on "docker-image-#{build_context.ruby.image_key}"
2732
key "build"
2833
command "bundle install && bundle exec rake preview_docs"
2934
timeout_in_minutes 15
3035
plugin :docker, {
31-
image: build_context.image_name_for("br-main", prefix: nil),
36+
image: build_context.image_name_for(build_context.build_id, prefix: nil),
3237
environment: [
3338
"BUILDKITE_BRANCH",
3439
"BUILDKITE_BUILD_CREATOR",
@@ -50,6 +55,9 @@
5055
key "deploy"
5156
depends_on "build"
5257
timeout_in_minutes 15
58+
plugin :secrets, {
59+
env: "docs_preview_env"
60+
}
5361
plugin :docker, {
5462
environment: [
5563
"BUILDKITE_BRANCH",
@@ -83,6 +91,9 @@
8391
download: ".buildkite/bin/docs-preview-annotate",
8492
compressed: ".buildkite.tgz"
8593
}
94+
plugin :secrets, {
95+
env: "docs_preview_env"
96+
}
8697
command "sh -c \"$$ANNOTATE_COMMAND\" | buildkite-agent annotate --style info"
8798
# CLOUDFLARE_API_TOKEN is used to fetch preview URL from latest deployment
8899
env "ANNOTATE_COMMAND" => <<~ANNOTATE.gsub(/[[:space:]]+/, " ").strip

pipelines/rails-ci/initial.yml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
steps:
55
- name: ":pipeline: rails-initial-pipeline"
66
command: |
7+
echo "Fetching registry details"
8+
export REGISTRY="$$(nsc workspace describe -o json -k registry_url)"
9+
710
PATH=/bin:/usr/bin
811
set -e
912
@@ -29,6 +32,7 @@ steps:
2932
3033
(docker run --rm \
3134
-v "$$PWD":/app:ro -w /app \
35+
-v "$$PWD/cache/bundler":/usr/local/bundle \
3236
-e GITHUB_PUBLIC_REPO_TOKEN \
3337
-e BUILDKITE_REPO \
3438
-e BUILDKITE_PULL_REQUEST \
@@ -47,7 +51,6 @@ steps:
4751
sh -c "$$PIPELINE_COMMAND"
4852
4953
([ -f .buildkite/.dockerignore ] && cp .buildkite/.dockerignore .dockerignore) || true
50-
5154
plugins:
5255
- artifacts#v1.9.3:
5356
upload: ".dockerignore"
@@ -65,6 +68,7 @@ steps:
6568
PIPELINE_COMMAND: >-
6669
docker run --rm
6770
-v "$$PWD":/app:ro -w /app
71+
-v "$$PWD/cache/bundler":/usr/local/bundle
6872
-e CI
6973
-e BUILDKITE
7074
-e BUILDKITE_AGENT_META_DATA_QUEUE
@@ -79,6 +83,7 @@ steps:
7983
-e DOCKER_IMAGE
8084
-e RUN_QUEUE
8185
-e QUEUE
86+
-e REGISTRY
8287
ruby:latest
8388
.buildkite/bin/pipeline-generate rails-ci |
8489
buildkite-agent pipeline upload

pipelines/rails-ci/pipeline.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@
77
use Buildkite::Config::RakeCommand
88
use Buildkite::Config::RubyGroup
99

10-
plugin :docker_compose, "docker-compose#v4.16.0"
10+
plugin :docker_compose, "docker-compose#v5.6.0"
1111
plugin :artifacts, "artifacts#v1.9.3"
12+
plugin :secrets, "cluster-secrets#v1.0.0"
1213

1314
if build_context.nightly?
1415
build_context.rubies << Buildkite::Config::RubyConfig.master_ruby

0 commit comments

Comments
 (0)