Building single/partial steps from cloudbuild.yaml

We have a single project that builds our GCR/gcloud base-images, it has a lot of reuse between steps so it is nice to not have multiple repos, but locally testing the build became super long since there are so many different images.

Here is a little script to build only a single step and it’s dependencies.

 


remote = ARGV.delete("–remote")
id = ARGV.pop
abort "Usage: ruby build.rb <id|all> [–remote]" unless ARGV.empty?
unless system('which container-builder-local')
abort "Run: gcloud components install container-builder-local"
end
def dependencies(steps, id)
step = steps.detect { |s| s.fetch("id") == id }
[id] + (step["waitFor"] || []).flat_map { |w| dependencies(steps, w) }
end
# make sure to use all variables to avoid:
# "invalid build: key in the substitution data is not matched in the template"
def echo_step
{
"id" => "echo",
"name" => "gcr.io/cloud-builders/wget",
"entrypoint" => "echo",
"args" => ["_IMAGE_DIR is $_IMAGE_DIR"]
}
end
require 'yaml'
config = YAML.load_file("cloudbuild.yaml")
config.delete("images")
unless id == "all"
steps = config.fetch("steps")
keep = dependencies(steps, id)
steps.select! { |s| keep.include? s.fetch("id") }
steps.unshift(echo_step)
puts "Running steps #{steps.map { |s| s.fetch("id")}.join(", ")}"
else
puts "Running all steps"
end
require 'tempfile'
Tempfile.open("docker-images-base-test") do |f|
f.write config.to_yaml
f.close
command = if remote
"gcloud container builds submit –config=#{f.path} –substitutions=_IMAGE_DIR=tmp ."
else
"container-builder-local –config=#{f.path} –dryrun=false –substitutions=_IMAGE_DIR=tmp ."
end
puts command
system(command)
exit $?.exitstatus
end

view raw

build.rb

hosted with ❤ by GitHub