From ead80316e0d417bcd049171942226d430e2fb4c4 Mon Sep 17 00:00:00 2001 From: "Stanko K.R." Date: Mon, 1 Dec 2025 10:40:24 +0100 Subject: [PATCH] Port over release script Co-Authored-By: Kevin McConnell --- .dockerignore | 3 ++ Dockerfile-export | 17 +++++++++ bin/release | 95 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 115 insertions(+) create mode 100644 Dockerfile-export create mode 100755 bin/release diff --git a/.dockerignore b/.dockerignore index 9a86cc5..26b9fe0 100644 --- a/.dockerignore +++ b/.dockerignore @@ -45,5 +45,8 @@ # Ignore ignore .dockerignore +# Don't include our README +/README.md + # Ignore test files /test/ diff --git a/Dockerfile-export b/Dockerfile-export new file mode 100644 index 0000000..7bdd939 --- /dev/null +++ b/Dockerfile-export @@ -0,0 +1,17 @@ +# syntax = docker/dockerfile:1 + +# Make sure it matches the Ruby version in .ruby-version and Gemfile +ARG RUBY_VERSION=3.4.7 +FROM ruby:$RUBY_VERSION-slim as base + +# Install the tools we need +RUN apt update && apt install -y zip + +# Export from this folder +WORKDIR /campfire + +# Copy application code +COPY . . + +# Create the archive +CMD ["zip", "-r", "/campfire.zip", "."] diff --git a/bin/release b/bin/release new file mode 100755 index 0000000..1be5337 --- /dev/null +++ b/bin/release @@ -0,0 +1,95 @@ +#!/usr/bin/env ruby +require "bundler/inline" +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 +VERSION = ARGV[0] + +DOCKER_IMAGE_NAME = "registry.once.com/campfire" +DOCKER_LATEST_IMAGE_NAME = [ DOCKER_IMAGE_NAME, :latest ].join(":") +DOCKER_TAGGED_IMAGE_NAME = [ DOCKER_IMAGE_NAME, GIT_SHA ].join(":") + +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`" unless VERSION + +def announcing(message) + puts "\n\e[1;36m#{message}\e[0m" + yield +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 "Tagging as #{VERSION}" do + on(:local) do + execute :git, :fetch, :origin, "--tags" + + unless system "git tag -a v#{VERSION} -m 'Version #{VERSION}\n\n- list important changes here' -e" + abort "Failed to create tag; check that the version doesn't already exist" + end + + unless test :git, :push, "--tags" + abort "Failed to push tag; check that the version doesn't already exist" + end + 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, "." + 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", EXPORT_DOCKERFILE, "." + 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