Add build args

This commit is contained in:
David Heinemeier Hansson
2023-01-15 10:35:15 +01:00
parent 53cd13a0fa
commit bafbde52fe
6 changed files with 64 additions and 19 deletions

View File

@@ -146,6 +146,28 @@ builder:
multiarch: false multiarch: false
``` ```
### Configuring build args for new images
Some images might need an argument passed in during build time, like a GITHUB_TOKEN to give access to private gem repositories. This can be done like so:
```yaml
builder:
args:
GITHUB_TOKEN: <create-a-new-token-from-github>
```
This build arg can then be used in the Dockerfile:
```
# Private repositories need an access token during the build
ARG GITHUB_TOKEN
ENV BUNDLE_GITHUB__COM=x-access-token:$GITHUB_TOKEN
# Install application gems
COPY Gemfile Gemfile.lock ./
RUN bundle install
```
## Commands ## Commands
### Remote execution ### Remote execution

View File

@@ -10,14 +10,12 @@ class Mrsk::Commands::Builder < Mrsk::Commands::Base
def target def target
case case
when config.builder.nil? when config.builder && config.builder["multiarch"] == false
multiarch
when config.builder["multiarch"] == false
native native
when config.builder["local"] && config.builder["remote"] when config.builder && config.builder["local"] && config.builder["remote"]
multiarch_remote multiarch_remote
else else
raise ArgumentError, "Builder configuration incorrect: #{config.builder.inspect}" multiarch
end end
end end

View File

@@ -0,0 +1,18 @@
require "mrsk/commands/base"
class Mrsk::Commands::Builder::Base < Mrsk::Commands::Base
delegate :argumentize, to: Mrsk::Configuration
def pull
docker :pull, config.absolute_image
end
def build_args
argumentize "--build-args", args
end
private
def args
config.builder["args"] || {}
end
end

View File

@@ -1,6 +1,6 @@
require "mrsk/commands/base" require "mrsk/commands/builder/base"
class Mrsk::Commands::Builder::Multiarch < Mrsk::Commands::Base class Mrsk::Commands::Builder::Multiarch < Mrsk::Commands::Builder::Base
def create def create
docker :buildx, :create, "--use", "--name", builder_name docker :buildx, :create, "--use", "--name", builder_name
end end
@@ -10,11 +10,7 @@ class Mrsk::Commands::Builder::Multiarch < Mrsk::Commands::Base
end end
def push def push
docker :buildx, :build, "--push", "--platform linux/amd64,linux/arm64", "-t", config.absolute_image, "." docker :buildx, :build, "--push", "--platform linux/amd64,linux/arm64", "-t", config.absolute_image, *build_args, "."
end
def pull
docker :pull, config.absolute_image
end end
def info def info

View File

@@ -1,6 +1,6 @@
require "mrsk/commands/base" require "mrsk/commands/builder/base"
class Mrsk::Commands::Builder::Native < Mrsk::Commands::Base class Mrsk::Commands::Builder::Native < Mrsk::Commands::Builder::Base
def create def create
# No-op on native # No-op on native
end end
@@ -11,14 +11,10 @@ class Mrsk::Commands::Builder::Native < Mrsk::Commands::Base
def push def push
combine \ combine \
docker(:build, "-t", config.absolute_image, "."), docker(:build, "-t", *build_args, config.absolute_image, "."),
docker(:push, config.absolute_image) docker(:push, config.absolute_image)
end end
def pull
docker :pull, config.absolute_image
end
def info def info
# No-op on native # No-op on native
end end

View File

@@ -21,4 +21,19 @@ class BuilderCommandTest < ActiveSupport::TestCase
builder = Mrsk::Commands::Builder.new(Mrsk::Configuration.new(@config.merge({ builder: { "local" => { }, "remote" => { } } }))) builder = Mrsk::Commands::Builder.new(Mrsk::Configuration.new(@config.merge({ builder: { "local" => { }, "remote" => { } } })))
assert builder.remote? assert builder.remote?
end end
test "build args" do
builder = Mrsk::Commands::Builder.new(Mrsk::Configuration.new(@config.merge({ builder: { "args" => { "a" => 1, "b" => 2 } } })))
assert_equal [ "--build-args", "a=1", "--build-args", "b=2" ], builder.target.build_args
end
test "native push with build args" do
builder = Mrsk::Commands::Builder.new(Mrsk::Configuration.new(@config.merge({ builder: { "multiarch" => false, "args" => { "a" => 1, "b" => 2 } } })))
assert_equal [ :docker, :build, "-t", "--build-args", "a=1", "--build-args", "b=2", "dhh/app:123", ".", "&&", :docker, :push, "dhh/app:123" ], builder.push
end
test "multiarch push with build args" do
builder = Mrsk::Commands::Builder.new(Mrsk::Configuration.new(@config.merge({ builder: { "args" => { "a" => 1, "b" => 2 } } })))
assert_equal [ :docker, :buildx, :build, "--push", "--platform linux/amd64,linux/arm64", "-t", "dhh/app:123", "--build-args", "a=1", "--build-args", "b=2", "." ], builder.push
end
end end