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.
Arguments are declared with the ARG keyword.
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
FROM alpine:latesthello:ARG nameRUN echo "hello $name"
Then we will specify a value for the
name argument on the command line when we invoke
earthly --build-arg name=world +hello
This will output
buildkitd | Found buildkit daemon as docker container (earthly-buildkitd)alpine:latest | --> Load metadata linux/amd64+foo | --> FROM alpine:latest+foo | [██████████] resolve docker.io/library/alpine:[email protected]:69e70a79f2d41ab5d637de98c1e0b055206ba40a8145e7bddb55ccc04e13cf8f ... 100%+foo | name=world+foo | --> RUN echo "hello $name"+foo | hello worldoutput | --> exporting outputs
If we re-run
earthly --build-arg name=world +hello, we will see that the echo command is cached (and won't re-display the hello world text):
+foo | *cached* --> RUN echo "hello $name"
Argument values can be set multiple ways:
On the command line
The value can be directly specified on the command line (as shown in the previous example):
earthly --build-arg name=world +hello
From an environment variable
If no value is given for name, then earthly will look for the value in the corresponding environment variable on the localhost:
export name="banana"earthly --build-arg name +hello
It is also possible to create an
.env file to contain the build arguments to pass to earthly. First create an
.env file with:
Then simply run earthly:
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 \--build-arg name=world \--build-arg name=banana \--build-arg name=eggplant \+hello
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 worldoutput | --> exporting outputs
In addition to the
BUILD command, the
--build-arg flag can also be used with
COPY and a number of other commands.
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
+secrets/passwd and exposes it under the environment variable
FROM alpine:latesthush:RUN --secret mypassword=+secrets/passwd echo "my password is $mypassword"
The value for
+secrets/passwd must then be supplied when earthly is invoked. This can be either done directly via:
earthly --secret passwd=itsasecret +hush
or if the value is omitted, then earthly will attempt to lookup the value from an environment variable on the localhost:
passwd=itsasecret \earthly --secret passwd +hush
Alternatively, earthly offers cloud-based secrets if you need to share secrets between colleagues.
Once earthly is invoked, it will output:
+hush | --> RUN echo "my password is $mypassword"+hush | my password is itsasecret
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 persist 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.