Build and Release #60
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Build and Release | |
| on: | |
| schedule: | |
| - cron: '29 11 * * 1' # 每周一 11:29 UTC | |
| workflow_dispatch: # 保留手动触发功能 | |
| push: | |
| tags: | |
| - 'v*' # 保留标签触发功能 | |
| jobs: | |
| build: | |
| name: Build and Release | |
| runs-on: ubuntu-latest | |
| permissions: | |
| contents: write # 添加写入权限 | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v4 # 更新到 v4 | |
| - name: Set up Go | |
| uses: actions/setup-go@v5 # 更新到 v5 | |
| with: | |
| go-version: '1.21' # 更新 Go 版本 | |
| - name: Generate version | |
| id: version | |
| run: | | |
| if [[ $GITHUB_REF == refs/tags/* ]]; then | |
| echo "VERSION=${GITHUB_REF#refs/tags/}" >> $GITHUB_ENV | |
| else | |
| # 生成基于周数和时间的版本号:v年份.周数.HHMM | |
| CURRENT_TIME=$(date +'%H%M') | |
| VERSION="v$(date +'%Y.%V').${CURRENT_TIME}" | |
| # 检查标签是否存在 | |
| if git rev-parse "$VERSION" >/dev/null 2>&1; then | |
| echo "Tag already exists, adding random suffix..." | |
| RANDOM_SUFFIX=$(head /dev/urandom | tr -dc 'a-z0-9' | head -c 4) | |
| VERSION="${VERSION}-${RANDOM_SUFFIX}" | |
| fi | |
| # 保存版本号到环境变量 | |
| echo "VERSION=$VERSION" >> $GITHUB_ENV | |
| # 配置 git | |
| git config --global user.name 'GitHub Actions' | |
| git config --global user.email 'actions@github.com' | |
| # 创建并推送标签 | |
| echo "Creating tag: $VERSION" | |
| git tag "$VERSION" | |
| git push origin "$VERSION" | |
| fi | |
| - name: Initialize Go modules | |
| run: | | |
| if [ ! -f "go.mod" ]; then | |
| echo "Initializing new Go module..." | |
| go mod init HashSum | |
| else | |
| echo "Go module already initialized" | |
| fi | |
| # 确保依赖是最新的 | |
| echo "Downloading and verifying dependencies..." | |
| go mod tidy | |
| if [ $? -ne 0 ]; then | |
| echo "Failed to download dependencies" | |
| exit 1 | |
| fi | |
| # 验证依赖 | |
| go mod verify | |
| if [ $? -ne 0 ]; then | |
| echo "Module verification failed" | |
| exit 1 | |
| fi | |
| - name: Build for all platforms | |
| run: | | |
| platforms=("windows" "linux" "darwin") | |
| archs=("amd64" "386" "arm64") | |
| for platform in "${platforms[@]}"; do | |
| for arch in "${archs[@]}"; do | |
| if [ "$platform-$arch" != "darwin-386" ]; then | |
| echo "Building for $platform/$arch..." | |
| # 设置输出目录 | |
| output_dir="build/${platform}-${arch}-${VERSION}" | |
| mkdir -p "$output_dir" | |
| # 设置输出文件名 | |
| if [ "$platform" = "windows" ]; then | |
| output_name="HashSum.exe" | |
| else | |
| output_name="HashSum" | |
| fi | |
| # 构建 | |
| GOOS=$platform GOARCH=$arch go build -o "$output_dir/$output_name" . | |
| # 复制README和LICENSE | |
| cp README.md "$output_dir/" | |
| cp LICENSE "$output_dir/" 2>/dev/null || true | |
| # 创建独立可执行文件的副本 | |
| standalone_dir="build/standalone" | |
| mkdir -p "$standalone_dir" | |
| if [ "$platform" = "windows" ]; then | |
| cp "$output_dir/$output_name" "$standalone_dir/HashSum-${platform}-${arch}-${VERSION}.exe" | |
| else | |
| cp "$output_dir/$output_name" "$standalone_dir/HashSum-${platform}-${arch}-${VERSION}" | |
| chmod +x "$standalone_dir/HashSum-${platform}-${arch}-${VERSION}" | |
| fi | |
| # 创建压缩包 | |
| if [ "$platform" = "windows" ]; then | |
| (cd build && zip -r "HashSum-${platform}-${arch}-${VERSION}.zip" "${platform}-${arch}-${VERSION}") | |
| else | |
| (cd build && tar czf "HashSum-${platform}-${arch}-${VERSION}.tar.gz" "${platform}-${arch}-${VERSION}") | |
| fi | |
| fi | |
| done | |
| done | |
| - name: Create Release | |
| id: create_release | |
| uses: softprops/action-gh-release@v1 | |
| with: | |
| name: HashSum ${{ env.VERSION }} | |
| draft: false | |
| prerelease: false | |
| generate_release_notes: true | |
| tag_name: ${{ env.VERSION }} | |
| body: | | |
| HashSum 自动发布版本 ${{ env.VERSION }} | |
| 发布时间:${{ github.event.head_commit.timestamp }} | |
| ### 更新内容 | |
| - 自动周更版本 | |
| - 包含所有平台的构建版本 | |
| ### 支持平台 | |
| - Windows (x86, x64, ARM64) | |
| - Linux (x86, x64, ARM64) | |
| - macOS (x64, ARM64) | |
| ### 下载说明 | |
| 1. 完整版本(包含README和LICENSE): | |
| - Windows: `HashSum-windows-{arch}-${VERSION}.zip` | |
| - Linux/macOS: `HashSum-{platform}-{arch}-${VERSION}.tar.gz` | |
| 2. 独立可执行文件: | |
| - Windows: `HashSum-windows-{arch}-${VERSION}.exe` | |
| - Linux/macOS: `HashSum-{platform}-{arch}-${VERSION}` | |
| files: | | |
| build/*.zip | |
| build/*.tar.gz | |
| build/standalone/* | |
| env: | |
| GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| - name: Upload Build Artifacts | |
| uses: actions/upload-artifact@v4 | |
| with: | |
| name: builds | |
| path: | | |
| build/*.zip | |
| build/*.tar.gz | |
| build/standalone/ | |
| retention-days: 5 | |
| - name: Send Feishu Notification | |
| if: always() # 无论构建成功还是失败都发送通知 | |
| run: | | |
| # 获取当前时间 | |
| CURRENT_DATE=$(date '+%Y/%m/%d %H:%M') | |
| # 设置通知状态 | |
| if [ ${{ job.status }} == "success" ]; then | |
| STATUS="发布成功" | |
| ICON="✅" | |
| else | |
| STATUS="发布失败" | |
| ICON="❌" | |
| fi | |
| # 构建通知内容 | |
| NOTIFICATION_CONTENT="{ | |
| \"msg_type\": \"post\", | |
| \"content\": { | |
| \"post\": { | |
| \"zh_cn\": { | |
| \"title\": \"${ICON} HashSum发布通知\", | |
| \"content\": [ | |
| [ | |
| { | |
| \"tag\": \"text\", | |
| \"text\": \"名称:程序 HashSum 已发布\\n发布日期: ${CURRENT_DATE}\\n发布结果:${STATUS}!\" | |
| } | |
| ] | |
| ] | |
| } | |
| } | |
| } | |
| }" | |
| # 发送通知到飞书 | |
| curl -X POST -H "Content-Type: application/json" \ | |
| -d "${NOTIFICATION_CONTENT}" \ | |
| ${{ secrets.FEISHU_WEBHOOK_URL }} | |
| env: | |
| TZ: 'Asia/Shanghai' # 设置时区为中国时区 | |