From 9cb5e2cf6ee9e8010da10f84c7fbe9aac0bbb968 Mon Sep 17 00:00:00 2001 From: DullJZ <79080562+DullJZ@users.noreply.github.com> Date: Thu, 2 Oct 2025 22:05:38 +0800 Subject: [PATCH] gh actions build --- .github/workflows/build.yml | 99 +++++++++++++++++++++++++++++++++++++ VERSION | 1 + deploy/docker/Dockerfile | 11 +++-- 3 files changed, 108 insertions(+), 3 deletions(-) create mode 100644 .github/workflows/build.yml create mode 100644 VERSION diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml new file mode 100644 index 0000000..dda1097 --- /dev/null +++ b/.github/workflows/build.yml @@ -0,0 +1,99 @@ +name: Build and Publish + +on: + push: + branches: + - main + workflow_dispatch: + +jobs: + build-docker: + name: Build and Publish Docker Image + runs-on: ubuntu-latest + + steps: + - name: Check out repository + uses: actions/checkout@v2 + + - name: Set VERSION + run: | + echo "VERSION=$(cat VERSION | tr -d '\n')" >> $GITHUB_ENV + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v2 + with: + install: true + use: true + + - name: Install QEMU for multi-platform builds + uses: docker/setup-qemu-action@v2 + with: + platforms: all + + - name: Log in to Docker Hub + uses: docker/login-action@v1 + with: + username: ${{ secrets.DOCKER_USERNAME }} + password: ${{ secrets.DOCKER_PASSWORD }} + + - name: Build and push Docker image + uses: docker/build-push-action@v2 + with: + context: ./deploy/docker + platforms: linux/amd64,linux/arm64,linux/arm/v7 + push: true + tags: | + ${{ secrets.DOCKER_USERNAME }}/s3-balance:latest + ${{ secrets.DOCKER_USERNAME }}/s3-balance:${{ env.VERSION }} + + build-binary: + name: Build and Release Binary + runs-on: ubuntu-latest + + steps: + - name: Check out repository + uses: actions/checkout@v2 + + - name: Set VERSION + run: | + echo "VERSION=$(cat VERSION | tr -d '\n')" >> $GITHUB_ENV + + - name: Setup Go + uses: actions/setup-go@v2 + with: + go-version: '^1.24' + + - name: Build Binary + run: | + platforms=("linux/amd64" "linux/arm64" "darwin/amd64" "darwin/arm64" "windows/amd64" "windows/arm64") + for platform in "${platforms[@]}"; do + IFS="/" read -r GOOS GOARCH <<< "$platform" + output_name="build/S3Balance_${GOOS}_${GOARCH}" + if [ "$GOOS" == "windows" ]; then + output_name+=".exe" + fi + GOOS=$GOOS GOARCH=$GOARCH go build -o $output_name cmd/s3-balance/main.go + done + + sudo apt-get update + sudo apt-get install -y upx + upx --best --lzma build/S3Balance_* || true + + - name: Create Release + id: create_release + uses: actions/create-release@v1 + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + with: + tag_name: ${{ env.VERSION }} + release_name: Release ${{ env.VERSION }} + draft: false + prerelease: false + + - name: Upload All Release Assets + uses: actions/upload-release-asset@v1 + with: + upload_url: ${{ steps.create_release.outputs.upload_url }} + asset_path: ./build + asset_name: "" + asset_content_type: application/octet-stream diff --git a/VERSION b/VERSION new file mode 100644 index 0000000..9ff151c --- /dev/null +++ b/VERSION @@ -0,0 +1 @@ +v0.1.0 \ No newline at end of file diff --git a/deploy/docker/Dockerfile b/deploy/docker/Dockerfile index 2af75ef..244e83f 100644 --- a/deploy/docker/Dockerfile +++ b/deploy/docker/Dockerfile @@ -3,7 +3,7 @@ FROM golang:1.24.5-alpine AS builder # 安装构建依赖 # 注意:不再需要 gcc, musl-dev, sqlite-dev,因为使用 modernc.org/sqlite 纯Go驱动 -RUN apk add --no-cache git ca-certificates tzdata +RUN apk add --no-cache git ca-certificates tzdata upx # 设置工作目录 WORKDIR /build @@ -15,12 +15,17 @@ RUN go mod download && go mod tidy # 复制源码 COPY . . +# 添加构建参数以支持多架构 +ARG TARGETOS=linux +ARG TARGETARCH=amd64 + # 构建应用 # CGO_ENABLED=0: 禁用CGO,使用纯Go SQLite驱动 (modernc.org/sqlite) -RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build \ +RUN CGO_ENABLED=0 GOOS=$TARGETOS GOARCH=$TARGETARCH go build \ -ldflags="-w -s -X main.version=$(git describe --tags --always)" \ -o s3-balance \ - cmd/s3-balance/main.go + cmd/s3-balance/main.go && \ + upx --best --lzma s3-balance || true # 运行时镜像 - 使用轻量级Alpine Linux FROM alpine:latest