Part 5: Reducing code duplication
In some cases, the dependencies might be used in more than one build target. For this use case, we might want to separate dependency downloading and reuse it. For this reason, let's consider breaking this out into a separate build target, called deps. We can then inherit from deps by using the command FROM +deps.
Note that in our case, only the JavaScript version has an example where FROM +deps is used in more than one place: both in build and in docker. Nevertheless, all versions show how dependencies may be separated.
Go
JavaScript
Java
Python
./Earthfile
1
FROM golang:1.15-alpine3.13
2
WORKDIR /go-example
3
​
4
deps:
5
COPY go.mod go.sum ./
6
RUN go mod download
7
# Output these back in case go mod download changes them.
8
SAVE ARTIFACT go.mod AS LOCAL go.mod
9
SAVE ARTIFACT go.sum AS LOCAL go.sum
10
​
11
build:
12
FROM +deps
13
COPY main.go .
14
RUN go build -o build/go-example main.go
15
SAVE ARTIFACT build/go-example /go-example AS LOCAL build/go-example
16
​
17
docker:
18
COPY +build/go-example .
19
ENTRYPOINT ["/go-example/go-example"]
20
SAVE IMAGE go-example:latest
Copied!

Note

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!
./Earthfile
1
FROM node:13.10.1-alpine3.11
2
WORKDIR /js-example
3
​
4
deps:
5
COPY package.json ./
6
COPY package-lock.json ./
7
RUN npm install
8
# Output these back in case npm install changes them.
9
SAVE ARTIFACT package.json AS LOCAL ./package.json
10
SAVE ARTIFACT package-lock.json AS LOCAL ./package-lock.json
11
​
12
build:
13
FROM +deps
14
COPY src src
15
RUN mkdir -p ./dist && cp ./src/index.html ./dist/
16
RUN npx webpack
17
SAVE ARTIFACT dist /dist AS LOCAL dist
18
​
19
docker:
20
FROM +deps
21
COPY +build/dist ./dist
22
EXPOSE 8080
23
ENTRYPOINT ["/js-example/node_modules/http-server/bin/http-server", "./dist"]
24
SAVE IMAGE js-example:latest
Copied!

Note

To copy the files for this example ( Part 5 ) run
1
earthly --artifact github.com/earthly/earthly/examples/tutorial/js:main+part5/part5 ./part5
Copied!
./Earthfile
1
FROM openjdk:8-jdk-alpine
2
RUN apk add --update --no-cache gradle
3
WORKDIR /java-example
4
​
5
deps:
6
COPY build.gradle ./
7
RUN gradle build
8
​
9
build:
10
FROM +deps
11
COPY src src
12
RUN gradle build
13
RUN gradle install
14
SAVE ARTIFACT build/install/java-example/bin AS LOCAL build/bin
15
SAVE ARTIFACT build/install/java-example/lib AS LOCAL build/lib
16
​
17
docker:
18
COPY +build/bin bin
19
COPY +build/lib lib
20
ENTRYPOINT ["/java-example/bin/java-example"]
21
SAVE IMAGE java-example:latest
Copied!

Note

To copy the files for this example ( Part 5 ) run
1
earthly --artifact github.com/earthly/earthly/examples/tutorial/java:main+part5/part5 ./part5
Copied!
./Earthfile
1
FROM python:3
2
WORKDIR /code
3
​
4
deps:
5
RUN pip install wheel
6
COPY requirements.txt ./
7
RUN pip wheel -r requirements.txt --wheel-dir=wheels
8
SAVE ARTIFACT wheels /wheels
9
​
10
build:
11
FROM +deps
12
COPY src src
13
SAVE ARTIFACT src /src
14
​
15
docker:
16
COPY +deps/wheels wheels
17
COPY +build/src src
18
COPY requirements.txt ./
19
RUN pip install --no-index --find-links=wheels -r requirements.txt
20
ENTRYPOINT ["python3", "./src/hello.py"]
21
SAVE IMAGE python-example:latest
Copied!

Note

To copy the files for this example ( Part 5 ) run
1
earthly --artifact github.com/earthly/earthly/examples/tutorial/python:main+part5/part5 ./part5
Copied!
Last modified 24d ago
Copy link