Files
MoShiro/ci/binary-hash.sh
T
2026-10-08 05:43:51 +08:00

21 lines
1.1 KiB
Bash
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env bash
# 镜像里某个可执行文件的 SHA-256:ci/binary-hash.sh <镜像> <镜像里的路径>
# 节点握手时上报的就是它正在运行的那个文件的哈希,CI 把这个哈希写进签名的清单(binaries),Backend 据此认「官方二进制」。
# 先试在镜像里运行 sha256sum(同架构),不行再挂载镜像文件系统直接读。
set -euo pipefail
image="${1:?usage: binary-hash.sh <image> <path>}"; path="${2:?path in the image}"
# 镜像是本地刚构建的,不需要仓库认证;CI 里 REGISTRY_AUTH_FILE 指向的文件在登录之前还不存在,buildah 会因此报错
unset REGISTRY_AUTH_FILE
container="$(buildah from "$image")"
trap 'buildah rm "$container" >/dev/null 2>&1 || true' EXIT
if out="$(buildah run "$container" -- sha256sum "$path" 2>/dev/null)" && [[ "$out" =~ ^([0-9a-f]{64})[[:space:]] ]]; then
echo "${BASH_REMATCH[1]}"
exit 0
fi
buildah unshare bash -c '
set -euo pipefail
mount="$(buildah mount "$1")"
sha256sum "$mount$2" | cut -d" " -f1
buildah umount "$1" >/dev/null
' _ "$container" "$path"