Skip to content

Go Build and Release #1

Go Build and Release

Go Build and Release #1

Workflow file for this run

name: Go Build and Release
on:
push:
tags:
- 'v*' # Trigger on version tags (比如 v1.2.3)
branches:
- main # 在主分支的推送时也触发
workflow_dispatch:
inputs:
tag_name:
description: 'Tag name for the release'
required: true
default: 'v1.2.3' # 默认版本号
permissions:
contents: write # Allow writing to GitHub releases
env:
PROJECT_NAME: ${{ github.event.repository.name }}
BUILD_DATE: ${{ github.run_id }}
VERSION: 'v2.0.0-beta.3' # 固定版本号
RELEASE_DIR: release
jobs:
build:
name: Build and Release
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0 # 获取完整的 Git 历史
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version-file: 'go.mod' # 使用 go.mod 文件指定 Go 版本
cache: true # 启用依赖缓存
- name: Get dependencies
run: go mod download
- name: Prepare Release Directory
run: mkdir -p ${{ env.RELEASE_DIR }}
# Build steps for different platforms
- name: Build Linux AMD64
run: |
CGO_ENABLED=1 GOOS=linux GOARCH=amd64 go build \
-ldflags="-X 'main.Version=${{ env.VERSION }}' -X 'main.BuildDate=${{ env.BUILD_DATE }}'" \
-o ${{ env.RELEASE_DIR }}/${{ env.PROJECT_NAME }}_${{ env.BUILD_DATE }}_linux_amd64
- name: Build Linux ARM64
run: |
CGO_ENABLED=1 GOOS=linux GOARCH=arm64 go build \
-ldflags="-X 'main.Version=${{ env.VERSION }}' -X 'main.BuildDate=${{ env.BUILD_DATE }}'" \
-o ${{ env.RELEASE_DIR }}/${{ env.PROJECT_NAME }}_${{ env.BUILD_DATE }}_linux_arm64
- name: Build Windows AMD64
run: |
CGO_ENABLED=1 GOOS=windows GOARCH=amd64 go build \
-ldflags="-X 'main.Version=${{ env.VERSION }}' -X 'main.BuildDate=${{ env.BUILD_DATE }}'" \
-o ${{ env.RELEASE_DIR }}/${{ env.PROJECT_NAME }}_${{ env.BUILD_DATE }}_windows_amd64.exe
- name: Build Windows ARM64
run: |
CGO_ENABLED=1 GOOS=windows GOARCH=arm64 go build \
-ldflags="-X 'main.Version=${{ env.VERSION }}' -X 'main.BuildDate=${{ env.BUILD_DATE }}'" \
-o ${{ env.RELEASE_DIR }}/${{ env.PROJECT_NAME }}_${{ env.BUILD_DATE }}_windows_arm64.exe
- name: Build macOS AMD64
run: |
CGO_ENABLED=1 GOOS=darwin GOARCH=amd64 go build \
-ldflags="-X 'main.Version=${{ env.VERSION }}' -X 'main.BuildDate=${{ env.BUILD_DATE }}'" \
-o ${{ env.RELEASE_DIR }}/${{ env.PROJECT_NAME }}_${{ env.BUILD_DATE }}_darwin_amd64
- name: Build macOS ARM64
run: |
CGO_ENABLED=1 GOOS=darwin GOARCH=arm64 go build \
-ldflags="-X 'main.Version=${{ env.VERSION }}' -X 'main.BuildDate=${{ env.BUILD_DATE }}'" \
-o ${{ env.RELEASE_DIR }}/${{ env.PROJECT_NAME }}_${{ env.BUILD_DATE }}_darwin_arm64
# Create Source Code Tarball
- name: Create Source Code Tarball
run: |
tar -czvf ${{ env.RELEASE_DIR }}/${{ env.PROJECT_NAME }}_${{ env.BUILD_DATE }}_source.tar.gz \
--exclude=.git \
--exclude=${{ env.RELEASE_DIR }} \
.
# Create Source Code Zip
- name: Create Source Code Zip
run: |
zip -r ${{ env.RELEASE_DIR }}/${{ env.PROJECT_NAME }}_${{ env.BUILD_DATE }}_source.zip \
. \
-x .git\* \
-x release\*
# Generate SHA256 Checksums
- name: Generate SHA256 Checksums
run: |
cd ${{ env.RELEASE_DIR }}
sha256sum * > ${{ env.PROJECT_NAME }}_${{ env.BUILD_DATE }}_checksums.txt
# Create GitHub Release
- name: Create Release
uses: softprops/action-gh-release@v2
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
files: ${{ env.RELEASE_DIR }}/*
generate_release_notes: true # 自动生成发行说明
draft: false # 不创建草稿
prerelease: false # 不标记为预发布
tag_name: ${{ env.VERSION }} # 使用固定版本号作为发布标签