目录

Gitea Earthly 构建时间从 1m30s 优化到 11s

目录

文章简介:Gitea Earthly 构建时间从 1m30s 优化到 11s

背景

在我的 homelab server 上搭建了一套 gitea + actions cicd 平台,使用 docker + earthly 构建 golang 程序。workflow 如下:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
name: Gitea Actions Demo
run-name: ${{ gitea.actor }} is testing out Gitea Actions 🚀
on:
  push:
  schedule:
    - cron: '30 5 * * */2' # 每两天

jobs:
  Explore-Gitea-Actions:
    runs-on: ubuntu-latest
    steps:
      - run: echo "🎉 The job was automatically triggered by a ${{ gitea.event_name }} event."
      - run: echo "🐧 This job is now running on a ${{ runner.os }} server hosted by Gitea!"
      - run: echo "🔎 The name of your branch is ${{ gitea.ref }} and your repository is ${{ gitea.repository }}."
      - name: Check out repository code
        # uses: actions/checkout@v3
        uses: https://gitea.com/actions/checkout@v4
        with:
          fetch-depth: 0
      - name: install docker client
        run: |
          sed -E -i -e 's/(archive|ports|security).ubuntu.com/mirrors.dev.domain/g' /etc/apt/sources.list.d/ubuntu.sources
          apt update && apt install sudo
          sh .gitea/workflows/get-docker.sh  --mirror Exfly
          ls -alh /var/run/          
      # truenas 中不支持 安装 qemu,故限制了 crosscompile
      # - name: Set up QEMU
      #   id: qemu
      #   # docker/setup-qemu-action@v1
      #   uses: https://gitee.com/exfly/setupqemuaction@v1
      #   with:
      #       image: docker.m.daocloud.io/tonistiigi/binfmt:latest
      #       platforms: all
      # - run: /bin/sh -c "wget https://mirror.ghproxy.com/https://github.com/earthly/earthly/releases/latest/download/earthly-linux-amd64 -O /usr/local/bin/earthly && chmod +x /usr/local/bin/earthly && /usr/local/bin/earthly bootstrap --with-autocomplete"
      - uses: https://gitee.com/exfly/earthly-actions-setup@main
        with:
          version: "v0.8.1"
          # version: v0.7.2
      - name: Earthly version
        run: earthly --version
      - name: Run build
        run: earthly -V --buildkit-image=docker.1ms.run/earthly/buildkitd:v0.8.3 +build
      - run: echo "💡 The ${{ gitea.repository }} repository has been cloned to the runner."
      - run: echo "🖥️ The workflow is now ready to test your code on the runner."
      - name: List files in the repository
        run: |
          ls ${{ gitea.workspace }}                    
      - run: echo "🍏 This job's status is ${{ gitea.status }}."

/gitea-earthly-speedup-from-1m30s-to-11s/images/image.png

分析下 docker 的安装占用了大量时间;另一部分由于 earthly-actions-setup 从 github 中下载 earthly,众所周知的原因,非常不稳定,还好有正常可以访问时候的 cache,消耗时间比较短。

/gitea-earthly-speedup-from-1m30s-to-11s/images/image-3.png

可以基于 ubuntu-latest 添加 earthly + docker-client,看看效果:

/gitea-earthly-speedup-from-1m30s-to-11s/images/image-2.png

总结

ci 的优化空间从几分钟已经优化到 11s,方法就是将实时安装依赖转为放到基础镜像中。