# 兼容 Gitea Actions 与 GitHub Actions, 使用说明见 .github/README.md # 整包文件名由 .github/bundle-files.txt 一行内容决定, 上传落到仓库 Release 附件 name: containerd bundle on: workflow_dispatch: inputs: force_download: description: "重新下载已存在的安装包, 填 true" required: false default: "false" push: tags: - "v*" env: PACKAGE_NAME: Ansible-Containerd-bundle jobs: bundle: runs-on: ubuntu-latest timeout-minutes: 90 permissions: contents: write steps: - name: 1.拉取仓库 uses: actions/checkout@v4 - name: 2.读取整包文件名 run: | set -euo pipefail BUNDLE_FILE=$(grep -v -E '^[[:space:]]*#|^[[:space:]]*$' .github/bundle-files.txt | head -n 1 | tr -d '\r' | sed -E 's/^[[:space:]]+//; s/[[:space:]]+$//') if [ -z "${BUNDLE_FILE}" ]; then echo "bundle-files.txt 为空, 请写入一行整包文件名" exit 1 fi case "${BUNDLE_FILE}" in *.tar.gz) ;; *) echo "整包文件名必须以 .tar.gz 结尾: ${BUNDLE_FILE}" exit 1 ;; esac VERSION="${BUNDLE_FILE%.tar.gz}" VERSION="${VERSION#${PACKAGE_NAME}-}" mkdir -p dist { echo "BUNDLE_FILE=${BUNDLE_FILE}" echo "VERSION=${VERSION}" } > dist/build.env echo "整包: ${BUNDLE_FILE}" echo "版本: ${VERSION}" - name: 3.下载并校验安装包 env: FORCE_DOWNLOAD: ${{ inputs.force_download }} run: | set -euo pipefail if [ "${FORCE_DOWNLOAD:-false}" = "true" ]; then bash scripts/download-containerd.sh --all --force else bash scripts/download-containerd.sh --all fi - name: 4.生成校验文件并打出离线整包 run: | set -euo pipefail source dist/build.env rm -f packages/SHA256SUMS ( cd packages && sha256sum * ) > dist/SHA256SUMS cp dist/SHA256SUMS packages/SHA256SUMS STAGE="dist/Ansible-Containerd" rm -rf "${STAGE}" mkdir -p "${STAGE}" tar -cf - --exclude=./.git --exclude=./.github --exclude=./dist --exclude=./packages . | tar -xf - -C "${STAGE}" cp -a packages "${STAGE}/packages" tar -czf "dist/${BUNDLE_FILE}" -C dist Ansible-Containerd rm -rf "${STAGE}" ls -lh "dist/${BUNDLE_FILE}" echo "包内条目数: $(tar -tzf "dist/${BUNDLE_FILE}" | wc -l)" - name: 5.上传整包到仓库 Release if: ${{ !contains(github.server_url, 'github.com') }} env: RELEASE_TOKEN: ${{ secrets.RELEASE_TOKEN }} REGISTRY_USER: ${{ secrets.REGISTRY_USER }} run: | set -euo pipefail source dist/build.env API="${GITHUB_SERVER_URL}/api/v1/repos/${GITHUB_REPOSITORY}" USER_NAME="${REGISTRY_USER:-${GITHUB_ACTOR}}" TOKEN_VALUE="${RELEASE_TOKEN}" if [ -z "${TOKEN_VALUE}" ]; then echo "缺少令牌: 请配置 secrets.RELEASE_TOKEN" exit 1 fi AUTH="${USER_NAME}:${TOKEN_VALUE}" REL_JSON="dist/release.json" CODE=$(curl -s -o "${REL_JSON}" -w '%{http_code}' -u "${AUTH}" "${API}/releases/tags/${VERSION}") if [ "${CODE}" = "200" ]; then echo "Release 已存在: ${VERSION}" else echo "创建 Release: ${VERSION}" CODE=$(curl -s -o "${REL_JSON}" -w '%{http_code}' -u "${AUTH}" \ -H 'Content-Type: application/json' \ -d "{\"tag_name\":\"${VERSION}\",\"name\":\"Containerd 离线安装包 ${VERSION}\",\"draft\":false,\"prerelease\":false}" \ "${API}/releases") if [ "${CODE}" != "201" ] && [ "${CODE}" != "200" ]; then echo "创建 Release 失败, HTTP ${CODE}" cat "${REL_JSON}" exit 1 fi fi REL_ID=$(grep -o '"id":[0-9]*' "${REL_JSON}" | head -n 1 | cut -d: -f2) if [ -z "${REL_ID}" ]; then echo "未能解析 Release id" cat "${REL_JSON}" exit 1 fi echo "Release id: ${REL_ID}" ASSETS=$(curl -s -u "${AUTH}" "${API}/releases/${REL_ID}/assets") if printf '%s' "${ASSETS}" | grep -q "\"name\":\"${BUNDLE_FILE}\""; then echo "附件已存在, 跳过: ${BUNDLE_FILE}" exit 0 fi CODE=$(curl -s -o dist/upload.json -w '%{http_code}' -X POST -u "${AUTH}" \ -F "attachment=@dist/${BUNDLE_FILE}" \ "${API}/releases/${REL_ID}/assets?name=${BUNDLE_FILE}") if [ "${CODE}" != "201" ]; then echo "上传附件失败, HTTP ${CODE}" cat dist/upload.json exit 1 fi echo "已上传附件: ${BUNDLE_FILE}" - name: 6.发布到 GitHub Release if: ${{ contains(github.server_url, 'github.com') }} env: GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} run: | set -euo pipefail source dist/build.env if gh release view "${VERSION}" >/dev/null 2>&1; then echo "Release 已存在: ${VERSION}" else gh release create "${VERSION}" --title "Containerd 离线安装包 ${VERSION}" --notes "包含项目本体与全部二进制安装包, 解压即可离线使用" fi if gh release view "${VERSION}" --json assets --jq '.assets[].name' | grep -qx "${BUNDLE_FILE}"; then echo "附件已存在, 跳过: ${BUNDLE_FILE}" exit 0 fi gh release upload "${VERSION}" "dist/${BUNDLE_FILE}" echo "已上传附件: ${BUNDLE_FILE}"