nat*_*iix 21 apt github-actions
我将以下 Github Actions 工作流程用于我的 C 项目。工作流在大约 40 秒内完成,但其中超过一半的时间用于安装valgrind包及其依赖项。
我相信缓存可以帮助我加快工作流程。我不介意多等几秒钟,但这似乎是对 GitHub 资源的毫无意义的浪费。
name: C Workflow
on: [push, pull_request]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- name: make
run: make
- name: valgrind
run: |
sudo apt-get install -y valgrind
valgrind -v --leak-check=full --show-leak-kinds=all ./bin
Run Code Online (Sandbox Code Playgroud)
运行sudo apt-get install -y valgrind会安装以下软件包:
gdbgdbserverlibbabeltrace1libc6-dbglibipt1valgrind我知道 Actions 支持缓存特定目录(并且已经有几个已回答的 SO 问题和关于此的文章),但我不确定 apt 安装的所有不同软件包最终在哪里。我假设/bin/或/usr/bin/不是唯一受安装软件包影响的目录。
是否有一种优雅的方法来缓存已安装的系统包以供将来运行工作流?
sma*_*c89 20
此答案的目的是展示如何使用 github 操作完成缓存。不一定要展示如何缓存valgrind,它确实展示了,但更重要的是表明并非所有内容都可以/应该缓存,并且需要考虑缓存和恢复缓存与重新安装依赖项的权衡。
您将利用该actions/cache操作来执行此操作。
将其添加为一个步骤(在您需要使用 valgrind 之前):
- name: Cache valgrind
uses: actions/cache@v2
id: cache-valgrind
with:
path: "~/valgrind"
key: ${{secrets.VALGRIND_VERSION}}
Run Code Online (Sandbox Code Playgroud)
下一步应尝试安装缓存版本(如果有)或从存储库安装:
- name: Install valgrind
env:
CACHE_HIT: ${{steps.cache-valgrind.outputs.cache-hit}}
VALGRIND_VERSION: ${{secrets.VALGRIND_VERSION}}
run: |
if [[ "$CACHE_HIT" == 'true' ]]; then
sudo cp --verbose --force --recursive ~/valgrind/* /
else
sudo apt-get install --yes valgrind="$VALGRIND_VERSION"
mkdir -p ~/valgrind
sudo dpkg -L valgrind | while IFS= read -r f; do if test -f $f; then echo $f; fi; done | xargs cp --parents --target-directory ~/valgrind/
fi
Run Code Online (Sandbox Code Playgroud)
将VALGRIND_VERSIONsecret 设置为以下输出:
apt-cache policy valgrind | grep -oP '(?<=Candidate:\s)(.+)'
Run Code Online (Sandbox Code Playgroud)
这将允许您在发布新版本时仅通过更改密钥的值来使缓存无效。
dpkg -L valgrind用于列出使用时安装的所有文件sudo apt-get install valgrind。
我们现在可以使用此命令将所有依赖项复制到我们的缓存文件夹中:
dpkg -L valgrind | while IFS= read -r f; do if test -f $f; then echo $f; fi; done | xargs cp --parents --target-directory ~/valgrind/
Run Code Online (Sandbox Code Playgroud)
除了复制 的所有组件外valgrind,可能还需要复制依赖项(例如libc在这种情况下),但我不建议继续沿着这条路走,因为依赖链只是从那里开始增长的。准确的说,复制到最终有一个适合valgrind运行的环境所需要的依赖如下:
要复制所有这些依赖项,您可以使用与上述相同的语法:
for dep in libc6 libgcc1 gcc-8-base; do
dpkg -L $dep | while IFS= read -r f; do if test -f $f; then echo $f; fi; done | xargs cp --parents --target-directory ~/valgrind/
done
Run Code Online (Sandbox Code Playgroud)
当首先需要安装valgrind的只是运行时,所有这些工作真的值得sudo apt-get install valgrind吗?如果您的目标是加快构建过程,那么您还必须考虑恢复(下载和提取)缓存与再次运行命令来安装valgrind.
最后恢复缓存,假设它存储在/tmp/valgrind,你可以使用命令:
cp --force --recursive /tmp/valgrind/* /
Run Code Online (Sandbox Code Playgroud)
这将基本上将所有文件从缓存复制到根分区。
除了上面的过程,我还有一个“缓存valgrind”的例子,通过从源代码安装和编译它。缓存现在大约有 63MB(压缩)大小,仍然需要单独安装libc哪种方式。
注意:这个问题的另一个答案提出了我可以认为是一种更安全的缓存依赖项的方法,通过使用预安装依赖项的容器。最好的部分是您可以使用操作来使这些容器保持最新。
参考:
dei*_*vid 17
您可以创建一个valgrind预安装的 docker 映像并在其上运行您的工作流程。
创建一个Dockerfile类似的东西:
FROM ubuntu
RUN apt-get install -y valgrind
Run Code Online (Sandbox Code Playgroud)
构建它并将其推送到 dockerhub:
FROM ubuntu
RUN apt-get install -y valgrind
Run Code Online (Sandbox Code Playgroud)
然后使用以下内容作为您的工作流程:
docker build -t natiiix/valgrind .
docker push natiiix/valgrind
Run Code Online (Sandbox Code Playgroud)
完全未经测试,但你明白了。
Isr*_* RV 10
更新: 我创建了一个 GitHub 操作,它可以用作此解决方案,代码更少,优化更好。缓存任何新内容
该解决方案与投票最多的解决方案类似。我尝试了建议的解决方案,但它对我不起作用,因为我正在安装texlive-latex,并且pandoc它具有许多依赖项和子依赖项。
我创建了一个应该可以帮助很多人的解决方案。一种情况是当您安装几个软件包(apt install)时,另一种解决方案是当您安装make一个程序并且需要一段时间时。
解决方案:
find创建容器中所有文件的列表。make程序,无论您想要缓存什么。find创建容器中所有文件的列表。diff获取新创建的文件。actions/cache@v2。/。什么时候使用这个?
执行:
我的操作的登陆页面:工作流程。
发布.yml
name: CI - Release books
on:
release:
types: [ released ]
workflow_dispatch:
jobs:
build:
runs-on: ubuntu-18.04
steps:
- uses: actions/checkout@v2
- uses: actions/cache@v2
id: cache-packages
with:
path: ${{ runner.temp }}/cache-linux
key: ${{ runner.os }}-cache-packages-v2.1
- name: Install packages
if: steps.cache-packages.outputs.cache-hit != 'true'
env:
SOURCE: ${{ runner.temp }}/cache-linux
run: |
set +xv
echo "# --------------------------------------------------------"
echo "# Action environment variables"
echo "github.workspace: ${{ github.workspace }}"
echo "runner.workspace: ${{ runner.workspace }}"
echo "runner.os: ${{ runner.os }}"
echo "runner.temp: ${{ runner.temp }}"
echo "# --------------------------------------------------------"
echo "# Where am I?"
pwd
echo "SOURCE: ${SOURCE}"
ls -lha /
sudo du -h -d 1 / 2> /dev/null || true
echo "# --------------------------------------------------------"
echo "# APT update"
sudo apt update
echo "# --------------------------------------------------------"
echo "# Set up snapshot"
mkdir -p "${{ runner.temp }}"/snapshots/
echo "# --------------------------------------------------------"
echo "# Install tools"
sudo rm -f /var/lib/apt/lists/lock
#sudo apt install -y vim bash-completion
echo "# --------------------------------------------------------"
echo "# Take first snapshot"
sudo find / \
-type f,l \
-not \( -path "/sys*" -prune \) \
-not \( -path "/proc*" -prune \) \
-not \( -path "/mnt*" -prune \) \
-not \( -path "/dev*" -prune \) \
-not \( -path "/run*" -prune \) \
-not \( -path "/etc/mtab*" -prune \) \
-not \( -path "/var/cache/apt/archives*" -prune \) \
-not \( -path "/tmp*" -prune \) \
-not \( -path "/var/tmp*" -prune \) \
-not \( -path "/var/backups*" \) \
-not \( -path "/boot*" -prune \) \
-not \( -path "/vmlinuz*" -prune \) \
> "${{ runner.temp }}"/snapshots/snapshot_01.txt 2> /dev/null \
|| true
echo "# --------------------------------------------------------"
echo "# Install pandoc and dependencies"
sudo apt install -y texlive-latex-extra wget
wget -q https://github.com/jgm/pandoc/releases/download/2.11.2/pandoc-2.11.2-1-amd64.deb
sudo dpkg -i pandoc-2.11.2-1-amd64.deb
rm -f pandoc-2.11.2-1-amd64.deb
echo "# --------------------------------------------------------"
echo "# Take second snapshot"
sudo find / \
-type f,l \
-not \( -path "/sys*" -prune \) \
-not \( -path "/proc*" -prune \) \
-not \( -path "/mnt*" -prune \) \
-not \( -path "/dev*" -prune \) \
-not \( -path "/run*" -prune \) \
-not \( -path "/etc/mtab*" -prune \) \
-not \( -path "/var/cache/apt/archives*" -prune \) \
-not \( -path "/tmp*" -prune \) \
-not \( -path "/var/tmp*" -prune \) \
-not \( -path "/var/backups*" \) \
-not \( -path "/boot*" -prune \) \
-not \( -path "/vmlinuz*" -prune \) \
> "${{ runner.temp }}"/snapshots/snapshot_02.txt 2> /dev/null \
|| true
echo "# --------------------------------------------------------"
echo "# Filter new files"
diff -C 1 \
--color=always \
"${{ runner.temp }}"/snapshots/snapshot_01.txt \
"${{ runner.temp }}"/snapshots/snapshot_02.txt \
| grep -E "^\+" \
| sed -E s/..// \
> "${{ runner.temp }}"/snapshots/snapshot_new_files.txt
< "${{ runner.temp }}"/snapshots/snapshot_new_files.txt wc -l
ls -lha "${{ runner.temp }}"/snapshots/
echo "# --------------------------------------------------------"
echo "# Make cache directory"
rm -fR "${SOURCE}"
mkdir -p "${SOURCE}"
while IFS= read -r LINE
do
sudo cp -a --parent "${LINE}" "${SOURCE}"
done < "${{ runner.temp }}"/snapshots/snapshot_new_files.txt
ls -lha "${SOURCE}"
echo ""
sudo du -sh "${SOURCE}" || true
echo "# --------------------------------------------------------"
- name: Copy cached packages
if: steps.cache-packages.outputs.cache-hit == 'true'
env:
SOURCE: ${{ runner.temp }}/cache-linux
run: |
echo "# --------------------------------------------------------"
echo "# Using Cached packages"
ls -lha "${SOURCE}"
sudo cp --force --recursive "${SOURCE}"/. /
echo "# --------------------------------------------------------"
- name: Generate release files and commit in GitHub
run: |
echo "# --------------------------------------------------------"
echo "# Generating release files"
git fetch --all
git pull --rebase origin main
git checkout main
cd ./src/programming-from-the-ground-up
./make.sh
cd ../../
ls -lha release/
git config --global user.name 'Israel Roldan'
git config --global user.email 'israel.alberto.rv@gmail.com'
git add .
git status
git commit -m "Automated Release."
git push
git status
echo "# --------------------------------------------------------"
Run Code Online (Sandbox Code Playgroud)
解释一下部分代码:
这里的动作缓存,表示key将生成一次并在以后的执行中进行比较。这path是生成缓存压缩文件的文件所在的目录。
- uses: actions/cache@v2
id: cache-packages
with:
path: ${{ runner.temp }}/cache-linux
key: ${{ runner.os }}-cache-packages-v2.1
Run Code Online (Sandbox Code Playgroud)
此条件搜索缓存key,如果存在则为cache-hit“true”。
if: steps.cache-packages.outputs.cache-hit != 'true'
if: steps.cache-packages.outputs.cache-hit == 'true'
Run Code Online (Sandbox Code Playgroud)
这并不重要,但是当du命令第一次执行时,Linux 索引了所有文件(5~8 分钟),然后当我们使用 时find,只需要~50 秒即可获取所有文件。如果需要,您可以删除此行。
后缀命令|| true可防止2> /dev/null返回错误,否则操作将停止,因为它将检测到您的脚本有错误输出。您将在脚本中看到一些论文。
sudo du -h -d 1 / 2> /dev/null || true
Run Code Online (Sandbox Code Playgroud)
这是神奇的部分,用于find生成实际文件的列表,排除一些目录以优化缓存文件夹。它也将在安装和程序之后执行make。在下一个快照中,文件名应该不同snapshot_02.txt。
sudo find / \
-type f,l \
-not \( -path "/sys*" -prune \) \
-not \( -path "/proc*" -prune \) \
-not \( -path "/mnt*" -prune \) \
-not \( -path "/dev*" -prune \) \
-not \( -path "/run*" -prune \) \
-not \( -path "/etc/mtab*" -prune \) \
-not \( -path "/var/cache/apt/archives*" -prune \) \
-not \( -path "/tmp*" -prune \) \
-not \( -path "/var/tmp*" -prune \) \
-not \( -path "/var/backups*" \) \
-not \( -path "/boot*" -prune \) \
-not \( -path "/vmlinuz*" -prune \) \
> "${{ runner.temp }}"/snapshots/snapshot_01.txt 2> /dev/null \
|| true
Run Code Online (Sandbox Code Playgroud)
安装一些软件包和pandoc.
sudo apt install -y texlive-latex-extra wget
wget -q https://github.com/jgm/pandoc/releases/download/2.11.2/pandoc-2.11.2-1-amd64.deb
sudo dpkg -i pandoc-2.11.2-1-amd64.deb
rm -f pandoc-2.11.2-1-amd64.deb
Run Code Online (Sandbox Code Playgroud)
生成添加新文件的文本文件,这些文件也可以是符号文件。
diff -C 1 \
"${{ runner.temp }}"/snapshots/snapshot_01.txt \
"${{ runner.temp }}"/snapshots/snapshot_02.txt \
| grep -E "^\+" \
| sed -E s/..// \
> "${{ runner.temp }}"/snapshots/snapshot_new_files.txt
Run Code Online (Sandbox Code Playgroud)
最后将所有文件复制到缓存目录中作为存档,以保留原始信息。
while IFS= read -r LINE
do
sudo cp -a --parent "${LINE}" "${SOURCE}"
done < "${{ runner.temp }}"/snapshots/snapshot_new_files.txt
Run Code Online (Sandbox Code Playgroud)
步骤将所有缓存文件复制到主路径中/。
- name: Copy cached packages
if: steps.cache-packages.outputs.cache-hit == 'true'
env:
SOURCE: ${{ runner.temp }}/cache-linux
run: |
echo "# --------------------------------------------------------"
echo "# Using Cached packages"
ls -lha "${SOURCE}"
sudo cp --force --recursive "${SOURCE}"/. /
echo "# --------------------------------------------------------"
Run Code Online (Sandbox Code Playgroud)
这一步是我使用缓存生成的已安装包,./make.sh脚本用于pandoc执行一些转换。正如我所提到的,您可以创建使用缓存优势的其他步骤或不使用缓存的其他步骤。
- name: Generate release files and commit in GitHub
run: |
echo "# --------------------------------------------------------"
echo "# Generating release files"
cd ./src/programming-from-the-ground-up
./make.sh
Run Code Online (Sandbox Code Playgroud)
举例来说,已经存在几种实现:
https://github.com/awalsh128/cache-apt-pkgs-action
apt-fast从https://git.io/vokNn安装和使用,而不是直接使用apt-get(https://askubuntu.com/questions/52243/what-is-apt-fast-and-should-i-use-it)dpkg -L征集变更${cache_dir}/${installed_package}.tar(不压缩)。action/cache压缩:https://github.com/airvzxf/cache-anything-new-action
在 GitHub Actions 工作流程中缓存 APT 包
dpkg -L,但可以找到文件系统中的所有更改https://github.com/Mudlet/xmlstarlet-action
xmlstarlet使用参数运行的 docker 文件示例Dockerfile,entrypoint.sh不能使用外部脚本或指令集apt-get install,但多个包可能更快| 归档时间: |
|
| 查看次数: |
6616 次 |
| 最近记录: |