Build arguments and secrets
One of the core features of Earthly is support for build arguments. Build arguments can be used to dynamically set environment variables inside the context of RUN commands.
Build arguments can be passed between targets or from the command line. They encourage writing generic Earthfiles and ultimately promote greater code-reuse.
Additionally, Earthly defines secrets which are similar to build arguments, but are exposed as environment variables when explicitly allowed.
Let's consider a "hello world" example that allows us to change who is being greeted (e.g. hello banana, hello eggplant etc). We will create a hello target that accepts the
name
argument:VERSION 0.7
FROM alpine:latest
hello:
ARG name
RUN echo "hello $name"
Then we will specify a value for the
name
argument on the command line when we invoke earthly
:earthly +hello --name=world
This will output
buildkitd | Found buildkit daemon as docker container (earthly-buildkitd)
alpine:latest | --> Load metadata linux/arm64
+foo | --> FROM alpine:latest
+foo | [██████████] 100% resolve docker.io/library/alpine:[email protected]:21a3deaa0d32a8057914f36584b5288d2e5ecc984380bc0118285c70fa8c9300
+foo | name=world
+foo | --> RUN echo "hello $name"
+foo | hello world
output | --> exporting outputs
If we re-run
earthly +hello --name=world
, we will see that the echo command is cached (and won't re-display the hello world text):+foo | *cached* --> RUN echo "hello $name"
Arguments may also have default values, which may be either constant or dynamic. For example, the following target will greet the name identified by the arg
name
(which has a default value of John), with the current time:hello:
ARG time=$(date +%H:%M)
ARG name=John
RUN echo "hello $name, it is $time"
alpine:latest | --> Load metadata linux/arm64
+base | --> FROM alpine:latest
+base | [██████████] 100% resolve docker.io/library/alpine:[email protected]:21a3deaa0d32a8057914f36584b5288d2e5ecc984380bc0118285c70fa8c9300
+hello | --> ARG time = RUN $(date +%H:%M)
+hello | --> RUN echo "hello $name, it is $time"
+hello | hello John, it is 23:21
output | --> exporting outputs
If an arg has no default value, then the default value is the empty string.
Argument values can be set multiple ways:
- 1.On the command lineThe value can be directly specified on the command line (as shown in the previous example):earthly +hello --HELLO=world --FOO=bar
- 2.From environment variablesSimilar to above, except that the value is an environment variable:export HELLO="world"export FOO="bar"earthly +hello --HELLO="$HELLO" --FOO="$FOO"
- 3.Via the
EARTHLY_BUILD_ARGS
environment variableThe value can also be set via theEARTHLY_BUILD_ARGS
environment variable.export EARTHLY_BUILD_ARGS="HELLO=world,FOO=bar"earthly +helloThis may be useful if you have a set of build args that you'd like to always use and would prefer not to have to specify them on the command line every time. TheEARTHLY_BUILD_ARGS
environment variable may also be stored in your~/.bashrc
file, or some other shell-specific startup script. - 4.From an
.arg
fileIt is also possible to create an.arg
file to contain the build arguments to pass to earthly. First create an.arg
file with:name=eggplantThen simply run earthly:earthly +hello
Build arguments can also be set when calling build targets. If multiple build arguments values are defined for the same argument name, Earthly will build the target for each value; this makes it easy to configure a "build matrix" within Earthly.
For example, we can create a new
greetings
target which calls +hello
multiple times:greetings:
BUILD +hello \
--name=world \
--name=banana \
--name=eggplant
Then when we call
earthly +greetings
, earthly will call +hello
three times: buildkitd | Found buildkit daemon as docker container (earthly-buildkitd)
alpine:latest | --> Load metadata linux/amd64
+base | --> FROM alpine:latest
+base | [██████████] resolve docker.io/library/alpine:[email protected]:69e70a79f2d41ab5d637de98c1e0b055206ba40a8145e7bddb55ccc04e13cf8f ... 100%
+hello | name=banana
+hello | --> RUN echo "hello $name"
+hello | name=eggplant
+hello | --> RUN echo "hello $name"
+hello | name=world
+hello | --> RUN echo "hello $name"
+hello | hello banana
+hello | hello eggplant
+hello | hello world
output | --> exporting outputs
In addition to the
BUILD
command, build args can also be used with FROM
, COPY
, WITH DOCKER --load
and a number of other commands:BUILD +hello --name=world
COPY (+hello/file.txt --name=world) ./
FROM +hello --name=world
WITH DOCKER --load=(+hello --name=world)
...
END
Another way to pass build args is by specifying a dynamic value, delimited by
$(...)
. For example, in the following, the value of the arg name
will be set as the output of the shell command echo world
(which, of course is simply world
):BUILD +hello --name=$(echo world)
Secrets are similar to build arguments; however, they are not defined in targets, but instead are explicitly defined for each
RUN
command that is permitted to access them.Here's an example Earthfile that accesses a secret stored under
passwd
and exposes it under the environment variable mypassword
:FROM alpine:latest
hush:
RUN --secret mypassword=passwd echo "my password is $mypassword"
If the environment variable name is identical to the secret ID. For example to accesses a secret stored under
passwd
and exposes it under the environment variable passwd
you can use the shorthand :FROM alpine:latest
hush:
RUN --secret passwd echo "my password is $passwd"
It's also possible to temporarily mount a secret as a file:
RUN --mount type=secret,target=/root/mypassword,id=passwd echo "my password is $(cat /root/mypassword)"
The file will not be saved to the image snapshot.
The value for
passwd
in examples above must then be supplied when earthly is invoked.This is possible in a few ways:
- 1.Directly, on the command line:earthly --secret passwd=itsasecret +hush
- 2.Via an environment variable:export passwd=itsasecretearthly --secret passwd +hushIf the value of the secret is omitted on the command line Earthly will lookup the environment variable with that name.
- 3.Via the environment variable
EARTHLY_SECRETS
export EARTHLY_SECRETS="passwd=itsasecret"earthly +hushMultiple secrets can be specified by separating them with a comma. - 4.Via the
.secret
file.Create a.secret
file in the same directory where you plan to runearthly
from. Its contents should be:passwd=itsasecretThen simply run earthly:earthly +hello - 5.Via cloud-based secrets. This option helps share secrets within a wider team. To read more about this see the cloud-based secrets guide.
Regardless of the approach chosen from above, once earthly is invoked, in our example, it will output:
+hush | --> RUN echo "my password is $mypassword"
+hush | my password is itsasecret
Commands in earthly must be re-evaluated when the command itself changes (e.g.
echo "hello $name"
is changed to echo "greetings $name"
), or when one of its inputs has changed (e.g. --name=world
is changed to --name=banana
). Earthly creates a hash based on both the contents of the command and the contents of all defined arguments of the target build context.However, in the case of secrets, the contents of the secret is not included in the hash; therefore, if the contents of a secret changes, Earthly is unable to detect such a change, and thus the command will not be re-evaluated.
Earthly stores the contents of command-line-supplied secrets in memory on the localhost. When a
RUN
command that requires a secret is evaluated by BuildKit, the BuildKit daemon will request the secret from the earthly command-line process and will temporarily mount the secret inside the runc container that is evaluating the RUN
command. Once the command finishes the secret is unmounted. It will not persist as an environment variable within the saved container snapshot. Secrets will be kept in-memory until the earthly command exits.Earthly also supports cloud-based shared secrets which can be stored in the cloud. Secrets are never stored in the cloud unless a user creates an earthly account and explicitly calls the
earthly secrets set ...
command to transmit the secret to the earthly cloud-based secrets server. For more information about cloud-based secrets, check out our cloud-based secrets management guide.Last modified 2mo ago