gh actions build

This commit is contained in:
DullJZ
2025-10-02 22:05:38 +08:00
parent f33f93e74d
commit 9cb5e2cf6e
3 changed files with 108 additions and 3 deletions

99
.github/workflows/build.yml vendored Normal file
View File

@@ -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

1
VERSION Normal file
View File

@@ -0,0 +1 @@
v0.1.0

View File

@@ -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