mirror of
https://github.com/basecamp/once-campfire.git
synced 2026-11-23 17:54:55 +09:00
The tag check ignored a failed `git ls-remote` and continued as if the tag did not exist, and it asked `origin`, which can differ from the repository `gh` releases to. Look the tag up with `gh api`, abort when that call fails, and accept only a version of dot-separated numbers. A 1.5.x release still moves `latest` on both registries. That is harmless until 1.6 ships, so it is left for then.
157 lines
4.9 KiB
Ruby
Executable File
157 lines
4.9 KiB
Ruby
Executable File
#!/usr/bin/env ruby
|
|
require "bundler/inline"
|
|
require "json"
|
|
require "open3"
|
|
require "yaml"
|
|
|
|
gemfile do
|
|
source "https://rubygems.org"
|
|
gem "base64"
|
|
gem "bcrypt_pbkdf"
|
|
gem "ed25519"
|
|
gem "sshkit"
|
|
end
|
|
|
|
include SSHKit::DSL
|
|
|
|
ARCH = "linux/amd64,linux/arm64"
|
|
BUILDER_NAME = "campfire-builder"
|
|
GIT_SHA = `git rev-parse HEAD`.strip
|
|
MANUAL_NOTES = ARGV.delete("--notes")
|
|
VERSION = ARGV[0]
|
|
|
|
DOCKER_REGISTRY = "registry.once.com"
|
|
DOCKER_IMAGE_NAME = "#{DOCKER_REGISTRY}/campfire"
|
|
DOCKER_LATEST_IMAGE_NAME = [ DOCKER_IMAGE_NAME, :latest ].join(":")
|
|
DOCKER_TAGGED_IMAGE_NAME = [ DOCKER_IMAGE_NAME, GIT_SHA ].join(":")
|
|
|
|
SOURCE_DIR = "tmp/release-source"
|
|
|
|
EXPORT_IMAGE_NAME = "campfire-export"
|
|
EXPORT_DOCKERFILE = "Dockerfile-export"
|
|
EXPORT_TARGET_NAME = "campfire.zip"
|
|
EXPORT_REMOTE_USER = "app"
|
|
EXPORT_DEPLOY_HOSTS = %w[ once-store-app-101 once-store-app-102 ]
|
|
EXPORT_DEPLOY_LOCATION = "/local/app/product_releases/"
|
|
|
|
abort "You must specify a version, e.g. `bin/release 1.0.0` (pass --notes to write release notes manually)" unless VERSION
|
|
abort "Version must be dot-separated numbers, e.g. 1.0.0" unless VERSION.match?(/\A\d+(\.\d+)+\z/)
|
|
|
|
current_branch = `git rev-parse --abbrev-ref HEAD`.strip
|
|
if current_branch != "main"
|
|
print "\e[1;33mWarning: You are on branch '#{current_branch}', not 'main'. Continue? [y/N] \e[0m"
|
|
abort "Release aborted." unless $stdin.gets.strip.downcase.chars.first == "y"
|
|
end
|
|
|
|
unless `git status --porcelain`.strip.empty?
|
|
print "\e[1;33mWarning: You have uncommitted changes, which will not be released. Continue? [y/N] \e[0m"
|
|
abort "Release aborted." unless $stdin.gets.strip.downcase.chars.first == "y"
|
|
end
|
|
|
|
def github_api(path)
|
|
output, error, status = Open3.capture3("gh", "api", path)
|
|
abort "Failed to read #{path} from GitHub: #{error}" unless status.success?
|
|
JSON.parse(output)
|
|
end
|
|
|
|
TAG = "v#{VERSION}"
|
|
|
|
if github_api("repos/{owner}/{repo}/git/matching-refs/tags/#{TAG}").any? { |ref| ref["ref"] == "refs/tags/#{TAG}" }
|
|
tag_commit = github_api("repos/{owner}/{repo}/commits/refs/tags/#{TAG}")["sha"]
|
|
abort "Tag #{TAG} already exists on GitHub at #{tag_commit}, but HEAD is #{GIT_SHA}." unless tag_commit == GIT_SHA
|
|
end
|
|
|
|
def announcing(message)
|
|
puts "\n\e[1;36m#{message}\e[0m"
|
|
yield
|
|
end
|
|
|
|
announcing "Checking #{DOCKER_REGISTRY} login..." do
|
|
unless system "docker", "manifest", "inspect", DOCKER_LATEST_IMAGE_NAME, out: File::NULL, err: File::NULL
|
|
abort <<~MESSAGE
|
|
Not logged in to #{DOCKER_REGISTRY}. Run:
|
|
|
|
docker login #{DOCKER_REGISTRY}
|
|
|
|
The credentials are in the "chatinabox" entry in 1Password.
|
|
MESSAGE
|
|
end
|
|
end
|
|
|
|
on(:local) do
|
|
builder_found = test :docker, :buildx, :inspect, BUILDER_NAME
|
|
|
|
unless builder_found
|
|
announcing "Setting up builder..." do
|
|
execute :docker, :buildx, :create, "--name", BUILDER_NAME
|
|
end
|
|
end
|
|
end
|
|
|
|
announcing "Creating GitHub release #{TAG}..." do
|
|
on(:local) do
|
|
if MANUAL_NOTES
|
|
notes_file = "tmp/release_notes.md"
|
|
|
|
execute :mkdir, "-p", "tmp"
|
|
|
|
File.write(notes_file, "# What's Changed\n\n- \n")
|
|
system ENV["EDITOR"] || "vim", notes_file
|
|
|
|
unless system "gh", "release", "create", TAG, "--target", GIT_SHA, "--title", TAG, "--notes-file", notes_file
|
|
abort "Failed to create release; check that the version doesn't already exist"
|
|
end
|
|
|
|
execute :rm, notes_file
|
|
else
|
|
unless system "gh", "release", "create", TAG, "--target", GIT_SHA, "--title", TAG, "--generate-notes"
|
|
abort "Failed to create release; check that the version doesn't already exist"
|
|
end
|
|
end
|
|
end
|
|
end
|
|
|
|
announcing "Extracting #{GIT_SHA} to #{SOURCE_DIR}..." do
|
|
on(:local) do
|
|
execute :rm, "-rf", SOURCE_DIR
|
|
execute :mkdir, "-p", SOURCE_DIR
|
|
execute "git archive #{GIT_SHA} | tar -x -C #{SOURCE_DIR}"
|
|
end
|
|
end
|
|
|
|
announcing "Building and pushing image..." do
|
|
versioning = "--build-arg APP_VERSION=#{VERSION} --build-arg GIT_REVISION=#{GIT_SHA}"
|
|
|
|
on(:local) do
|
|
execute :docker, :buildx, :build, "--builder", BUILDER_NAME, "--push", versioning, "-t", DOCKER_LATEST_IMAGE_NAME, "-t", DOCKER_TAGGED_IMAGE_NAME, "--platform", ARCH, SOURCE_DIR
|
|
end
|
|
end
|
|
|
|
SSHKit::Backend::Netssh.configure do |ssh|
|
|
ssh.connection_timeout = 30
|
|
ssh.ssh_options = { user: EXPORT_REMOTE_USER }
|
|
end
|
|
|
|
announcing "Building source code ZIP image..." do
|
|
on(:local) do
|
|
execute :docker, :build, "-t", EXPORT_IMAGE_NAME, "-f", "#{SOURCE_DIR}/#{EXPORT_DOCKERFILE}", SOURCE_DIR
|
|
execute :rm, "-rf", SOURCE_DIR
|
|
end
|
|
end
|
|
|
|
announcing "Exporting source code to ZIP..." do
|
|
on(:local) do
|
|
execute :docker, :run, "--name", EXPORT_IMAGE_NAME, EXPORT_IMAGE_NAME
|
|
execute :docker, :cp, "#{EXPORT_IMAGE_NAME}:/#{EXPORT_TARGET_NAME}", "tmp/#{EXPORT_TARGET_NAME}"
|
|
execute :docker, :rm, EXPORT_IMAGE_NAME
|
|
end
|
|
end
|
|
|
|
announcing "Uploading source code ZIP..." do
|
|
on(EXPORT_DEPLOY_HOSTS) do
|
|
within EXPORT_DEPLOY_LOCATION do
|
|
upload! "tmp/#{EXPORT_TARGET_NAME}", EXPORT_TARGET_NAME
|
|
end
|
|
end
|
|
end
|