|
| 1 | +#!/usr/bin/env ruby |
| 2 | +require "bundler/inline" |
| 3 | +require "yaml" |
| 4 | + |
| 5 | +gemfile do |
| 6 | + source "https://rubygems.org" |
| 7 | + gem "base64" |
| 8 | + gem "bcrypt_pbkdf" |
| 9 | + gem "ed25519" |
| 10 | + gem "sshkit" |
| 11 | +end |
| 12 | + |
| 13 | +include SSHKit::DSL |
| 14 | + |
| 15 | +ARCH = "linux/amd64,linux/arm64" |
| 16 | +BUILDER_NAME = "campfire-builder" |
| 17 | +GIT_SHA = `git rev-parse HEAD`.strip |
| 18 | +VERSION = ARGV[0] |
| 19 | + |
| 20 | +DOCKER_IMAGE_NAME = "registry.once.com/campfire" |
| 21 | +DOCKER_LATEST_IMAGE_NAME = [ DOCKER_IMAGE_NAME, :latest ].join(":") |
| 22 | +DOCKER_TAGGED_IMAGE_NAME = [ DOCKER_IMAGE_NAME, GIT_SHA ].join(":") |
| 23 | + |
| 24 | +EXPORT_IMAGE_NAME = "campfire-export" |
| 25 | +EXPORT_DOCKERFILE = "Dockerfile-export" |
| 26 | +EXPORT_TARGET_NAME = "campfire.zip" |
| 27 | +EXPORT_REMOTE_USER = "app" |
| 28 | +EXPORT_DEPLOY_HOSTS = %w[ once-store-app-101 once-store-app-102 ] |
| 29 | +EXPORT_DEPLOY_LOCATION = "/local/app/product_releases/" |
| 30 | + |
| 31 | +abort "You must specify a version, e.g. `bin/release 1.0.0`" unless VERSION |
| 32 | + |
| 33 | +def announcing(message) |
| 34 | + puts "\n\e[1;36m#{message}\e[0m" |
| 35 | + yield |
| 36 | +end |
| 37 | + |
| 38 | +on(:local) do |
| 39 | + builder_found = test :docker, :buildx, :inspect, BUILDER_NAME |
| 40 | + |
| 41 | + unless builder_found |
| 42 | + announcing "Setting up builder..." do |
| 43 | + execute :docker, :buildx, :create, "--name", BUILDER_NAME |
| 44 | + end |
| 45 | + end |
| 46 | +end |
| 47 | + |
| 48 | +announcing "Tagging as #{VERSION}" do |
| 49 | + on(:local) do |
| 50 | + execute :git, :fetch, :origin, "--tags" |
| 51 | + |
| 52 | + unless system "git tag -a v#{VERSION} -m 'Version #{VERSION}\n\n- list important changes here' -e" |
| 53 | + abort "Failed to create tag; check that the version doesn't already exist" |
| 54 | + end |
| 55 | + |
| 56 | + unless test :git, :push, "--tags" |
| 57 | + abort "Failed to push tag; check that the version doesn't already exist" |
| 58 | + end |
| 59 | + end |
| 60 | +end |
| 61 | + |
| 62 | +announcing "Building and pushing image..." do |
| 63 | + versioning = "--build-arg APP_VERSION=#{VERSION} --build-arg GIT_REVISION=#{GIT_SHA}" |
| 64 | + |
| 65 | + on(:local) do |
| 66 | + execute :docker, :buildx, :build, "--builder", BUILDER_NAME, "--push", versioning, "-t", DOCKER_LATEST_IMAGE_NAME, "-t", DOCKER_TAGGED_IMAGE_NAME, "--platform", ARCH, "." |
| 67 | + end |
| 68 | +end |
| 69 | + |
| 70 | +SSHKit::Backend::Netssh.configure do |ssh| |
| 71 | + ssh.connection_timeout = 30 |
| 72 | + ssh.ssh_options = { user: EXPORT_REMOTE_USER } |
| 73 | +end |
| 74 | + |
| 75 | +announcing "Building source code ZIP image..." do |
| 76 | + on(:local) do |
| 77 | + execute :docker, :build, "-t", EXPORT_IMAGE_NAME, "-f", EXPORT_DOCKERFILE, "." |
| 78 | + end |
| 79 | +end |
| 80 | + |
| 81 | +announcing "Exporting source code to ZIP..." do |
| 82 | + on(:local) do |
| 83 | + execute :docker, :run, "--name", EXPORT_IMAGE_NAME, EXPORT_IMAGE_NAME |
| 84 | + execute :docker, :cp, "#{EXPORT_IMAGE_NAME}:/#{EXPORT_TARGET_NAME}", "tmp/#{EXPORT_TARGET_NAME}" |
| 85 | + execute :docker, :rm, EXPORT_IMAGE_NAME |
| 86 | + end |
| 87 | +end |
| 88 | + |
| 89 | +announcing "Uploading source code ZIP..." do |
| 90 | + on(EXPORT_DEPLOY_HOSTS) do |
| 91 | + within EXPORT_DEPLOY_LOCATION do |
| 92 | + upload! "tmp/#{EXPORT_TARGET_NAME}", EXPORT_TARGET_NAME |
| 93 | + end |
| 94 | + end |
| 95 | +end |
0 commit comments