Add GitHub CI and release workflows
This commit is contained in:
202
.github/workflows/build.yml
vendored
202
.github/workflows/build.yml
vendored
@@ -1,145 +1,117 @@
|
|||||||
name: Build Multi-Platform Binaries
|
name: CI Build
|
||||||
|
|
||||||
on:
|
on:
|
||||||
push:
|
push:
|
||||||
paths:
|
pull_request:
|
||||||
- '**.go'
|
|
||||||
- 'go.mod'
|
permissions:
|
||||||
- 'go.sum'
|
contents: read
|
||||||
- 'web/**'
|
|
||||||
- '.github/workflows/**'
|
concurrency:
|
||||||
|
group: ci-${{ github.workflow }}-${{ github.ref }}
|
||||||
|
cancel-in-progress: true
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
build-frontend:
|
frontend:
|
||||||
runs-on: node
|
name: Build frontend
|
||||||
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout code
|
- name: Checkout code
|
||||||
uses: actions/checkout@v4
|
uses: actions/checkout@v4
|
||||||
|
|
||||||
# 直接挂载宿主机的缓存目录
|
- name: Setup Node.js
|
||||||
- name: Build Frontend
|
uses: actions/setup-node@v4
|
||||||
run: |
|
with:
|
||||||
cd web
|
node-version: '20'
|
||||||
# 使用共享的 node_modules 缓存
|
cache: npm
|
||||||
if [ -d /data/cache/node_modules_cache ]; then
|
cache-dependency-path: web/package-lock.json
|
||||||
echo "Restoring node_modules from cache..."
|
|
||||||
cp -r /data/cache/node_modules_cache/node_modules . 2>/dev/null || true
|
- name: Install frontend dependencies
|
||||||
fi
|
working-directory: web
|
||||||
|
run: npm ci
|
||||||
npm ci --prefer-offline --no-audit
|
|
||||||
|
- name: Build frontend
|
||||||
# 保存缓存
|
working-directory: web
|
||||||
mkdir -p /data/cache/node_modules_cache
|
run: npm run build
|
||||||
cp -r node_modules /data/cache/node_modules_cache/ 2>/dev/null || true
|
|
||||||
|
- name: Upload frontend artifact
|
||||||
npm run build
|
uses: actions/upload-artifact@v4
|
||||||
|
|
||||||
- name: Upload Frontend Artifact
|
|
||||||
uses: actions/upload-artifact@v3
|
|
||||||
with:
|
with:
|
||||||
name: frontend-dist
|
name: frontend-dist
|
||||||
path: web/dist
|
path: web/dist
|
||||||
retention-days: 1
|
retention-days: 1
|
||||||
|
|
||||||
build-binaries:
|
build:
|
||||||
needs: build-frontend
|
name: Build ${{ matrix.goos }}/${{ matrix.goarch }}
|
||||||
runs-on: golang
|
needs: frontend
|
||||||
|
runs-on: ${{ matrix.runner }}
|
||||||
strategy:
|
strategy:
|
||||||
fail-fast: false
|
fail-fast: false
|
||||||
matrix:
|
matrix:
|
||||||
include:
|
include:
|
||||||
- { goos: linux, goarch: amd64, target: server, upx: true }
|
- runner: ubuntu-latest
|
||||||
- { goos: linux, goarch: amd64, target: client, upx: true }
|
goos: linux
|
||||||
- { goos: linux, goarch: arm64, target: server, upx: true }
|
goarch: amd64
|
||||||
- { goos: linux, goarch: arm64, target: client, upx: true }
|
- runner: ubuntu-latest
|
||||||
- { goos: linux, goarch: arm, goarm: 7, target: server, upx: true }
|
goos: linux
|
||||||
- { goos: linux, goarch: arm, goarm: 7, target: client, upx: true }
|
goarch: arm64
|
||||||
- { goos: windows, goarch: amd64, target: server, upx: true }
|
- runner: windows-latest
|
||||||
- { goos: windows, goarch: amd64, target: client, upx: true }
|
goos: windows
|
||||||
- { goos: windows, goarch: arm64, target: server, upx: false }
|
goarch: amd64
|
||||||
- { goos: darwin, goarch: amd64, target: server, upx: false }
|
- runner: macos-latest
|
||||||
- { goos: darwin, goarch: arm64, target: server, upx: false }
|
goos: darwin
|
||||||
|
goarch: amd64
|
||||||
|
- runner: macos-latest
|
||||||
|
goos: darwin
|
||||||
|
goarch: arm64
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: Install Dependencies
|
|
||||||
run: |
|
|
||||||
if command -v apk > /dev/null; then
|
|
||||||
apk add --no-cache nodejs upx
|
|
||||||
elif command -v apt-get > /dev/null; then
|
|
||||||
apt-get update && apt-get install -y nodejs upx-ucl
|
|
||||||
else
|
|
||||||
echo "Unsupported package manager" && exit 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
- name: Checkout code
|
- name: Checkout code
|
||||||
uses: actions/checkout@v4
|
uses: actions/checkout@v4
|
||||||
|
|
||||||
# 使用挂载卷缓存 Go 模块
|
- name: Setup Go
|
||||||
- name: Setup Go Cache
|
uses: actions/setup-go@v5
|
||||||
run: |
|
with:
|
||||||
# 创建缓存目录
|
go-version-file: go.mod
|
||||||
mkdir -p /data/cache/go-pkg-mod
|
cache: true
|
||||||
mkdir -p /data/cache/go-build-cache
|
|
||||||
mkdir -p ~/go/pkg/mod
|
- name: Download frontend artifact
|
||||||
mkdir -p ~/.cache/go-build
|
uses: actions/download-artifact@v4
|
||||||
|
|
||||||
# 恢复缓存(使用硬链接以节省空间和时间)
|
|
||||||
if [ -d /data/cache/go-pkg-mod ] && [ "$(ls -A /data/cache/go-pkg-mod)" ]; then
|
|
||||||
echo "Restoring Go pkg cache..."
|
|
||||||
cp -al /data/cache/go-pkg-mod/* ~/go/pkg/mod/ 2>/dev/null || true
|
|
||||||
fi
|
|
||||||
|
|
||||||
if [ -d /data/cache/go-build-cache ] && [ "$(ls -A /data/cache/go-build-cache)" ]; then
|
|
||||||
echo "Restoring Go build cache..."
|
|
||||||
cp -al /data/cache/go-build-cache/* ~/.cache/go-build/ 2>/dev/null || true
|
|
||||||
fi
|
|
||||||
|
|
||||||
- name: Download Go dependencies
|
|
||||||
run: go mod download
|
|
||||||
|
|
||||||
- name: Download Frontend Artifact
|
|
||||||
uses: actions/download-artifact@v3
|
|
||||||
with:
|
with:
|
||||||
name: frontend-dist
|
name: frontend-dist
|
||||||
path: internal/server/app/dist
|
path: internal/server/app/dist
|
||||||
|
|
||||||
- name: Build Binary
|
- name: Download Go modules
|
||||||
|
run: go mod download
|
||||||
|
|
||||||
|
- name: Build server and client
|
||||||
|
shell: bash
|
||||||
env:
|
env:
|
||||||
GOOS: ${{ matrix.goos }}
|
GOOS: ${{ matrix.goos }}
|
||||||
GOARCH: ${{ matrix.goarch }}
|
GOARCH: ${{ matrix.goarch }}
|
||||||
GOARM: ${{ matrix.goarm }}
|
|
||||||
CGO_ENABLED: 0
|
CGO_ENABLED: 0
|
||||||
run: |
|
run: |
|
||||||
ARM_VAL=""
|
mkdir -p build/${GOOS}_${GOARCH}
|
||||||
[ "${{ matrix.goarch }}" = "arm" ] && ARM_VAL="v${{ matrix.goarm }}"
|
|
||||||
|
|
||||||
EXT=""
|
EXT=""
|
||||||
[ "${{ matrix.goos }}" = "windows" ] && EXT=".exe"
|
if [ "$GOOS" = "windows" ]; then
|
||||||
|
EXT=".exe"
|
||||||
FILENAME="gotunnel-${{ matrix.target }}-${{ matrix.goos }}-${{ matrix.goarch }}${ARM_VAL}${EXT}"
|
fi
|
||||||
|
|
||||||
go build -trimpath -ldflags="-s -w" -o "${FILENAME}" ./cmd/${{ matrix.target }}
|
BUILD_TIME="$(date -u '+%Y-%m-%dT%H:%M:%SZ')"
|
||||||
|
GIT_COMMIT="${GITHUB_SHA::7}"
|
||||||
echo "CURRENT_FILENAME=${FILENAME}" >> $GITHUB_ENV
|
VERSION="${GITHUB_REF_NAME}"
|
||||||
|
if [ "${GITHUB_REF_TYPE}" != "tag" ]; then
|
||||||
# 保存 Go 缓存(异步,不阻塞主流程)
|
VERSION="${GITHUB_SHA::7}"
|
||||||
- name: Save Go Cache
|
fi
|
||||||
if: always()
|
|
||||||
run: |
|
LDFLAGS="-s -w -X 'main.Version=${VERSION}' -X 'main.BuildTime=${BUILD_TIME}' -X 'main.GitCommit=${GIT_COMMIT}'"
|
||||||
# 只在缓存有更新时保存(使用 rsync 可以更高效)
|
|
||||||
rsync -a --delete ~/go/pkg/mod/ /data/cache/go-pkg-mod/ 2>/dev/null || \
|
go build -trimpath -ldflags "$LDFLAGS" -o "build/${GOOS}_${GOARCH}/server${EXT}" ./cmd/server
|
||||||
cp -r ~/go/pkg/mod/* /data/cache/go-pkg-mod/ 2>/dev/null || true
|
go build -trimpath -ldflags "$LDFLAGS" -o "build/${GOOS}_${GOARCH}/client${EXT}" ./cmd/client
|
||||||
|
|
||||||
rsync -a --delete ~/.cache/go-build/ /data/cache/go-build-cache/ 2>/dev/null || \
|
- name: Upload binaries artifact
|
||||||
cp -r ~/.cache/go-build/* /data/cache/go-build-cache/ 2>/dev/null || true
|
uses: actions/upload-artifact@v4
|
||||||
|
|
||||||
- name: Run UPX Compression
|
|
||||||
if: matrix.upx == true
|
|
||||||
run: |
|
|
||||||
upx --best --lzma "${{ env.CURRENT_FILENAME }}" || echo "UPX skipped for this platform"
|
|
||||||
|
|
||||||
- name: Upload Binary
|
|
||||||
uses: actions/upload-artifact@v3
|
|
||||||
with:
|
with:
|
||||||
name: ${{ env.CURRENT_FILENAME }}
|
name: gotunnel-${{ matrix.goos }}-${{ matrix.goarch }}
|
||||||
path: ${{ env.CURRENT_FILENAME }}
|
path: build/${{ matrix.goos }}_${{ matrix.goarch }}/
|
||||||
retention-days: 7
|
retention-days: 7
|
||||||
|
|||||||
214
.github/workflows/release.yml
vendored
Normal file
214
.github/workflows/release.yml
vendored
Normal file
@@ -0,0 +1,214 @@
|
|||||||
|
name: Release
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
tags:
|
||||||
|
- 'v*'
|
||||||
|
workflow_dispatch:
|
||||||
|
inputs:
|
||||||
|
tag:
|
||||||
|
description: 'Release tag to publish, e.g. v1.2.3'
|
||||||
|
required: true
|
||||||
|
type: string
|
||||||
|
|
||||||
|
permissions:
|
||||||
|
contents: write
|
||||||
|
|
||||||
|
concurrency:
|
||||||
|
group: release-${{ github.event.inputs.tag || github.ref }}
|
||||||
|
cancel-in-progress: false
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
frontend:
|
||||||
|
name: Build frontend
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- name: Checkout code
|
||||||
|
uses: actions/checkout@v4
|
||||||
|
with:
|
||||||
|
fetch-depth: 0
|
||||||
|
|
||||||
|
- name: Setup Node.js
|
||||||
|
uses: actions/setup-node@v4
|
||||||
|
with:
|
||||||
|
node-version: '20'
|
||||||
|
cache: npm
|
||||||
|
cache-dependency-path: web/package-lock.json
|
||||||
|
|
||||||
|
- name: Install frontend dependencies
|
||||||
|
working-directory: web
|
||||||
|
run: npm ci
|
||||||
|
|
||||||
|
- name: Build frontend
|
||||||
|
working-directory: web
|
||||||
|
run: npm run build
|
||||||
|
|
||||||
|
- name: Upload frontend artifact
|
||||||
|
uses: actions/upload-artifact@v4
|
||||||
|
with:
|
||||||
|
name: frontend-dist
|
||||||
|
path: web/dist
|
||||||
|
retention-days: 1
|
||||||
|
|
||||||
|
build-assets:
|
||||||
|
name: Package ${{ matrix.component }} ${{ matrix.goos }}/${{ matrix.goarch }}
|
||||||
|
needs: frontend
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
strategy:
|
||||||
|
fail-fast: false
|
||||||
|
matrix:
|
||||||
|
include:
|
||||||
|
- component: server
|
||||||
|
goos: linux
|
||||||
|
goarch: amd64
|
||||||
|
archive_ext: tar.gz
|
||||||
|
- component: client
|
||||||
|
goos: linux
|
||||||
|
goarch: amd64
|
||||||
|
archive_ext: tar.gz
|
||||||
|
- component: server
|
||||||
|
goos: linux
|
||||||
|
goarch: arm64
|
||||||
|
archive_ext: tar.gz
|
||||||
|
- component: client
|
||||||
|
goos: linux
|
||||||
|
goarch: arm64
|
||||||
|
archive_ext: tar.gz
|
||||||
|
- component: server
|
||||||
|
goos: darwin
|
||||||
|
goarch: amd64
|
||||||
|
archive_ext: tar.gz
|
||||||
|
- component: client
|
||||||
|
goos: darwin
|
||||||
|
goarch: amd64
|
||||||
|
archive_ext: tar.gz
|
||||||
|
- component: server
|
||||||
|
goos: darwin
|
||||||
|
goarch: arm64
|
||||||
|
archive_ext: tar.gz
|
||||||
|
- component: client
|
||||||
|
goos: darwin
|
||||||
|
goarch: arm64
|
||||||
|
archive_ext: tar.gz
|
||||||
|
- component: server
|
||||||
|
goos: windows
|
||||||
|
goarch: amd64
|
||||||
|
archive_ext: zip
|
||||||
|
- component: client
|
||||||
|
goos: windows
|
||||||
|
goarch: amd64
|
||||||
|
archive_ext: zip
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- name: Checkout code
|
||||||
|
uses: actions/checkout@v4
|
||||||
|
with:
|
||||||
|
fetch-depth: 0
|
||||||
|
|
||||||
|
- name: Setup Go
|
||||||
|
uses: actions/setup-go@v5
|
||||||
|
with:
|
||||||
|
go-version-file: go.mod
|
||||||
|
cache: true
|
||||||
|
|
||||||
|
- name: Download frontend artifact
|
||||||
|
uses: actions/download-artifact@v4
|
||||||
|
with:
|
||||||
|
name: frontend-dist
|
||||||
|
path: internal/server/app/dist
|
||||||
|
|
||||||
|
- name: Download Go modules
|
||||||
|
run: go mod download
|
||||||
|
|
||||||
|
- name: Resolve release metadata
|
||||||
|
id: meta
|
||||||
|
shell: bash
|
||||||
|
run: |
|
||||||
|
if [ "${GITHUB_EVENT_NAME}" = "workflow_dispatch" ]; then
|
||||||
|
TAG="${{ github.event.inputs.tag }}"
|
||||||
|
else
|
||||||
|
TAG="${GITHUB_REF_NAME}"
|
||||||
|
fi
|
||||||
|
echo "tag=${TAG}" >> "$GITHUB_OUTPUT"
|
||||||
|
echo "commit=${GITHUB_SHA::7}" >> "$GITHUB_OUTPUT"
|
||||||
|
echo "build_time=$(date -u '+%Y-%m-%dT%H:%M:%SZ')" >> "$GITHUB_OUTPUT"
|
||||||
|
|
||||||
|
- name: Build binary
|
||||||
|
shell: bash
|
||||||
|
env:
|
||||||
|
GOOS: ${{ matrix.goos }}
|
||||||
|
GOARCH: ${{ matrix.goarch }}
|
||||||
|
CGO_ENABLED: 0
|
||||||
|
VERSION: ${{ steps.meta.outputs.tag }}
|
||||||
|
GIT_COMMIT: ${{ steps.meta.outputs.commit }}
|
||||||
|
BUILD_TIME: ${{ steps.meta.outputs.build_time }}
|
||||||
|
run: |
|
||||||
|
mkdir -p dist/package
|
||||||
|
EXT=""
|
||||||
|
if [ "$GOOS" = "windows" ]; then
|
||||||
|
EXT=".exe"
|
||||||
|
fi
|
||||||
|
|
||||||
|
OUTPUT_NAME="${{ matrix.component }}${EXT}"
|
||||||
|
LDFLAGS="-s -w -X 'main.Version=${VERSION}' -X 'main.BuildTime=${BUILD_TIME}' -X 'main.GitCommit=${GIT_COMMIT}'"
|
||||||
|
go build -trimpath -ldflags "$LDFLAGS" -o "dist/package/${OUTPUT_NAME}" ./cmd/${{ matrix.component }}
|
||||||
|
|
||||||
|
- name: Create release archive
|
||||||
|
id: package
|
||||||
|
shell: bash
|
||||||
|
run: |
|
||||||
|
TAG="${{ steps.meta.outputs.tag }}"
|
||||||
|
ARCHIVE="gotunnel-${{ matrix.component }}-${TAG}-${{ matrix.goos }}-${{ matrix.goarch }}.${{ matrix.archive_ext }}"
|
||||||
|
mkdir -p dist/out
|
||||||
|
if [ "${{ matrix.archive_ext }}" = "zip" ]; then
|
||||||
|
(cd dist/package && zip -r "../out/${ARCHIVE}" .)
|
||||||
|
else
|
||||||
|
tar -C dist/package -czf "dist/out/${ARCHIVE}" .
|
||||||
|
fi
|
||||||
|
echo "archive=dist/out/${ARCHIVE}" >> "$GITHUB_OUTPUT"
|
||||||
|
|
||||||
|
- name: Upload release asset artifact
|
||||||
|
uses: actions/upload-artifact@v4
|
||||||
|
with:
|
||||||
|
name: release-${{ matrix.component }}-${{ matrix.goos }}-${{ matrix.goarch }}
|
||||||
|
path: ${{ steps.package.outputs.archive }}
|
||||||
|
retention-days: 1
|
||||||
|
|
||||||
|
publish:
|
||||||
|
name: Publish release
|
||||||
|
needs: build-assets
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- name: Download packaged assets
|
||||||
|
uses: actions/download-artifact@v4
|
||||||
|
with:
|
||||||
|
path: release-artifacts
|
||||||
|
pattern: release-*
|
||||||
|
merge-multiple: true
|
||||||
|
|
||||||
|
- name: Resolve release metadata
|
||||||
|
id: meta
|
||||||
|
shell: bash
|
||||||
|
run: |
|
||||||
|
if [ "${GITHUB_EVENT_NAME}" = "workflow_dispatch" ]; then
|
||||||
|
TAG="${{ github.event.inputs.tag }}"
|
||||||
|
else
|
||||||
|
TAG="${GITHUB_REF_NAME}"
|
||||||
|
fi
|
||||||
|
echo "tag=${TAG}" >> "$GITHUB_OUTPUT"
|
||||||
|
|
||||||
|
- name: Generate checksums
|
||||||
|
shell: bash
|
||||||
|
run: |
|
||||||
|
cd release-artifacts
|
||||||
|
sha256sum * > SHA256SUMS.txt
|
||||||
|
|
||||||
|
- name: Create or update GitHub release
|
||||||
|
uses: softprops/action-gh-release@v2
|
||||||
|
with:
|
||||||
|
tag_name: ${{ steps.meta.outputs.tag }}
|
||||||
|
target_commitish: ${{ github.sha }}
|
||||||
|
generate_release_notes: true
|
||||||
|
fail_on_unmatched_files: true
|
||||||
|
files: |
|
||||||
|
release-artifacts/*
|
||||||
Reference in New Issue
Block a user