|
| 1 | +# This workflow requires Ubuntu 22.04 or 24.04 |
| 2 | + |
| 3 | +name: Auto Tag & Release |
| 4 | + |
| 5 | +on: |
| 6 | + push: |
| 7 | + branches: |
| 8 | + - master |
| 9 | + - main |
| 10 | + tags: |
| 11 | + - "v*" |
| 12 | + paths-ignore: |
| 13 | + - "**.md" |
| 14 | + - "LICENSE" |
| 15 | + - ".gitignore" |
| 16 | + workflow_call: {} |
| 17 | + |
| 18 | +permissions: |
| 19 | + contents: write |
| 20 | + packages: write |
| 21 | + actions: write |
| 22 | + |
| 23 | +jobs: |
| 24 | + pre_job: |
| 25 | + runs-on: ubuntu-22.04 |
| 26 | + outputs: |
| 27 | + should_skip: ${{ steps.skip_check.outputs.should_skip }} |
| 28 | + steps: |
| 29 | + - id: skip_check |
| 30 | + uses: fkirc/skip-duplicate-actions@v5.3.0 |
| 31 | + with: |
| 32 | + cancel_others: "true" |
| 33 | + concurrent_skipping: "same_content" |
| 34 | + |
| 35 | + auto-tag-release: |
| 36 | + needs: pre_job |
| 37 | + if: | |
| 38 | + needs.pre_job.outputs.should_skip != 'true' || |
| 39 | + startsWith(github.ref, 'refs/tags/v') |
| 40 | + runs-on: ubuntu-22.04 |
| 41 | + timeout-minutes: 15 |
| 42 | + outputs: |
| 43 | + version: ${{ steps.get_latest_tag.outputs.version }} |
| 44 | + concurrency: |
| 45 | + group: ${{ github.workflow }}-${{ github.ref }} |
| 46 | + cancel-in-progress: true |
| 47 | + |
| 48 | + steps: |
| 49 | + - name: Checkout |
| 50 | + uses: actions/checkout@v3 |
| 51 | + with: |
| 52 | + fetch-depth: 0 |
| 53 | + lfs: true |
| 54 | + submodules: recursive |
| 55 | + |
| 56 | + - name: Setup Go |
| 57 | + uses: actions/setup-go@v3 |
| 58 | + with: |
| 59 | + go-version: "1.21" |
| 60 | + check-latest: true |
| 61 | + cache: true |
| 62 | + |
| 63 | + - name: Cache |
| 64 | + uses: actions/cache@v3 |
| 65 | + with: |
| 66 | + path: | |
| 67 | + ~/.cache/go-build |
| 68 | + ~/go/pkg/mod |
| 69 | + ~/.cache/git |
| 70 | + key: ${{ runner.os }}-build-${{ hashFiles('**/go.sum') }} |
| 71 | + restore-keys: | |
| 72 | + ${{ runner.os }}-build- |
| 73 | + ${{ runner.os }}- |
| 74 | +
|
| 75 | + # 只在非tag推送时执行自动打tag |
| 76 | + - name: Get latest tag |
| 77 | + if: ${{ !startsWith(github.ref, 'refs/tags/v') }} |
| 78 | + id: get_latest_tag |
| 79 | + run: | |
| 80 | + set -euo pipefail |
| 81 | + git fetch --tags --force || { |
| 82 | + echo "::error::Failed to fetch tags" |
| 83 | + exit 1 |
| 84 | + } |
| 85 | + latest_tag=$(git tag -l 'v*' --sort=-v:refname | head -n 1) |
| 86 | + if [ -z "$latest_tag" ]; then |
| 87 | + new_version="v0.1.0" |
| 88 | + else |
| 89 | + major=$(echo $latest_tag | cut -d. -f1) |
| 90 | + minor=$(echo $latest_tag | cut -d. -f2) |
| 91 | + patch=$(echo $latest_tag | cut -d. -f3) |
| 92 | + new_patch=$((patch + 1)) |
| 93 | + new_version="$major.$minor.$new_patch" |
| 94 | + fi |
| 95 | + echo "version=$new_version" >> "$GITHUB_OUTPUT" |
| 96 | + echo "Generated version: $new_version" |
| 97 | +
|
| 98 | + - name: Validate version |
| 99 | + if: ${{ !startsWith(github.ref, 'refs/tags/v') }} |
| 100 | + run: | |
| 101 | + set -euo pipefail |
| 102 | + new_tag="${{ steps.get_latest_tag.outputs.version }}" |
| 103 | + echo "Validating version: $new_tag" |
| 104 | + if [[ ! $new_tag =~ ^v[0-9]+\.[0-9]+\.[0-9]+$ ]]; then |
| 105 | + echo "::error::Invalid version format: $new_tag" |
| 106 | + exit 1 |
| 107 | + fi |
| 108 | + major=$(echo $new_tag | cut -d. -f1 | tr -d 'v') |
| 109 | + minor=$(echo $new_tag | cut -d. -f2) |
| 110 | + patch=$(echo $new_tag | cut -d. -f3) |
| 111 | + if [[ $major -gt 99 || $minor -gt 99 || $patch -gt 999 ]]; then |
| 112 | + echo "::error::Version numbers out of valid range" |
| 113 | + exit 1 |
| 114 | + fi |
| 115 | + echo "Version validation passed" |
| 116 | +
|
| 117 | + - name: Create new tag |
| 118 | + if: ${{ !startsWith(github.ref, 'refs/tags/v') }} |
| 119 | + env: |
| 120 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 121 | + run: | |
| 122 | + new_tag=${{ steps.get_latest_tag.outputs.version }} |
| 123 | + git config --global user.name 'github-actions[bot]' |
| 124 | + git config --global user.email 'github-actions[bot]@users.noreply.github.com' |
| 125 | + git tag -a $new_tag -m "Release $new_tag" |
| 126 | + git push origin $new_tag |
| 127 | +
|
| 128 | + # 在 Run GoReleaser 之前添加配置检查步骤 |
| 129 | + - name: Check GoReleaser config |
| 130 | + run: | |
| 131 | + if [ ! -f ".goreleaser.yml" ] && [ ! -f ".goreleaser.yaml" ]; then |
| 132 | + echo "::error::GoReleaser configuration file not found" |
| 133 | + exit 1 |
| 134 | + fi |
| 135 | +
|
| 136 | + # 添加依赖检查步骤 |
| 137 | + - name: Check Dependencies |
| 138 | + run: | |
| 139 | + go mod verify |
| 140 | + go mod download |
| 141 | + # 如果使用 vendor 模式,则执行以下命令 |
| 142 | + if [ -d "vendor" ]; then |
| 143 | + go mod vendor |
| 144 | + fi |
| 145 | +
|
| 146 | + # 添加构建环境准备步骤 |
| 147 | + - name: Prepare Build Environment |
| 148 | + run: | |
| 149 | + echo "Building version: ${VERSION:-development}" |
| 150 | + echo "GOOS=${GOOS:-$(go env GOOS)}" >> $GITHUB_ENV |
| 151 | + echo "GOARCH=${GOARCH:-$(go env GOARCH)}" >> $GITHUB_ENV |
| 152 | + echo "GO111MODULE=on" >> $GITHUB_ENV |
| 153 | +
|
| 154 | + # 添加清理步骤 |
| 155 | + - name: Cleanup workspace |
| 156 | + run: | |
| 157 | + rm -rf /tmp/go/ |
| 158 | + rm -rf .cache/ |
| 159 | + rm -rf dist/ |
| 160 | + git clean -fdx |
| 161 | + git status |
| 162 | +
|
| 163 | + # 修改 GoReleaser 步骤 |
| 164 | + - name: Run GoReleaser |
| 165 | + if: ${{ startsWith(github.ref, 'refs/tags/v') || (success() && steps.get_latest_tag.outputs.version != '') }} |
| 166 | + uses: goreleaser/goreleaser-action@v3 |
| 167 | + with: |
| 168 | + distribution: goreleaser |
| 169 | + version: latest |
| 170 | + args: release --clean --timeout 60m |
| 171 | + env: |
| 172 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 173 | + VERSION: ${{ steps.get_latest_tag.outputs.version }} |
| 174 | + CGO_ENABLED: 0 |
| 175 | + GOPATH: /tmp/go |
| 176 | + GOROOT: ${{ env.GOROOT }} |
| 177 | + GOCACHE: /tmp/.cache/go-build |
| 178 | + GOMODCACHE: /tmp/go/pkg/mod |
| 179 | + GORELEASER_DEBUG: 1 |
| 180 | + GORELEASER_CURRENT_TAG: ${{ steps.get_latest_tag.outputs.version }} |
| 181 | + # 添加额外的构建信息 |
| 182 | + BUILD_TIME: ${{ steps.get_latest_tag.outputs.version }} |
| 183 | + BUILD_COMMIT: ${{ github.sha }} |
| 184 | + |
| 185 | + # 优化 vendor 同步步骤 |
| 186 | + - name: Sync vendor directory |
| 187 | + run: | |
| 188 | + echo "Syncing vendor directory..." |
| 189 | + go mod tidy |
| 190 | + go mod vendor |
| 191 | + go mod verify |
| 192 | + # 验证 vendor 目录 |
| 193 | + if [ -d "vendor" ]; then |
| 194 | + echo "Verifying vendor directory..." |
| 195 | + go mod verify |
| 196 | + # 检查是否有未跟踪的文件 |
| 197 | + if [ -n "$(git status --porcelain vendor/)" ]; then |
| 198 | + echo "Warning: Vendor directory has uncommitted changes" |
| 199 | + git status vendor/ |
| 200 | + fi |
| 201 | + fi |
| 202 | +
|
| 203 | + # 添加错误检查步骤 |
| 204 | + - name: Check GoReleaser Output |
| 205 | + if: failure() |
| 206 | + run: | |
| 207 | + echo "::group::GoReleaser Debug Info" |
| 208 | + cat dist/artifacts.json || true |
| 209 | + echo "::endgroup::" |
| 210 | +
|
| 211 | + echo "::group::GoReleaser Config" |
| 212 | + cat .goreleaser.yml |
| 213 | + echo "::endgroup::" |
| 214 | +
|
| 215 | + echo "::group::Environment Info" |
| 216 | + go version |
| 217 | + go env |
| 218 | + echo "::endgroup::" |
| 219 | +
|
| 220 | + - name: Set Release Version |
| 221 | + if: startsWith(github.ref, 'refs/tags/v') |
| 222 | + run: | |
| 223 | + echo "VERSION=${GITHUB_REF#refs/tags/}" >> $GITHUB_ENV |
| 224 | +
|
| 225 | + # 改进验证步骤 |
| 226 | + - name: Verify Release |
| 227 | + if: ${{ startsWith(github.ref, 'refs/tags/v') || (success() && steps.get_latest_tag.outputs.version != '') }} |
| 228 | + run: | |
| 229 | + echo "Verifying release artifacts..." |
| 230 | + if [ ! -d "dist" ]; then |
| 231 | + echo "::error::Release artifacts not found" |
| 232 | + exit 1 |
| 233 | + fi |
| 234 | + # 验证生成的二进制文件 |
| 235 | + for file in dist/cursor-id-modifier_*; do |
| 236 | + if [ -f "$file" ]; then |
| 237 | + echo "Verifying: $file" |
| 238 | + if [[ "$file" == *.exe ]]; then |
| 239 | + # Windows 二进制文件检查 |
| 240 | + if ! [ -x "$file" ]; then |
| 241 | + echo "::error::$file is not executable" |
| 242 | + exit 1 |
| 243 | + fi |
| 244 | + else |
| 245 | + # Unix 二进制文件检查 |
| 246 | + if ! [ -x "$file" ]; then |
| 247 | + echo "::error::$file is not executable" |
| 248 | + exit 1 |
| 249 | + fi |
| 250 | + fi |
| 251 | + fi |
| 252 | + done |
| 253 | +
|
| 254 | + - name: Notify on failure |
| 255 | + if: failure() |
| 256 | + run: | |
| 257 | + echo "::error::Release process failed" |
| 258 | +
|
| 259 | + # 修改构建摘要步骤 |
| 260 | + - name: Build Summary |
| 261 | + if: always() |
| 262 | + run: | |
| 263 | + echo "## Build Summary" >> $GITHUB_STEP_SUMMARY |
| 264 | + echo "- Go Version: $(go version)" >> $GITHUB_STEP_SUMMARY |
| 265 | + echo "- Release Version: ${VERSION:-N/A}" >> $GITHUB_STEP_SUMMARY |
| 266 | + echo "- GPG Signing: Disabled" >> $GITHUB_STEP_SUMMARY |
| 267 | + echo "- Build Status: ${{ job.status }}" >> $GITHUB_STEP_SUMMARY |
| 268 | +
|
| 269 | + if [ -d "dist" ]; then |
| 270 | + echo "### Generated Artifacts" >> $GITHUB_STEP_SUMMARY |
| 271 | + ls -lh dist/ | awk '{print "- "$9" ("$5")"}' >> $GITHUB_STEP_SUMMARY |
| 272 | + fi |
0 commit comments