Run Incus Tests #418
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Test Incus Images | |
on: | |
schedule: | |
- cron: '0 4 2-30/2 * *' | |
workflow_dispatch: | |
jobs: | |
prepare-matrix: | |
runs-on: ubuntu-latest | |
outputs: | |
test-matrix: ${{ steps.set-matrix.outputs.matrix }} | |
steps: | |
- uses: actions/checkout@v4 | |
- id: set-matrix | |
run: | | |
# 下载两个平台的镜像列表 | |
curl -o x86_64_all_images.txt https://raw.githubusercontent.com/oneclickvirt/incus_images/main/x86_64_all_images.txt | |
curl -o arm64_all_images.txt https://raw.githubusercontent.com/oneclickvirt/incus_images/main/arm64_all_images.txt | |
echo "构建测试矩阵..." | |
matrix_json="{\"include\":[" | |
while IFS= read -r image; do | |
if [[ -n "$image" ]]; then | |
matrix_json+="{\"image\":\"$image\",\"arch\":\"amd64\",\"runner\":\"ubuntu-latest\"}," | |
fi | |
done < x86_64_all_images.txt | |
while IFS= read -r image; do | |
if [[ -n "$image" ]]; then | |
matrix_json+="{\"image\":\"$image\",\"arch\":\"arm64\",\"runner\":\"ubuntu-24.04-arm\"}," | |
fi | |
done < arm64_all_images.txt | |
# 去掉最后一个逗号 | |
matrix_json=${matrix_json%,} | |
matrix_json+="]}" | |
echo "matrix=$matrix_json" >> $GITHUB_OUTPUT | |
# 同时用 set-output 兼容部分旧版本 runner | |
echo "::set-output name=matrix::$matrix_json" | |
test-single-image: | |
needs: prepare-matrix | |
strategy: | |
fail-fast: false | |
max-parallel: 12 | |
matrix: ${{ fromJson(needs.prepare-matrix.outputs.test-matrix) }} | |
runs-on: ${{ matrix.runner }} | |
timeout-minutes: 10 | |
# 定义 job 输出,方便在后续任务中收集每个测试的状态 | |
outputs: | |
success: ${{ steps.test.outputs.success }} | |
image: ${{ steps.test.outputs.image }} | |
arch: ${{ steps.test.outputs.arch }} | |
steps: | |
- uses: actions/checkout@v4 | |
- name: Setup Fresh Environment | |
run: | | |
echo "设置新环境..." | |
sudo apt update -y | |
sudo sh -c 'export noninteractive=true && curl -L https://raw.githubusercontent.com/oneclickvirt/incus/main/scripts/incus_install.sh -o incus_install.sh && chmod +x incus_install.sh && bash incus_install.sh' | |
- name: Configure Git | |
run: | | |
git config --global user.name "daily-test" | |
git config --global user.email "[email protected]" | |
- name: Test Image | |
id: test | |
# 使用 continue-on-error 保证即使当前镜像测试失败,也不会影响整个矩阵执行 | |
continue-on-error: true | |
run: | | |
echo "测试镜像: ${{ matrix.image }}" | |
# 备份 DNS 配置 | |
sudo cp /etc/resolv.conf /etc/resolv.conf.backup | |
if sudo bash test.sh "${{ matrix.image }}"; then | |
echo "测试通过: ${{ matrix.image }}" | |
echo "success=true" >> $GITHUB_OUTPUT | |
else | |
echo "测试失败: ${{ matrix.image }}" | |
echo "success=false" >> $GITHUB_OUTPUT | |
# 使用 exit 1 标记此任务失败,但由于 continue-on-error,不会终止整个矩阵 | |
exit 1 | |
fi | |
# 还原 DNS 配置 | |
sudo mv /etc/resolv.conf.backup /etc/resolv.conf || true | |
# 同时输出当前镜像与架构,方便后续整理时匹配 | |
echo "image=${{ matrix.image }}" >> $GITHUB_OUTPUT | |
echo "arch=${{ matrix.arch }}" >> $GITHUB_OUTPUT | |
results-updater: | |
needs: test-single-image | |
if: always() | |
runs-on: ubuntu-latest | |
steps: | |
- uses: actions/checkout@v4 | |
- name: Download Image Lists | |
run: | | |
curl -o x86_64_all_images.txt https://raw.githubusercontent.com/oneclickvirt/incus_images/main/x86_64_all_images.txt | |
curl -o arm64_all_images.txt https://raw.githubusercontent.com/oneclickvirt/incus_images/main/arm64_all_images.txt | |
# 初始化固定镜像列表文件 | |
> x86_64_fixed_images.txt | |
> arm64_fixed_images.txt | |
- name: Collect Test Results | |
run: | | |
echo "收集测试结果..." | |
# 将 prepare-matrix 任务构造的矩阵 JSON 提取出来,并遍历每个条目 | |
MATRIX_JSON='${{ needs.prepare-matrix.outputs.test-matrix }}' | |
echo "$MATRIX_JSON" | jq -c '.include[]' | nl -v 0 -w 1 -s ' ' | while read idx entry; do | |
IMAGE=$(echo "$entry" | jq -r '.image') | |
ARCH=$(echo "$entry" | jq -r '.arch') | |
# 从矩阵任务输出中提取对应索引的 success 状态 | |
# 这里借助 toJSON(needs.test-single-image) 获取各个矩阵 job 的输出,并用 jq 过滤出 index 为 idx 的项 | |
SUCCESS=$(echo '${{ toJSON(needs.test-single-image) }}' | jq -r --arg idx "$idx" '.[$idx].outputs.success') | |
if [ "$SUCCESS" = "true" ]; then | |
if [ "$ARCH" = "amd64" ]; then | |
echo "$IMAGE" >> x86_64_fixed_images.txt | |
else | |
echo "$IMAGE" >> arm64_fixed_images.txt | |
fi | |
else | |
echo "跳过测试失败的镜像: $IMAGE" | |
fi | |
done | |
# 去重并排序 | |
sort -u x86_64_fixed_images.txt -o x86_64_fixed_images.txt | |
sort -u arm64_fixed_images.txt -o arm64_fixed_images.txt | |
- name: Push Results | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
run: | | |
# 如果有任一固定镜像文件非空,则更新仓库 | |
if [[ -s x86_64_fixed_images.txt || -s arm64_fixed_images.txt ]]; then | |
echo "推送测试结果..." | |
git pull origin main | |
git add x86_64_fixed_images.txt arm64_fixed_images.txt | |
git commit -m "更新镜像测试结果" | |
git push | |
fi | |
# 即使推送失败也不影响后续清理 | |
continue-on-error: true | |
cleanup: | |
needs: results-updater | |
if: always() | |
runs-on: ubuntu-latest | |
steps: | |
- name: Check Test Results | |
run: | | |
echo "所有测试完成" | |
echo "查看 Actions 页面获取详细测试结果" |