LogoLogo
HomeBlogGitHubGet Started FreeLogin
Earthly 0.6
Earthly 0.6
  • 👋Introduction
  • 💻Installation
  • 🎓Learn the basics
    • Part 1: A simple Earthfile
    • Part 2: Outputs
    • Part 3: Adding dependencies With Caching
    • Part 4: Args
    • Part 5: Importing
    • Part 6: Using Docker In Earthly
    • Final words
  • ✅Best practices
  • 📖Docs
    • Guides
      • Authenticating Git and image registries
      • Target, artifact and command referencing
      • Build arguments and secrets
      • User-defined commands (UDCs)
      • Managing cache
      • Advanced local caching
      • Using Docker in Earthly
      • Integration Testing
      • Debugging techniques
      • Multi-platform builds
      • Podman
      • Configuring registries
        • AWS ECR
        • GCP Artifact Registry
        • Azure ACR
        • Self-signed certificates
      • Using the Earthly Docker Images
        • earthly/earthly
        • earthly/buildkitd
    • Remote runners
    • Remote caching
    • Earthfile reference
      • Builtin args
      • Excluding patterns
      • Version-specific features
    • The earthly command
    • Configuration reference
    • Examples
    • Misc
      • Alternative installation
      • Data collection
      • Definitions
      • Public key authentication
  • 🔧CI Integration
    • Overview
    • Use the Earthly CI Image
    • Build your own Earthly CI Image
    • Pull-Through Cache
    • Remote BuildKit
    • Vendor-Specific Guides
      • Jenkins
      • Circle CI
      • GitHub Actions
      • AWS CodeBuild
      • Kubernetes
      • Google Cloud Build
      • GitLab CI/CD
      • Woodpecker CI
  • ☁️ Earthly Cloud
    • Overview
    • Cloud secrets
    • Satellites
      • Managing Satellites
      • Using Satellites
Powered by GitBook
On this page
  • Calling on Targets From Other Earthfiles
  • Importing Whole Projects
  • More Examples

Was this helpful?

Edit on GitHub
  1. Learn the basics

Part 5: Importing

PreviousPart 4: ArgsNextPart 6: Using Docker In Earthly

Last updated 1 year ago

Was this helpful?

To copy the files for run

earthly --artifact github.com/earthly/earthly/examples/tutorial/go:main+part5/part5 ./part5

Examples in , and 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-workdir

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 output/example main.go
    SAVE ARTIFACT output/example AS LOCAL local-output/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. NOTE: relative paths must use ./ or ../.

./services/service-one/Earthfile


VERSION 0.6
FROM golang:1.15-alpine3.13
WORKDIR /go-workdir

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 output/example main.go
    SAVE ARTIFACT output/example AS LOCAL local-output/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.

build:
    FROM github.com/example/project+remote-target
    COPY main.go .
    RUN go build -o output/example main.go
    SAVE ARTIFACT output/example AS LOCAL local-output/go-example

Importing Whole Projects

In addition to importing single targets from other files, we can also import an entire Earthfile with the IMPORT command. This is helpful if there are several targets in a separate Earthfile that you want access to in your current file. It also allows you to create an alias.

VERSION 0.6
IMPORT ./services/service-one AS my_service
FROM golang:1.15-alpine3.13
WORKDIR /go-workdir

build:
    FROM my_service+deps
    COPY main.go .
    RUN go build -o output/example main.go
    SAVE ARTIFACT output/example AS LOCAL local-output/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
earthly --artifact github.com/earthly/earthly/examples/tutorial/js:main+part5/part5 ./part5

./Earthfile

VERSION 0.6
FROM node:13.10.1-alpine3.11
WORKDIR /js-example

build:
    FROM ./services/service-one+deps
    COPY src src
    RUN mkdir -p ./dist && cp ./src/index.html ./dist/
    RUN npx webpack
    SAVE ARTIFACT dist /dist AS LOCAL dist

docker:
    FROM +deps
    ARG tag='latest'
    COPY +build/dist ./dist
    EXPOSE 8080
    ENTRYPOINT ["/js-example/node_modules/http-server/bin/http-server", "./dist"]
    SAVE IMAGE js-example:$tag
Java
earthly --artifact github.com/earthly/earthly/examples/tutorial/java:main+part5/part5 ./part5

./Earthfile

VERSION 0.6
FROM openjdk:8-jdk-alpine
RUN apk add --update --no-cache gradle
WORKDIR /java-example

build:
    FROM ./services/service-one+deps
    COPY src src
    RUN gradle build
    RUN gradle install
    SAVE ARTIFACT build/install/java-example/bin AS LOCAL build/bin
    SAVE ARTIFACT build/install/java-example/lib AS LOCAL build/lib

docker:
    COPY +build/bin bin
    COPY +build/lib lib
    ARG tag='latest'
    ENTRYPOINT ["/java-example/bin/java-example"]
    SAVE IMAGE java-example:$tag
Python
earthly --artifact github.com/earthly/earthly/examples/tutorial/python:main+part5/part5 ./part5

./Earthfile

VERSION 0.6
FROM python:3
WORKDIR /code

build:
    FROM ./services/service-one+deps
    COPY src src
    SAVE ARTIFACT src /src

docker:
    COPY +deps/wheels wheels
    COPY +build/src src
    COPY requirements.txt ./
    ARG tag='latest'
    RUN pip install --no-index --find-links=wheels -r requirements.txt
    ENTRYPOINT ["python3", "./src/hello.py"]
    SAVE IMAGE python-example:$tag

We can also reference an Earthfile in another repo, which works in a similar way. If the reference does not begin with one of /, ./, or ../, then earthly treats it as a repository. See for details.

To copy the files for run

To copy the files for run

To copy the files for run

🎓
the reference
this example ( Part 5 )
this example ( Part 5 )
this example ( Part 5 )
this example ( Part 5 )
Python
JavaScript
Java