|
| 1 | +name: release |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + tags: ["v*"] |
| 6 | + |
| 7 | +permissions: |
| 8 | + contents: read |
| 9 | + |
| 10 | +concurrency: |
| 11 | + # 发布不允许被后续运行打断。 |
| 12 | + group: release-${{ github.ref }} |
| 13 | + cancel-in-progress: false |
| 14 | + |
| 15 | +jobs: |
| 16 | + # 复用测试工作流的全部门禁(lint / test / 协议一致性),门禁不过不发布。 |
| 17 | + test: |
| 18 | + uses: ./.github/workflows/test.yaml |
| 19 | + permissions: |
| 20 | + contents: read |
| 21 | + |
| 22 | + build: |
| 23 | + needs: test |
| 24 | + runs-on: ubuntu-latest |
| 25 | + strategy: |
| 26 | + fail-fast: false |
| 27 | + matrix: |
| 28 | + include: |
| 29 | + - goos: darwin |
| 30 | + goarch: amd64 |
| 31 | + arch: x86_64 |
| 32 | + format: tar.gz |
| 33 | + - goos: darwin |
| 34 | + goarch: arm64 |
| 35 | + arch: arm64 |
| 36 | + format: tar.gz |
| 37 | + - goos: linux |
| 38 | + goarch: amd64 |
| 39 | + arch: x86_64 |
| 40 | + format: tar.gz |
| 41 | + - goos: linux |
| 42 | + goarch: arm64 |
| 43 | + arch: arm64 |
| 44 | + format: tar.gz |
| 45 | + - goos: windows |
| 46 | + goarch: amd64 |
| 47 | + arch: x86_64 |
| 48 | + format: zip |
| 49 | + - goos: windows |
| 50 | + goarch: arm64 |
| 51 | + arch: arm64 |
| 52 | + format: zip |
| 53 | + steps: |
| 54 | + - uses: actions/checkout@v5 |
| 55 | + - uses: actions/setup-go@v6 |
| 56 | + with: |
| 57 | + go-version-file: go.mod |
| 58 | + cache: true |
| 59 | + - name: 构建并打包 |
| 60 | + env: |
| 61 | + GOOS: ${{ matrix.goos }} |
| 62 | + GOARCH: ${{ matrix.goarch }} |
| 63 | + CGO_ENABLED: "0" |
| 64 | + run: | |
| 65 | + set -euo pipefail |
| 66 | + VERSION="${GITHUB_REF_NAME#v}" |
| 67 | + BUILD_DATE=$(git show -s --format=%cI HEAD) |
| 68 | + COMMIT_TIMESTAMP=$(git show -s --format=%ct HEAD) |
| 69 | + PACKAGE="sctl_${VERSION}_${GOOS}_${{ matrix.arch }}" |
| 70 | + STAGE="dist/${PACKAGE}" |
| 71 | + BINARY=sctl |
| 72 | + if [ "$GOOS" = windows ]; then |
| 73 | + BINARY=sctl.exe |
| 74 | + fi |
| 75 | +
|
| 76 | + mkdir -p "$STAGE/docs" |
| 77 | + go build -trimpath -ldflags "-s -w \ |
| 78 | + -X github.com/scriptscat/sctl/internal/cli.Version=${VERSION} \ |
| 79 | + -X github.com/scriptscat/sctl/internal/cli.Commit=${GITHUB_SHA} \ |
| 80 | + -X github.com/scriptscat/sctl/internal/cli.BuildDate=${BUILD_DATE}" \ |
| 81 | + -o "$STAGE/$BINARY" ./cmd/sctl |
| 82 | + cp README.md LICENSE* "$STAGE/" |
| 83 | + cp -R docs/. "$STAGE/docs/" |
| 84 | + find "$STAGE" -exec touch -d "@${COMMIT_TIMESTAMP}" {} + |
| 85 | +
|
| 86 | + if [ "${{ matrix.format }}" = zip ]; then |
| 87 | + (cd dist && zip -X -q -r "${PACKAGE}.zip" "$PACKAGE") |
| 88 | + else |
| 89 | + tar --sort=name --mtime="@${COMMIT_TIMESTAMP}" --owner=0 --group=0 --numeric-owner \ |
| 90 | + -C dist -czf "dist/${PACKAGE}.tar.gz" "$PACKAGE" |
| 91 | + fi |
| 92 | + - uses: actions/upload-artifact@v7 |
| 93 | + with: |
| 94 | + name: release-${{ matrix.goos }}-${{ matrix.goarch }} |
| 95 | + path: dist/*.${{ matrix.format }} |
| 96 | + if-no-files-found: error |
| 97 | + |
| 98 | + release: |
| 99 | + name: Create Release |
| 100 | + needs: build |
| 101 | + runs-on: ubuntu-latest |
| 102 | + permissions: |
| 103 | + contents: write # 创建 GitHub Release 并上传产物 |
| 104 | + id-token: write # 构建来源证明(build provenance)签名 |
| 105 | + attestations: write |
| 106 | + steps: |
| 107 | + - uses: actions/checkout@v5 |
| 108 | + - uses: actions/download-artifact@v8 |
| 109 | + with: |
| 110 | + path: artifacts |
| 111 | + pattern: release-* |
| 112 | + merge-multiple: true |
| 113 | + - name: 生成校验和 |
| 114 | + run: cd artifacts && sha256sum * > checksums.txt |
| 115 | + - name: 判断是否为预发布 |
| 116 | + id: prerelease |
| 117 | + env: |
| 118 | + TAG: ${{ github.ref_name }} |
| 119 | + run: | |
| 120 | + if [[ "$TAG" == *-* ]]; then |
| 121 | + echo "flag=--prerelease" >> "$GITHUB_OUTPUT" |
| 122 | + fi |
| 123 | + - name: 创建 GitHub 草稿 Release |
| 124 | + env: |
| 125 | + GH_TOKEN: ${{ github.token }} |
| 126 | + PRERELEASE_FLAG: ${{ steps.prerelease.outputs.flag }} |
| 127 | + run: | |
| 128 | + gh release create "$GITHUB_REF_NAME" \ |
| 129 | + --draft \ |
| 130 | + --title "$GITHUB_REF_NAME" \ |
| 131 | + --generate-notes \ |
| 132 | + $PRERELEASE_FLAG \ |
| 133 | + artifacts/* |
| 134 | + - name: 生成构建来源证明 |
| 135 | + # 只对 checksums.txt 签名即可:它覆盖了全部产物的 sha256, |
| 136 | + # 用户先 `sha256sum -c` 再 `gh attestation verify checksums.txt` 就形成完整信任链。 |
| 137 | + uses: actions/attest-build-provenance@v4 |
| 138 | + with: |
| 139 | + subject-path: artifacts/checksums.txt |
| 140 | + - name: 输出发布摘要 |
| 141 | + run: | |
| 142 | + { |
| 143 | + echo "## sctl ${GITHUB_REF_NAME} 发布产物" |
| 144 | + echo |
| 145 | + echo '```' |
| 146 | + cat artifacts/checksums.txt |
| 147 | + echo '```' |
| 148 | + echo |
| 149 | + echo "Release 以**草稿**形式创建,确认 changelog 后手动点击 Publish。" |
| 150 | + } >> "$GITHUB_STEP_SUMMARY" |
0 commit comments