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

Calling on Targets From Other Earthfiles

So far we've seen how the FROM command in Earthly has the ability to reference another target's image as its base image, like in the case below where the +build target uses the image from the +deps target.
VERSION 0.6
FROM golang:1.15-alpine3.13
WORKDIR /go-example
​
deps:
COPY go.mod go.sum ./
RUN go mod download
# Output these back in case go mod download changes them.
SAVE ARTIFACT go.mod AS LOCAL go.mod
SAVE ARTIFACT go.sum AS LOCAL go.sum
​
build:
FROM +deps
COPY main.go .
RUN go build -o build/go-example main.go
SAVE ARTIFACT build/go-example /go-example AS LOCAL build/go-example
But FROM also has the ability to import targets from Earthfiles in different directories. Let's say we have a directory structure like this.
.
β”œβ”€β”€ services
| └── service-one
| β”œβ”€β”€ Earthfile (containing +deps)
| β”œβ”€β”€ go.mod
| └── go.sum
β”œβ”€β”€ main.go
└── Earthfile
We can use a target in the Earthfile in /services/service-one from inside the Earthfile in the root of our directory.
./services/service-one/Earthfile
VERSION 0.6
FROM golang:1.15-alpine3.13
WORKDIR /go-example
​
deps:
COPY go.mod go.sum ./
RUN go mod download
# Output these back in case go mod download changes them.
SAVE ARTIFACT go.mod AS LOCAL go.mod
SAVE ARTIFACT go.sum AS LOCAL go.sum
./Earthfile
build:
FROM ./services/service-one+deps
COPY main.go .
RUN go build -o build/go-example main.go
SAVE ARTIFACT build/go-example /go-example AS LOCAL build/go-example
This code tells FROM that there is another Earthfile in the services/service-one directory and that the Earthfile contains a target called +deps. In this case, if we were to run +build Earthly is smart enough to go into the subdirectory, run the +deps target in that Earthfile, and then use it as the base image for +build.
We can also reference an Earthfile in another repo, which works in a similar way.
build:
FROM github.com/example/project+remote-target
COPY main.go .
RUN go build -o build/go-example main.go
SAVE ARTIFACT build/go-example /go-example AS LOCAL build/go-example

Importing Whole Projects

In addition to importing targets from other files, we can also import an entire file with the IMPORT command.
VERSION 0.6
IMPORT ./services/service-one AS my_service
FROM golang:1.15-alpine3.13
WORKDIR /go-example
​
build:
FROM my_service+deps
COPY main.go .
RUN go build -o build/go-example main.go
SAVE ARTIFACT build/go-example /go-example AS LOCAL build/go-example
In this example, we assume there is a ./services/service-one directory that contains its own Earthfile. We import it and then use the AS keyword to give it an alias.
Then, in our +build target we can inherit from any target in the imported Earthfile by passing alias+target-name. In this case the Earthfile in the service directory has a target named +deps.

More Examples

Javascript
Java
Python
Copy link
On this page
Calling on Targets From Other Earthfiles
Importing Whole Projects
More Examples