Part 5: Importing
To copy the files for this example ( Part 5 ) run
1
earthly --artifact github.com/earthly/earthly/examples/tutorial/go:main+part5/part5 ./part5
Copied!
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.
1
VERSION 0.6
2
FROM golang:1.15-alpine3.13
3
WORKDIR /go-example
4
​
5
deps:
6
COPY go.mod go.sum ./
7
RUN go mod download
8
# Output these back in case go mod download changes them.
9
SAVE ARTIFACT go.mod AS LOCAL go.mod
10
SAVE ARTIFACT go.sum AS LOCAL go.sum
11
​
12
build:
13
FROM +deps
14
COPY main.go .
15
RUN go build -o build/go-example main.go
16
SAVE ARTIFACT build/go-example /go-example AS LOCAL build/go-example
Copied!
But FROM also has the ability to import targets from Earthfiles in different directories. Let's say we have a directory structure like this.
1
.
2
β”œβ”€β”€ services
3
| └── service-one
4
| β”œβ”€β”€ Earthfile (containing +deps)
5
| β”œβ”€β”€ go.mod
6
| └── go.sum
7
β”œβ”€β”€ main.go
8
└── Earthfile
Copied!
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
1
VERSION 0.6
2
FROM golang:1.15-alpine3.13
3
WORKDIR /go-example
4
​
5
deps:
6
COPY go.mod go.sum ./
7
RUN go mod download
8
# Output these back in case go mod download changes them.
9
SAVE ARTIFACT go.mod AS LOCAL go.mod
10
SAVE ARTIFACT go.sum AS LOCAL go.sum
Copied!
./Earthfile
1
build:
2
FROM ./services/service-one+deps
3
COPY main.go .
4
RUN go build -o build/go-example main.go
5
SAVE ARTIFACT build/go-example /go-example AS LOCAL build/go-example
Copied!
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.
1
build:
2
FROM github.com/example/project+remote-target
3
COPY main.go .
4
RUN go build -o build/go-example main.go
5
SAVE ARTIFACT build/go-example /go-example AS LOCAL build/go-example
Copied!

Importing Whole Projects

In addition to importing tagets from other files, we can also import an entire file with the IMPORT command.
1
VERSION 0.6
2
IMPORT ./services/service-one AS my_service
3
FROM golang:1.15-alpine3.13
4
WORKDIR /go-example
5
​
6
build:
7
FROM my_service+deps
8
COPY main.go .
9
RUN go build -o build/go-example main.go
10
SAVE ARTIFACT build/go-example /go-example AS LOCAL build/go-example
Copied!
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