5

玩转Qt(12)-github-Actions缓存优化

 2 years ago
source link: https://jaredtao.github.io/2019/12/04/%E7%8E%A9%E8%BD%ACQt(12)-github-Actions%E7%BC%93%E5%AD%98%E4%BC%98%E5%8C%96/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

玩转Qt(12)-github-Actions缓存优化

本文于 853 天之前发表,文中内容可能已经过时。

在之前两篇文章《github-Actions自动化编译》《github-Actions自动化发行》中,

介绍了github-Actions的一些用法,其中有部分配置,已经有了缓存相关的步骤。

这里专门开一篇文章,来记录github-Actions的缓存优化相关的知识。

缓存actions模板

github-Actions提供了缓存模板cache

官方文档也有说明 缓存文档

缓存大致原理就是把目标路径打包存储下来,并记录一个唯一key。

下次启动时,根据key去查找。找到了就再按路径解压开。

缓存大小限制

注意缓存有大小限制。对于免费用户,单个包不能超过500MB,整个仓库的缓存不能超过2G。

缓存运作流程

一般我们在任务步骤中增加一个cache

steps:
...
- use: actions/cache@v1
with:
...
...

那么在这个地方,缓存执行的操作是restore。

在steps的末尾,会自动增加一个PostCache,执行的操作是record。

Qt项目的缓存优化

Qt项目每次运行Actions时,都是先通过install-qt-action模板,安装Qt,之后再获取代码,编译运行。

安装Qt这个步骤,可快可慢,涛哥在windows平台测试下来,平均要1分30秒左右。

加上cache后,平均只有25秒。

无缓存的配置

先看一个Qt项目的编译配置

name: Windows
on: [push,pull_request]
jobs:
build:
name: Build
runs-on: windows-latest
strategy:
matrix:
qt_ver: [5.12.6]
qt_target: [desktop]
qt_arch: [win64_msvc2017_64, win32_msvc2017]
include:
- qt_arch: win64_msvc2017_64
msvc_arch: x64
- qt_arch: win32_msvc2017
msvc_arch: x86
# 步骤
steps:
# 安装Qt
- name: Install Qt
uses: jurplel/[email protected]
with:
version: ${{ matrix.qt_ver }}
target: ${{ matrix.qt_target }}
arch: ${{ matrix.qt_arch }}
# 拉取代码
- uses: actions/checkout@v1
with:
fetch-depth: 1
# 编译msvc
- name: build-msvc
shell: cmd
env:
vc_arch: ${{ matrix.msvc_arch }}
run: |
call "C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\VC\Auxiliary\Build\vcvarsall.bat" %vc_arch%
qmake
nmake

缓存步骤,一般尽量写steps最前面。

# 步骤
steps:
# 缓存
- name: cacheQt
id: WindowsCacheQt
uses: actions/cache@v1
with:
path: ../Qt/${{matrix.qt_ver}}/${{matrix.qt_arch_install}}
key: ${{ runner.os }}-Qt/${{matrix.qt_ver}}/${{matrix.qt_arch}}

install-qt-action有默认的Qt安装路径${RUNNER_WORKSPACE},不过这个环境变量不一定能取到。

涛哥实际测试下来,以当前路径的上一级作为Qt路径即可。

环境变量还原

缓存只是把文件还原了,环境变量并没有还原,我们还需要手动还原环境变量。

install-qt-action这个模板增加了一个环境变量Qt5_Dir,值为Qt的安装路径,并把对应的bin添加到了Path。

我们要做的,就是在缓存恢复成功后,重新设置这两个变量,并去掉install-qt的步骤。

- name: setupQt
if: steps.WindowsCacheQt.outputs.cache-hit == 'true'
shell: pwsh
env:
QtPath: ../Qt/${{matrix.qt_ver}}/${{matrix.qt_arch_install}}
run: |
$qt_Path=${env:QtPath}
echo "::set-env name=Qt5_Dir::$qt_Path"
echo "::add-path::$qt_Path/bin"

steps.WindowsCacheQt.outputs.cache-hit == ‘true’

是缓存模板的输出值,可以作为后续步骤的条件判断。

写个伪配置,简单示例一下缓存流程

steps:

  • cache
  • setupQt
    if: cache-hit == ‘true’
  • installQt
    if: cache-hit = ‘false’
name: Windows
on:
# push代码时触发workflow
push:
# 忽略README.md
paths-ignore:
- 'README.md'
- 'LICENSE'
# pull_request时触发workflow
pull_request:
# 忽略README.md
paths-ignore:
- 'README.md'
- 'LICENSE'
jobs:
build:
name: Build
# 运行平台, windows-latest目前是windows server 2019
runs-on: windows-latest
strategy:
# 矩阵配置
matrix:
qt_ver: [5.12.6]
qt_target: [desktop]
# mingw用不了
# qt_arch: [win64_msvc2017_64, win32_msvc2017, win32_mingw53,win32_mingw73]
qt_arch: [win64_msvc2017_64, win32_msvc2017]
# 额外设置msvc_arch
include:
- qt_arch: win64_msvc2017_64
msvc_arch: x64
qt_arch_install: msvc2017_64
- qt_arch: win32_msvc2017
msvc_arch: x86
qt_arch_install: msvc2017
env:
targetName: HelloActions-Qt.exe
# 步骤
steps:
- name: cacheQt
id: WindowsCacheQt
uses: actions/cache@v1
with:
path: ../Qt/${{matrix.qt_ver}}/${{matrix.qt_arch_install}}
key: ${{ runner.os }}-Qt/${{matrix.qt_ver}}/${{matrix.qt_arch}}
- name: setupQt
if: steps.WindowsCacheQt.outputs.cache-hit == 'true'
shell: pwsh
env:
QtPath: ../Qt/${{matrix.qt_ver}}/${{matrix.qt_arch_install}}
run: |
$qt_Path=${env:QtPath}
echo "::set-env name=Qt5_Dir::$qt_Path"
echo "::add-path::$qt_Path/bin"
# 安装Qt
- name: Install Qt
if: steps.WindowsCacheQt.outputs.cache-hit != 'true'
# 使用外部action。这个action专门用来安装Qt
uses: jurplel/[email protected]
with:
# Version of Qt to install
version: ${{ matrix.qt_ver }}
# Target platform for build
target: ${{ matrix.qt_target }}
# Architecture for Windows/Android
arch: ${{ matrix.qt_arch }}
# 拉取代码
- uses: actions/checkout@v1
with:
fetch-depth: 1
# 编译msvc
- name: build-msvc
shell: cmd
env:
vc_arch: ${{ matrix.msvc_arch }}
run: |
call "C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\VC\Auxiliary\Build\vcvarsall.bat" %vc_arch%
qmake
nmake

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK