利用Earthly工具进行CI/CD

  1. 1. Earthly工具尝试
    1. 1.1. 简单部署
    2. 1.2. 官网例子
  2. 2. Earthly + Gitlab-CI简单实践
    1. 2.1. Earthfile
    2. 2.2. .gitlab-ci.yml

Earthly工具尝试

最近发现一个比较有意思的镜像构建工具Earthly,文档地址

根据Earthly工具的介绍,它相当于Dockerfile+MAkefile+Bash

简单部署

根据官网文档简单试用一下:

因为各种原因,个人选择在docker:dind的容器环境中尝试使用。

启动docker:dind容器:

1
docker run -d --privileged  docker:dind

在容器中:

1
2
wget https://github.com/earthly/earthly/releases/download/v0.5.19/earthly-linux-amd64 -O /usr/local/bin/earthly && chmod +x /usr/local/bin/earthly
#安装earthly

官网例子

1
2
3
4
test/
├── Earthfile
└── src
└── hello.py

Earthfile:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
FROM python:3
WORKDIR /test

build:
# In Python, there's nothing to build.
COPY src src
SAVE ARTIFACT src /src

docker:
COPY +build/src src
ENTRYPOINT ["python3", "./src/hello.py"]
RUN echo "tag: $TAG"
SAVE IMAGE python-example:latest

hello.py:

1
print("hello")

执行build部分:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
earthly  +build
bootstrap | Bootstrapping successful.
buildkitd | Starting buildkit daemon as a docker container (earthly-buildkitd)...
buildkitd-pull | Pulling buildkitd image...
buildkitd-pull | ...Done
buildkitd | ...Done
python:3 | --> Load metadata linux/amd64
context | --> local context .
+base | --> FROM python:3
+base | [ ] resolve docker.io/library/python:3@sha256:8926eeefa354a195ba51309c5df6f381811dacad53da2d6f8242ee075736d1d4 ... 0%
context | [██████████] resolve docker.io/library/python:3@sha256:8926eeefa354a195ba51309c5df6f381811dacad53da2d6f8242ee075736d1d4 ... 100%
ongoing |
ongoing |
+base | --> WORKDIR /test
ongoing | +base (5 seconds ago)
+build | --> COPY src src
output | --> exporting outputs
=================================== SUCCESS ====================================

执行docker部分

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/test # earthly  +docker
buildkitd | Found buildkit daemon as docker container (earthly-buildkitd)
python:3 | --> Load metadata linux/amd64
+base | --> FROM python:3
context | --> local context .
+base | [██████████] resolve docker.io/library/python:3@sha256:8926eeefa354a195ba51309c5df6f381811dacad53da2d6f8242ee075736d1d4 ... 100%
context | transferred 0 file(s) for context . (54 B, 2 file/dir stats)
+base | *cached* --> WORKDIR /test
+build | *cached* --> COPY src src
+build | --> SAVE ARTIFACT src +build/src
+docker | --> COPY +build/src src
output | --> exporting outputs
output | [██████████] exporting layers ... 100%
output | [██████████] exporting manifest sha256:6a1de82c98188800693691d45673e58b56ce7ae5da64a884b7c7f511a6cd2584 ... 100%
output | [██████████] exporting config sha256:aaf41e55619a181ffe17a9925ba449154fbc9fb404640e1d821b285f94b0bd4d ... 100%
output | [██████████] transferring (via tar) docker.io/library/python-example:latest ... 100%
================================ SUCCESS [main] ================================
+docker | Image +docker as python-example:latest

Earthly + Gitlab-CI简单实践

备注:此处仅做演示,故仅采用docker:dind容器来进行CI,仓库仍采用上面的python演示仓库

1
2
3
4
5
../testproject
├── Earthfile
├── README.md
└── src
└── hello.py

Earthfile

1
2
3
4
5
6
7
8
9
10
11
12
13
14
FROM python:3
WORKDIR /test
ARG TAG
ARG IMAGE
build:
# In Python, there's nothing to build.
COPY src src
SAVE ARTIFACT src /src

docker:
COPY +build/src src
ENTRYPOINT ["python3", "./src/hello.py"]
RUN echo "using tag: $TAG"
SAVE IMAGE --push $IMAGE:$TAG

.gitlab-ci.yml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
stages:
- build

variables:
DOCKER_HOST: tcp://localhost:2375
DOCKER_TLS_CERTDIR: ""

build-project:
stage: build
image: docker:19.03.1
services:
- docker:19.03.1-dind
retry: 2
before_script:
- docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY
# 登陆gitlab私有仓库
- /bin/sh -c 'wget https://github.com/earthly/earthly/releases/download/v0.5.19/earthly-linux-amd64 -O /usr/local/bin/earthly && chmod +x /usr/local/bin/earthly'
# 下载earthly
script:
- earthly +build
- earthly --build-arg IMAGE=$CI_REGISTRY_IMAGE --build-arg TAG=$CI_COMMIT_SHORT_SHA --push +docker

等待执行完毕即可,生产环境清使用专有earthly镜像