mirror of
https://github.com/yanhao98/composite-actions.git
synced 2026-11-23 00:56:47 +08:00
119 lines
3.9 KiB
YAML
119 lines
3.9 KiB
YAML
name: 'Setup Skopeo'
|
||
description: '安装并配置 Skopeo CLI(优先 APT 源安装带重试,失败自动 Fallback 至动态拉取 SHA256 严格校验的预编译二进制)'
|
||
author: 'yanhao98'
|
||
|
||
inputs:
|
||
version:
|
||
description: '二进制回退时使用的 Skopeo 版本(默认 v1.23.0)'
|
||
required: false
|
||
default: 'v1.23.0'
|
||
|
||
runs:
|
||
using: 'composite'
|
||
steps:
|
||
- name: 检查并安装 Skopeo
|
||
shell: bash
|
||
run: |
|
||
set -euo pipefail
|
||
|
||
if command -v skopeo >/dev/null 2>&1; then
|
||
echo "skopeo is already installed"
|
||
skopeo --version
|
||
exit 0
|
||
fi
|
||
|
||
# Ubuntu ARM 官方源曾连续超时;先切换 HTTPS,再对完整的 update+install 做三轮重试
|
||
if [ -f /etc/apt/sources.list ]; then
|
||
sed -i 's#http://ports.ubuntu.com/ubuntu-ports#https://ports.ubuntu.com/ubuntu-ports#g' /etc/apt/sources.list
|
||
fi
|
||
for source_file in /etc/apt/sources.list.d/*.sources; do
|
||
[ -e "$source_file" ] || continue
|
||
sed -i 's#http://ports.ubuntu.com/ubuntu-ports#https://ports.ubuntu.com/ubuntu-ports#g' "$source_file"
|
||
done
|
||
|
||
apt_installed=false
|
||
for attempt in 1 2 3; do
|
||
echo "apt 安装 skopeo:第 ${attempt}/3 次"
|
||
if apt-get \
|
||
-o Acquire::Retries=1 \
|
||
-o Acquire::http::Timeout=20 \
|
||
-o Acquire::https::Timeout=20 \
|
||
update -qq \
|
||
&& DEBIAN_FRONTEND=noninteractive apt-get \
|
||
-o Acquire::Retries=1 \
|
||
-o Acquire::http::Timeout=20 \
|
||
-o Acquire::https::Timeout=20 \
|
||
install -qq -y --no-install-recommends skopeo; then
|
||
apt_installed=true
|
||
break
|
||
fi
|
||
|
||
if [ "$attempt" -lt 3 ]; then
|
||
if [ "$attempt" -eq 1 ]; then
|
||
sleep 10
|
||
else
|
||
sleep 30
|
||
fi
|
||
fi
|
||
done
|
||
|
||
if [ "$apt_installed" != true ]; then
|
||
echo "apt 连续安装失败,启用动态 SHA256 校验的预编译二进制 fallback"
|
||
version="${{ inputs.version }}"
|
||
case "$(uname -m)" in
|
||
x86_64|amd64)
|
||
arch="amd64"
|
||
;;
|
||
aarch64|arm64)
|
||
arch="arm64"
|
||
;;
|
||
*)
|
||
echo "不支持的 Runner 架构:$(uname -m)" >&2
|
||
exit 1
|
||
;;
|
||
esac
|
||
|
||
base_url="https://github.com/lework/skopeo-binary/releases/download/${version}/skopeo-linux-${arch}"
|
||
binary_path="$(mktemp)"
|
||
checksum_path="$(mktemp)"
|
||
trap 'rm -f "$binary_path" "$checksum_path"' EXIT
|
||
|
||
# 1. 下载二进制文件
|
||
echo "正在下载 Skopeo 二进制包 (${version}, linux/${arch}) ..."
|
||
curl \
|
||
--fail \
|
||
--location \
|
||
--retry 5 \
|
||
--retry-all-errors \
|
||
--connect-timeout 10 \
|
||
--max-time 180 \
|
||
"${base_url}" \
|
||
--output "$binary_path"
|
||
|
||
# 2. 动态下载上游对应版本的 SHA256 校验文件
|
||
echo "正在下载对应版本的 SHA256 校验文件 ..."
|
||
curl \
|
||
--fail \
|
||
--location \
|
||
--retry 5 \
|
||
--retry-all-errors \
|
||
--connect-timeout 10 \
|
||
--max-time 60 \
|
||
"${base_url}.sha256" \
|
||
--output "$checksum_path"
|
||
|
||
# 3. 严格比对哈希值,确保文件未损坏且未被篡改
|
||
expected_sha256="$(awk '{print $1}' "$checksum_path")"
|
||
if [ -z "$expected_sha256" ]; then
|
||
echo "未能从上游获取到有效的 SHA256 校验码" >&2
|
||
exit 1
|
||
fi
|
||
|
||
printf '%s %s\n' "$expected_sha256" "$binary_path" | sha256sum --check --strict -
|
||
echo "SHA256 校验通过:${expected_sha256}"
|
||
|
||
install -m 0755 "$binary_path" /usr/local/bin/skopeo
|
||
fi
|
||
|
||
skopeo --version
|