Part 4: Args
To copy the files for this example ( Part 4 ) run
earthly --artifact github.com/earthly/earthly/examples/tutorial/go:main+part4/part4 ./part4
Examples in Python, Javascript and Java are at the bottom of this page.

Just Like Docker...Mostly

ARGs in Earthly work similar to ARGs in Dockerfiles, however there are a few differences when it comes to scope. Also, Earthly has a number of built in ARGs that are available to use.
Let's say we wanted to have the option to pass in a tag for our Docker image when we run earthly +docker.
docker:
ARG tag='latest'
COPY +build/go-example .
ENTRYPOINT ["/go-example/go-example"]
SAVE IMAGE go-example:$tag
In our +docker target we can create an ARG called tag. In this case, we give it a default value of latest. If we do not provide a default value the default will be an empty string.
Then, down in our SAVE IMAGE command, we are able to reference the ARG with $ followed by the ARG name.
Now we can take advantage of this when we run Earthly.
earthly +docker --tag='my-new-image-tag'
In this case my-new-image-tag will override the default value and become the new tag for our docker image. If we hadn't passed in a value for tag, then the default latest would have been used.
earthly +docker
# tag for image will be 'latest'

Passing ARGs in FROM, BUILD, and COPY

We can also pass ARGs when referencing a target inside an Earthfile. Using the FROM and BUILD commands, this looks pretty similar to what we did above on the command line.
docker:
ARG tag='latest'
COPY +build/go-example .
ENTRYPOINT ["/go-example/go-example"]
SAVE IMAGE go-example:$tag
​
with-build:
BUILD +docker --tag='my-new-image-tag'
​
with-from:
FROM +docker --tag='my-new-image-tag'
We can also pass ARGs when using the COPY command, though the syntax is a little different.
build:
ARG version
COPY main.go .
RUN go build -o build/go-example-$version main.go
SAVE ARTIFACT build/go-example-$version /go-example AS LOCAL build/go-example
​
with-copy:
COPY (+build/go-example --version='1.0') .

Builtin ARGs

There are a number of builtin ARGs that Earthly offers. You can read about a complete list of them, but for now, let's take a look at how they work.
In order to use Earthly builtin ARGs they need to be pre-declared. Once you do that, you can use them just like any other ARG.
ARG USERARCH
RUN echo $USERARCH
In this case we've declared the ARG USERARCH which is a builtin that holds the processor architecture the target is being built from.

More Examples

Javascript
Java
Python
Copy link
On this page
Just Like Docker...Mostly
Builtin ARGs
More Examples