Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

使用github actions cache缓存工具链, 加速你的云编译项目,让你做更快的男人 #7796

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

stupidloud
Copy link
Contributor

@stupidloud stupidloud commented Sep 2, 2021

壁画不多直接上效果,最短25分钟完成

简单原理就是,
github给actions提供了5gb的高速缓存空间,
使用这个空间缓存工具链的编译进度,
可以节省掉不必要的40多分钟耗时。

食用方法:(参考pr)
在你的openwrt-ci.yml actions/checkout段尾部加入

          fetch-depth: 0

(因为工具使用了tools和toolchain的commit id决定是否更新缓存,所以需要全量拉取代码)

随后添加工具调用,即可

      - name: cache
        uses: klever1988/cachewrtbuild@main

第一次编译不会有变化,因为你的仓库还没有缓存,随后的编译作业就会从你仓库拉去第一次编译生成的缓存,跳过无意义的工具链编译时间。
可用参数和默认值:

      - name: cache
        uses: klever1988/cachewrtbuild@main
        with:
          #是否一并缓存.ccache目录,如果你启用了ccache。这是唯一的常用参数,其他三个用于除错,一般不需要调整
          ccache: false

          #是否缓存工具链目录
          toolchain: true

          #是否跳过工具链编译
          skip: true

          #清空缓存
          clean: false

建议在make menuconfig或者

echo -e 'CONFIG_DEVEL=y\nCONFIG_CCACHE=y' >> .config; make defconfig

开启ccache获得进一步的加速时间。

使用P3TERX/Actions-OpenWrt编译的筒子们,因为你们yml的checkout段并不是检出openwrt源代码,而是检出你们的编译脚本。所以应该把uses段放在真正的clone openwrt源码代码段之后,并且用prefix参数声明openwrt源码路径,比如

    - name: Clone source code
      working-directory: /workdir
      run: |
        df -hT $PWD
        git clone $REPO_URL -b $REPO_BRANCH openwrt
        ln -sf /workdir/openwrt $GITHUB_WORKSPACE/openwrt
    - name: Cache
      uses: klever1988/cachewrtbuild@main
      with:
        ccache: 'true'
        prefix: ${{ github.workspace }}/openwrt

@stupidloud stupidloud marked this pull request as ready for review September 2, 2021 01:06
@smallprogram
Copy link
Contributor

Unexpected value 'ccache: 'true'
image

@stupidloud
Copy link
Contributor Author

Unexpected value 'ccache: 'true'

😢 with 加了 |
改好了

@bob-zebedy
Copy link
Contributor

bob-zebedy commented Sep 2, 2021

Google Chrome-add Cache Toolchain · deplivesOpenWrt-CI@3e44766-20210902140011

Code-Build-OpenWrt yml — OpenWrt-CI-20210902140254

@xcf13363175
Copy link

跟楼上一样的报错

@smallprogram
Copy link
Contributor

https://github.com/smallprogram/OpenWrtAction/blob/main/.github/workflows/build-openwrt_X64.yml
@deplives @xcf13363175
参考我这个,经过 @klever1988 调整过的 ,没毛病。

@jarod360
Copy link

jarod360 commented Sep 2, 2021

@smallprogram 使用你的这个出现这个错误,如何修复。
Clone source code

Run df -hT $PWD
Filesystem Type Size Used Avail Use% Mounted on
/dev/root ext4 84G 29G 55G 35% /
fatal: destination path '.' already exists and is not an empty directory.
Error: Process completed with exit code 128.

@xcf13363175
Copy link

尴尬,又是和楼上一样。

@ZeaKyX
Copy link
Contributor

ZeaKyX commented Sep 2, 2021

也许可以写个触发,这样大版本更新的时候可以重新缓存保险一点?

@smallprogram
Copy link
Contributor

@jarod360 我在你的repositories里转了一圈也没找着,不行你fork我的,改config文件夹里的x64.config文件,改成你自己的,然后跑Action编译把。你这个报错之前都已经解决了。
@xcf13363175

@xcf13363175
Copy link

xcf13363175 commented Sep 2, 2021

@smallprogram fork后可以通过。

@dpy013
Copy link
Contributor

dpy013 commented Sep 2, 2021

也许可以写个触发,这样大版本更新的时候可以重新缓存保险一点?

支持。

@smallprogram
Copy link
Contributor

也许可以写个触发,这样大版本更新的时候可以重新缓存保险一点?

有 fetch-depth: 0就行

@ZeaKyX
Copy link
Contributor

ZeaKyX commented Sep 2, 2021

也许可以写个触发,这样大版本更新的时候可以重新缓存保险一点?

有 fetch-depth: 0就行

才看懂,谢谢指示

@Cathgao
Copy link

Cathgao commented Sep 2, 2021

疑问:如果有缓存的话,会不会导致源码更新却没同步到action呢?

@stupidloud
Copy link
Contributor Author

stupidloud commented Sep 2, 2021

使用p3terx大佬编译脚本的要改一下
main改成test分支
uses: klever1988/cachewrtbuild@main
with:
ccache: 'true'
prefix: ${{ github.workspace }}/openwrt

并且应该放置到你clone openwrt源码之后
不是checkout

@jarod360
Copy link

jarod360 commented Sep 2, 2021

@klever1988 大佬按您说的改了,出现这个报错。Error: ENOENT: no such file or directory, chdir '/home/runner/work/Redmi_AX6/Redmi_AX6' -> '/openwrt'
详见https://github.com/jarod360/Redmi_AX6/runs/3494622549?check_suite_focus=true

@stupidloud
Copy link
Contributor Author

@jarod360

prefix: '/workdir/openwrt'

@xcf13363175
Copy link

@klever1988 改了prefix: '/workdir/openwrt'
Error: ENOENT: no such file or directory, chdir '/home/runner/work/OpenWrtAction/OpenWrtAction' -> '/workdir/openwrt'
还是报错

@stupidloud
Copy link
Contributor Author

疑问:如果有缓存的话,会不会导致源码更新却没同步到action呢?

不会的,只是缓存了工具链的编译目录和二进制文件而已,kernel和package每次都会重新编译
而且工具链的源码如果有更新,也是会再次编译,刷新缓存,因为缓存记录了工具链源码的commit id

@ghost
Copy link

ghost commented Sep 3, 2021

使用p3terx大佬编译脚本的要改一下
main改成test分支
uses: klever1988/cachewrtbuild@test
with:
ccache: 'true'
prefix: ${{ github.workspace }}/openwrt

并且应该放置到你clone openwrt源码之后
不是checkout

这个对使用P3TERX/Actions-OpenWrt脚本在线编译的用户是有效的
我开了2个脚本
测试下用脚本前后的差异
看看快了多少

@ababwbq
Copy link

ababwbq commented Sep 3, 2021

image
P3TERX/Actions-OpenWrt脚本 进行编译,测试的确快了不少。

@felixxiao99
Copy link

@klever1988 大佬你好,我使用的P3TERX/Actions-OpenWrt编译,在clone openwrt源码代码段之后加入了
- name: Cache
uses: klever1988/cachewrtbuild@test
with:
ccache: 'true'
prefix: ${{ github.workspace }}/openwrt

请问还需要在actions/checkout段尾部加入fetch-depth: 0吗?
另外Download package代码段里是否还要修改成echo -e 'CONFIG_DEVEL=y\nCONFIG_CCACHE=y' >> .config; make defconfig
谢谢

@stupidloud
Copy link
Contributor Author

@klever1988 大佬你好,我使用的P3TERX/Actions-OpenWrt编译,在clone openwrt源码代码段之后加入了

  • name: Cache
    uses: klever1988/cachewrtbuild@test
    with:
    ccache: 'true'
    prefix: ${{ github.workspace }}/openwrt

请问还需要在actions/checkout段尾部加入fetch-depth: 0吗?
另外Download package代码段里是否还要修改成echo -e 'CONFIG_DEVEL=y\nCONFIG_CCACHE=y' >> .config; make defconfig
谢谢

不要用test分支了,可以直接用main,klever1988/cachewrtbuild@main
用P3TERX的脚本编译不需要加fetch-depth
CONFIG_CCACHE加不加都可以,加了编译速度更快,不过有时候编译出错可以去掉它,排除出错原因

@ghost
Copy link

ghost commented Sep 4, 2021

缓存必须要actions过程全通过了才有效,只能在第1次通过了以后再重新开始就能用缓存了。

解开多线程编译才是最有效的加速。

@stupidloud
Copy link
Contributor Author

缓存必须要actions过程全通过了才有效,只能在第1次通过了以后再重新开始就能用缓存了。

解开多线程编译才是最有效的加速。

make -jnproc 难道不是多线程编译了吗

1-1-2 added a commit to 1-1-2/Actions-Debug that referenced this pull request Jul 11, 2022
@1-1-2
Copy link

1-1-2 commented Jul 14, 2022

【疑问】观察到了预期外的缓存命中

先上 log

> Run klever1988/cachewrtbuild@main
cache-openwrt-9dc86d1962 [ 'cache-openwrt' ]
Received 0 of 1872610335 (0.0%), 0.0 MBs/sec
Received 100663296 of 1872610335 (5.4%), 47.9 MBs/sec
Received 234881024 of 1872610335 (12.5%), 74.6 MBs/sec
Received 369098752 of 1872610335 (19.7%), 87.8 MBs/sec
Received 503316480 of 1872610335 (26.9%), 95.8 MBs/sec
Received 616562688 of 1872610335 (32.9%), 97.8 MBs/sec
Received 754974720 of 1872610335 (40.3%), 102.7 MBs/sec
Received 876609536 of 1872610335 (46.8%), 104.3 MBs/sec
Received 1019215872 of 1872610335 (54.4%), 107.8 MBs/sec
Received 1132462080 of 1872610335 (60.5%), 107.8 MBs/sec
Received 1249902592 of 1872610335 (66.7%), 108.2 MBs/sec
Received 1363148800 of 1872610335 (72.8%), 108.2 MBs/sec
Received 1484783616 of 1872610335 (79.3%), 108.7 MBs/sec
Received 1589641216 of 1872610335 (84.9%), 108.1 MBs/sec
Received 1694498816 of 1872610335 (90.5%), 107.5 MBs/sec
Received 1795162112 of 1872610335 (95.9%), 106.8 MBs/sec
Received 1868416031 of 1872610335 (99.8%), 104.6 MBs/sec
Received 1872610335 of 1872610335 (100.0%), 76.2 MBs/sec
Cache Size: ~1786 MB (1872610335 B)
/usr/bin/tar --use-compress-program zstd -d --long=30 -xf /home/runner/work/_temp/e8a0246a-a259-4c01-ab52-4da84271af9d/cache.tzst -P -C /home/runner/work/OpenWrt-Custom/OpenWrt-Custom
Cache restored successfully
cache-openwrt-3c06a344e9  cache fetched!
skiped

可以观察到 latest commit 是 openwrt/openwrt@9dc86d1 ,但拉取到的缓存 key 是 cache-openwrt-3c06a344e9,而且 skip 了 toolchain 的编译。

【问题产生】

以下是调用参数,比较常规

  with:
    ccache: false
    prefix: /home/runner/work/OpenWrt-Custom/OpenWrt-Custom/openwrt
    toolchain: true
    skip: true
    clean: false

【一些探究】

观察到 const cacheKey = cache.restoreCache(paths, keyString, restoreKeys)
在本次调用中 keyString=cache-openwrt-9dc86d1962, restoreKeys=[ 'cache-openwrt' ] ?没理解错的话

回看定义 cache.restoreCache ,备注中提到

 * @param primaryKey an explicit key for restoring the cache
 * @param restoreKeys an optional ordered list of keys to use for restoring the cache if no cache hit occurred for key

是不是意味着如果 keyString 没有命中,也会根据 restoreKeys 拉取到一份旧缓存,并且在 Post Cache 阶段中被打上新的 tag 作为新的缓存被继续误用,导致不符合“上游源码更新会重新编译 tool 和 toolchain”的预期?

以上是一些三脚猫功夫的理解,如果对源码意图有理解不到位的地方,还请谅解。

@zhangguanzhang
Copy link
Contributor

【疑问】观察到了预期外的缓存命中

先上 log

> Run klever1988/cachewrtbuild@main
cache-openwrt-9dc86d1962 [ 'cache-openwrt' ]
Received 0 of 1872610335 (0.0%), 0.0 MBs/sec
Received 100663296 of 1872610335 (5.4%), 47.9 MBs/sec
Received 234881024 of 1872610335 (12.5%), 74.6 MBs/sec
Received 369098752 of 1872610335 (19.7%), 87.8 MBs/sec
Received 503316480 of 1872610335 (26.9%), 95.8 MBs/sec
Received 616562688 of 1872610335 (32.9%), 97.8 MBs/sec
Received 754974720 of 1872610335 (40.3%), 102.7 MBs/sec
Received 876609536 of 1872610335 (46.8%), 104.3 MBs/sec
Received 1019215872 of 1872610335 (54.4%), 107.8 MBs/sec
Received 1132462080 of 1872610335 (60.5%), 107.8 MBs/sec
Received 1249902592 of 1872610335 (66.7%), 108.2 MBs/sec
Received 1363148800 of 1872610335 (72.8%), 108.2 MBs/sec
Received 1484783616 of 1872610335 (79.3%), 108.7 MBs/sec
Received 1589641216 of 1872610335 (84.9%), 108.1 MBs/sec
Received 1694498816 of 1872610335 (90.5%), 107.5 MBs/sec
Received 1795162112 of 1872610335 (95.9%), 106.8 MBs/sec
Received 1868416031 of 1872610335 (99.8%), 104.6 MBs/sec
Received 1872610335 of 1872610335 (100.0%), 76.2 MBs/sec
Cache Size: ~1786 MB (1872610335 B)
/usr/bin/tar --use-compress-program zstd -d --long=30 -xf /home/runner/work/_temp/e8a0246a-a259-4c01-ab52-4da84271af9d/cache.tzst -P -C /home/runner/work/OpenWrt-Custom/OpenWrt-Custom
Cache restored successfully
cache-openwrt-3c06a344e9  cache fetched!
skiped

可以观察到 latest commit 是 openwrt/openwrt@9dc86d1 ,但拉取到的缓存 key 是 cache-openwrt-3c06a344e9,而且 skip 了 toolchain 的编译。

【问题产生】

以下是调用参数,比较常规

  with:
    ccache: false
    prefix: /home/runner/work/OpenWrt-Custom/OpenWrt-Custom/openwrt
    toolchain: true
    skip: true
    clean: false

【一些探究】

观察到 const cacheKey = cache.restoreCache(paths, keyString, restoreKeys) 在本次调用中 keyString=cache-openwrt-9dc86d1962, restoreKeys=[ 'cache-openwrt' ] ?没理解错的话

回看定义 cache.restoreCache ,备注中提到

 * @param primaryKey an explicit key for restoring the cache
 * @param restoreKeys an optional ordered list of keys to use for restoring the cache if no cache hit occurred for key

是不是意味着如果 keyString 没有命中,也会根据 restoreKeys 拉取到一份旧缓存,并且在 Post Cache 阶段中被打上新的 tag 作为新的缓存被继续误用,导致不符合“上游源码更新会重新编译 tool 和 toolchain”的预期?

以上是一些三脚猫功夫的理解,如果对源码意图有理解不到位的地方,还请谅解。

我现在是用 zstd img 用loop device 挂载成目录,最后把img文件 zstd 压缩后 split成单份 2g后上传 dockerhub

@HiGarfield
Copy link
Contributor

HiGarfield commented Sep 28, 2022

我做了一些更新。全部合并到我的repo的main分支了。
https://github.com/HiGarfield/cachewrtbuild

更新主要内容:

  1. 改进mixkey,使得mixkey跟cache-openwrt之间有个-
  2. 升级依赖模块源码版本。
  3. 重新格式化js代码。
  4. cache命中时不再重新上传cache。
  5. 增加skip_saving选项。主要用于定时拉取一下cache(不用保存),保持cache不会过期失效(比如一周以上不编译)。详见https://github.com/HiGarfield/lede-17.01.4-Mod/blob/master/.github/workflows/fetch-cache.yml
  6. 重写Action的文档。

如果觉得这些修改可以合并的话,跟我说,我提交pr。

@antermin
Copy link

antermin commented Oct 1, 2022

openwrt/openwrt#10863 may be of interest.

From the recent commits on upstream OpenWrt, I guess eventually we can use prebuilt host tools + prebuilt external toolchain to build OpenWrt images, without having to rely on GitHub Actions cache.

@smdx
Copy link

smdx commented Oct 11, 2022

大佬,很久没编译固件了,昨天在云编译结束时提示空间不足错误但是固件已经生成了。请问怎么解决啊?谢谢

.yml添加项

- name: Clone source code
  working-directory: /workdir
  run: |
    df -hT $PWD
    git clone $REPO_URL -b $REPO_BRANCH openwrt
    ln -sf /workdir/openwrt $GITHUB_WORKSPACE/openwrt
- name: Cache
  uses: klever1988/cachewrtbuild@main
  with:
    ccache: 'true'
    prefix: ${{ github.workspace }}/openwrt

任务提示错误信息:
Annotations
2 errors and 1 warning

build
No space left on device : '/home/runner/runners/2.298.2/_diag/pages/5bcd5090-bae5-4225-8d40-3b400054e470_648b2855-6530-4171-b7de-cfa5cf49a170_1.log'

build
The hosted runner: Hosted Agent lost communication with the server. Anything in your workflow that terminates the runner process, starves it for CPU/Memory, or blocks its network access can cause this error.

build
You are running out of disk space. The runner will stop working when the machine runs out of disk space. Free space left: 61 MB

@EnnawYang
Copy link
Contributor

这几天试了下本库的预编译无法加速了,但是官方的库可以加速,本人能力有限没看出来是哪里的问题。另外近期更新的 ext-tools.sh 也很好用, 但是使用 ext-toolchain.sh 会有一点点bug

@binge8
Copy link

binge8 commented Nov 6, 2022

缓存好像真的不能加速了,还有别的办法吗?还是我搞错了,到底需要缓存哪些文件呢

@BGzEroll
Copy link

BGzEroll commented Dec 6, 2022

action被禁用了😹

???

我之前用 Github Action 建任务,自动编译 openwrt,但是跑了几次之后,就被官方禁了。 image

你该不会是开了ssh吧

@binge8
Copy link

binge8 commented Dec 16, 2022

没用了,可以关闭了

@zhangguanzhang
Copy link
Contributor

🤔我自己的思路还是可以用

@binge8
Copy link

binge8 commented Dec 16, 2022

thinking我自己的思路还是可以用

大概说说你的思路吧

@zhangguanzhang
Copy link
Contributor

thinking我自己的思路还是可以用

大概说说你的思路吧

就是先创建一个空的zstd的img文件通过loop方式挂载成目录,然后编译完成后,把这个img文件split后分段缓存住,下次编译就下载缓存,相当于再次编译

@davidtall
Copy link

失效了吗?

@binge8
Copy link

binge8 commented Apr 23, 2023

失效了吗?

还有效

@lxl6125
Copy link
Contributor

lxl6125 commented Apr 25, 2023

失效了吗?

没失效,我第一次编译5个半小时,后面最多一个半小时

@smdx
Copy link

smdx commented May 2, 2023

失效了吗?

没失效,我第一次编译5个半小时,后面最多一个半小时

还是用下面这段?还是要修改?谢谢

- name: Cache
  uses: klever1988/cachewrtbuild@main
  with:
    ccache: 'true'
    prefix: ${{ github.workspace }}/openwrt

@clsddy
Copy link

clsddy commented Nov 17, 2023

请问大佬,缓存机制仍然有效吗,

  • name: Cache
    uses: klever1988/cachewrtbuild@main
    with:
    ccache: 'true'
    prefix: ${{ github.workspace }}/openwrt

@lxl6125
Copy link
Contributor

lxl6125 commented Nov 27, 2023

请问大佬,缓存机制仍然有效吗,

  • name: Cache
    uses: klever1988/cachewrtbuild@main
    with:
    ccache: 'true'
    prefix: ${{ github.workspace }}/openwrt

有效
Screenshot_20231127_155401_com vivaldi browser

@aiamadeus aiamadeus force-pushed the master branch 2 times, most recently from 63c106f to 6606bb3 Compare February 3, 2024 09:40
@liwenjie119
Copy link
Contributor

liwenjie119 commented Mar 22, 2024

现在好像改名成stupidloud/cachewrtbuild了,要注意恶意同名模块攻击,我被安全团队提醒了
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet