Extract builder from app

Building is different from running
This commit is contained in:
David Heinemeier Hansson
2023-01-12 18:16:52 +01:00
parent 08c30a14b9
commit b3992973d6
6 changed files with 49 additions and 36 deletions

View File

@@ -1,5 +1,6 @@
require "mrsk/configuration" require "mrsk/configuration"
require "mrsk/commands/app" require "mrsk/commands/app"
require "mrsk/commands/builder"
require "mrsk/commands/prune" require "mrsk/commands/prune"
require "mrsk/commands/traefik" require "mrsk/commands/traefik"
require "mrsk/commands/registry" require "mrsk/commands/registry"
@@ -20,6 +21,10 @@ class Mrsk::Commander
@app ||= Mrsk::Commands::App.new(config) @app ||= Mrsk::Commands::App.new(config)
end end
def builder
@builder ||= Mrsk::Commands::Builder.new(config)
end
def traefik def traefik
@traefik ||= Mrsk::Commands::Traefik.new(config) @traefik ||= Mrsk::Commands::Traefik.new(config)
end end

View File

@@ -1,14 +1,6 @@
require "mrsk/commands/base" require "mrsk/commands/base"
class Mrsk::Commands::App < Mrsk::Commands::Base class Mrsk::Commands::App < Mrsk::Commands::Base
def push
docker :buildx, :build, "--push", "--platform linux/amd64,linux/arm64", "-t", config.absolute_image, "."
end
def pull
docker :pull, config.absolute_image
end
def run(role: :web) def run(role: :web)
role = config.role(role) role = config.role(role)
@@ -64,10 +56,6 @@ class Mrsk::Commands::App < Mrsk::Commands::Base
docker :image, :prune, "-a", "-f", *service_filter docker :image, :prune, "-a", "-f", *service_filter
end end
def create_new_builder
docker :buildx, :create, "--use", "--name", "mrsk"
end
private private
def service_filter def service_filter
[ "--filter", "label=service=#{config.service}" ] [ "--filter", "label=service=#{config.service}" ]

View File

@@ -0,0 +1,15 @@
require "mrsk/commands/base"
class Mrsk::Commands::Builder < Mrsk::Commands::Base
def create
docker :buildx, :create, "--use", "--name", "mrsk"
end
def push
docker :buildx, :build, "--push", "--platform linux/amd64,linux/arm64", "-t", config.absolute_image, "."
end
def pull
docker :pull, config.absolute_image
end
end

View File

@@ -2,28 +2,6 @@ require_relative "setup"
namespace :mrsk do namespace :mrsk do
namespace :app do namespace :app do
desc "Deliver a newly built app image to servers"
task deliver: %i[ push pull ]
desc "Build locally and push app image to registry"
task :push do
run_locally do
begin
info "Building multi-architecture images may take a while (run with VERBOSE=1 for progress logging)"
execute *MRSK.app.push
rescue SSHKit::Command::Failed => e
error "Missing compatible buildx builder, so creating a new one first"
execute *MRSK.app.create_new_builder
execute *MRSK.app.push
end
end unless ENV["VERSION"]
end
desc "Pull app image from the registry onto servers"
task :pull do
on(MRSK.config.hosts) { execute *MRSK.app.pull }
end
desc "Run app on servers (or start them if they've already been run)" desc "Run app on servers (or start them if they've already been run)"
task :run do task :run do
MRSK.config.roles.each do |role| MRSK.config.roles.each do |role|

View File

@@ -0,0 +1,27 @@
require_relative "setup"
namespace :mrsk do
namespace :builder do
desc "Deliver a newly built app image to servers"
task deliver: %i[ push pull ]
desc "Build locally and push app image to registry"
task :push do
run_locally do
begin
info "Building multi-architecture images may take a while (run with VERBOSE=1 for progress logging)"
execute *MRSK.builder.push
rescue SSHKit::Command::Failed => e
error "Missing compatible buildx builder, so creating a new one first"
execute *MRSK.builder.create
execute *MRSK.builder.push
end
end unless ENV["VERSION"]
end
desc "Pull app image from the registry onto servers"
task :pull do
on(MRSK.config.hosts) { execute *MRSK.builder.pull }
end
end
end

View File

@@ -2,10 +2,10 @@ require_relative "setup"
namespace :mrsk do namespace :mrsk do
desc "Deploy app for the first time to a fresh server" desc "Deploy app for the first time to a fresh server"
task fresh: %w[ server:bootstrap registry:login app:deliver traefik:run app:stop app:run ] task fresh: %w[ server:bootstrap registry:login builder:deliver traefik:run app:stop app:run ]
desc "Push the latest version of the app, ensure Traefik is running, then restart app" desc "Push the latest version of the app, ensure Traefik is running, then restart app"
task deploy: %w[ registry:login app:deliver traefik:run app:stop app:run prune ] task deploy: %w[ registry:login builder:deliver traefik:run app:stop app:run prune ]
desc "Rollback to VERSION=x that was already run as a container on servers" desc "Rollback to VERSION=x that was already run as a container on servers"
task rollback: %w[ app:restart ] task rollback: %w[ app:restart ]