Skip to content

Instantly share code, notes, and snippets.

@grosser
Last active April 16, 2018 22:35
Show Gist options
  • Save grosser/40c1fadc4d55d0411bd264f88e6dd3b1 to your computer and use it in GitHub Desktop.
Save grosser/40c1fadc4d55d0411bd264f88e6dd3b1 to your computer and use it in GitHub Desktop.
Building single/partial steps from cloudbuild.yaml
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment