diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 0973e3f..fb42dbb 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -12,12 +12,12 @@ jobs: - uses: actions/checkout@v4 with: fetch-depth: 0 # 获取完整历史,用于生成changelog - + - name: Set up Go uses: actions/setup-go@v4 with: go-version: '1.23.1' - + - name: Check VERSION file and determine release id: version run: | @@ -27,41 +27,82 @@ jobs: echo "Please create a VERSION file in the root directory with format: 1.0.0" exit 1 fi - + # 读取VERSION文件内容 CURRENT_VERSION=$(cat VERSION | tr -d '[:space:]') echo "Version in file: $CURRENT_VERSION" - + # 验证版本格式 (x.y.z) - if ! echo "$CURRENT_VERSION" | grep -qE '^[0-9]+\.[0-9]+\.[0-9]+ - + if ! echo "$CURRENT_VERSION" | grep -qE '^[0-9]+\.[0-9]+\.[0-9]+$'; then + echo "❌ Invalid version format in VERSION file: $CURRENT_VERSION" + echo "Expected format: x.y.z (e.g., 1.0.0)" + exit 1 + fi + + # 添加v前缀 + NEW_VERSION="v${CURRENT_VERSION}" + + # 获取最后一个tag + LAST_TAG=$(git describe --tags --abbrev=0 2>/dev/null || echo "v0.0.0") + echo "Last release tag: $LAST_TAG" + echo "New version: $NEW_VERSION" + + # 检查版本是否有变化 + if [ "$NEW_VERSION" = "$LAST_TAG" ]; then + echo "✅ Version $NEW_VERSION already released, skipping..." + echo "skip_release=true" >> $GITHUB_OUTPUT + exit 0 + fi + + # 检查版本是否比上一个版本新 + if [ "$LAST_TAG" != "v0.0.0" ]; then + LAST_VERSION=$(echo $LAST_TAG | sed 's/v//') + if ! printf '%s\n%s\n' "$LAST_VERSION" "$CURRENT_VERSION" | sort -V -C; then + echo "⚠️ Warning: New version $CURRENT_VERSION is not higher than last version $LAST_VERSION" + echo "This might indicate a version rollback or incorrect versioning" + fi + fi + + echo "✅ Version changed from $LAST_TAG to $NEW_VERSION, creating release..." + echo "new_version=${NEW_VERSION}" >> $GITHUB_OUTPUT + echo "current_version=${CURRENT_VERSION}" >> $GITHUB_OUTPUT + + # 生成changelog + if [ "$LAST_TAG" = "v0.0.0" ]; then + CHANGELOG=$(git log --pretty=format:"- %s (%h)" --reverse HEAD) + else + CHANGELOG=$(git log ${LAST_TAG}..HEAD --pretty=format:"- %s (%h)" --reverse) + fi + + echo "changelog<> $GITHUB_OUTPUT + echo "$CHANGELOG" >> $GITHUB_OUTPUT + echo "EOF" >> $GITHUB_OUTPUT + - name: Build binaries if: steps.version.outputs.skip_release != 'true' run: | VERSION=${{ steps.version.outputs.current_version }} mkdir -p dist - - # 构建不同平台的二进制文件 + platforms=("linux/amd64" "linux/arm64" "windows/amd64" "darwin/amd64" "darwin/arm64") - + for platform in "${platforms[@]}"; do platform_split=(${platform//\// }) GOOS=${platform_split[0]} GOARCH=${platform_split[1]} - + output_name="bamboo-exporter-v${VERSION}-${GOOS}-${GOARCH}" - if [ $GOOS = "windows" ]; then + if [ "$GOOS" = "windows" ]; then output_name+='.exe' fi - + echo "Building for $GOOS/$GOARCH..." env CGO_ENABLED=0 GOOS=$GOOS GOARCH=$GOARCH go build \ -ldflags="-s -w -X main.version=v${VERSION}" \ -o dist/$output_name . - - # 创建压缩包 + cd dist - if [ $GOOS = "windows" ]; then + if [ "$GOOS" = "windows" ]; then zip ${output_name%.exe}.zip $output_name rm $output_name else @@ -70,7 +111,7 @@ jobs: fi cd .. done - + - name: Create Release if: steps.version.outputs.skip_release != 'true' uses: actions/create-release@v1 @@ -82,11 +123,11 @@ jobs: release_name: Release ${{ steps.version.outputs.new_version }} body: | ## 🚀 What's New - + ${{ steps.version.outputs.changelog }} - + ## 📦 Download - + 选择适合你平台的二进制文件下载: - **Linux (x64)**: `bamboo-exporter-${{ steps.version.outputs.new_version }}-linux-amd64.tar.gz` - **Linux (ARM64)**: `bamboo-exporter-${{ steps.version.outputs.new_version }}-linux-arm64.tar.gz` @@ -95,12 +136,12 @@ jobs: - **macOS (Apple Silicon)**: `bamboo-exporter-${{ steps.version.outputs.new_version }}-darwin-arm64.tar.gz` draft: false prerelease: false - + - name: Upload Release Assets if: steps.version.outputs.skip_release != 'true' run: | upload_url="${{ steps.create_release.outputs.upload_url }}" - + for file in dist/*; do filename=$(basename "$file") echo "Uploading $filename..." @@ -108,47 +149,4 @@ jobs: -H "Content-Type: application/octet-stream" \ --data-binary @"$file" \ "${upload_url%{?name,label}*}?name=$filename" - done; then - echo "❌ Invalid version format in VERSION file: $CURRENT_VERSION" - echo "Expected format: x.y.z (e.g., 1.0.0)" - exit 1 - fi - - # 添加v前缀 - NEW_VERSION="v${CURRENT_VERSION}" - - # 获取最后一个tag - LAST_TAG=$(git describe --tags --abbrev=0 2>/dev/null || echo "v0.0.0") - echo "Last release tag: $LAST_TAG" - echo "New version: $NEW_VERSION" - - # 检查版本是否有变化 - if [ "$NEW_VERSION" = "$LAST_TAG" ]; then - echo "✅ Version $NEW_VERSION already released, skipping..." - echo "skip_release=true" >> $GITHUB_OUTPUT - exit 0 - fi - - # 检查版本是否比上一个版本新 - if [ "$LAST_TAG" != "v0.0.0" ]; then - LAST_VERSION=$(echo $LAST_TAG | sed 's/v//') - if ! printf '%s\n%s\n' "$LAST_VERSION" "$CURRENT_VERSION" | sort -V -C; then - echo "⚠️ Warning: New version $CURRENT_VERSION is not higher than last version $LAST_VERSION" - echo "This might indicate a version rollback or incorrect versioning" - fi - fi - - echo "✅ Version changed from $LAST_TAG to $NEW_VERSION, creating release..." - echo "new_version=${NEW_VERSION}" >> $GITHUB_OUTPUT - echo "current_version=${CURRENT_VERSION}" >> $GITHUB_OUTPUT - - # 生成changelog (从上个tag到现在的提交) - if [ "$LAST_TAG" = "v0.0.0" ]; then - CHANGELOG=$(git log --pretty=format:"- %s (%h)" --reverse HEAD) - else - CHANGELOG=$(git log ${LAST_TAG}..HEAD --pretty=format:"- %s (%h)" --reverse) - fi - - echo "changelog<> $GITHUB_OUTPUT - echo "$CHANGELOG" >> $GITHUB_OUTPUT - echo "EOF" >> $GITHUB_OUTPUT \ No newline at end of file + done