Files
flvx/.github/workflows/docker-build.yml
T
2025-08-17 22:08:40 +08:00

221 lines
7.1 KiB
YAML

name: Build and Push Vue or Spring Boot Images
on:
push:
branches:
- main
paths:
- 'vue-frontend/**'
- 'vite-frontend/**'
- 'springboot-backend/**'
- 'go-gost/**'
- '.github/workflows/docker-build.yml'
jobs:
build-gost:
if: contains(github.event.head_commit.message, '[skip gost]') == false
name: Build & Compress GOST Binary
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- uses: actions/checkout@v3
- name: Check for GOST changes
uses: dorny/paths-filter@v2
id: gost-changes
with:
filters: |
gost:
- 'go-gost/**'
- name: Set up Go
if: steps.gost-changes.outputs.gost == 'true'
uses: actions/setup-go@v4
with:
go-version: '1.21'
- name: Cache Go modules
if: steps.gost-changes.outputs.gost == 'true'
uses: actions/cache@v3
with:
path: |
~/.cache/go-build
~/go/pkg/mod
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-
- name: Install UPX
if: steps.gost-changes.outputs.gost == 'true'
run: |
wget https://github.com/upx/upx/releases/download/v4.2.1/upx-4.2.1-amd64_linux.tar.xz
tar -xf upx-4.2.1-amd64_linux.tar.xz
sudo mv upx-4.2.1-amd64_linux/upx /usr/local/bin/
rm -rf upx-4.2.1-amd64_linux*
- name: Build GOST binary
if: steps.gost-changes.outputs.gost == 'true'
working-directory: ./go-gost
run: |
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags="-s -w" -o gost
- name: Compress with UPX
if: steps.gost-changes.outputs.gost == 'true'
working-directory: ./go-gost
run: |
upx --best --lzma gost
- name: Get Release
if: steps.gost-changes.outputs.gost == 'true'
id: get_release
run: |
# 尝试获取现有的 release
RELEASE_ID=$(gh api repos/${{ github.repository }}/releases/tags/gost-latest --jq .id 2>/dev/null || echo "")
if [ -n "$RELEASE_ID" ]; then
echo "release_id=$RELEASE_ID" >> $GITHUB_OUTPUT
echo "existing_release=true" >> $GITHUB_OUTPUT
else
echo "existing_release=false" >> $GITHUB_OUTPUT
fi
- name: Create Release
if: steps.gost-changes.outputs.gost == 'true' && steps.get_release.outputs.existing_release == 'false'
id: create_release
uses: actions/create-release@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: gost-latest
release_name: GOST Binary Latest
body: |
GOST binary compiled from commit ${{ github.sha }}
- Platform: Linux AMD64
- Compressed with UPX
- Build date: ${{ github.event.head_commit.timestamp }}
draft: false
prerelease: false
- name: Get Release ID for existing release
if: steps.gost-changes.outputs.gost == 'true' && steps.get_release.outputs.existing_release == 'true'
id: get_release_id
run: |
RELEASE_ID=$(gh api repos/${{ github.repository }}/releases/tags/gost-latest --jq .id)
echo "release_id=$RELEASE_ID" >> $GITHUB_OUTPUT
- name: Upload GOST binary to Release
if: steps.gost-changes.outputs.gost == 'true'
run: |
# 确定 release ID
if [ "${{ steps.get_release.outputs.existing_release }}" == "true" ]; then
RELEASE_ID="${{ steps.get_release_id.outputs.release_id }}"
else
RELEASE_ID="${{ steps.create_release.outputs.id }}"
fi
# 删除已存在的 asset(如果有)
gh api repos/${{ github.repository }}/releases/$RELEASE_ID/assets --jq '.[] | select(.name=="gost") | .id' | xargs -I {} gh api repos/${{ github.repository }}/releases/assets/{} -X DELETE 2>/dev/null || echo "No existing asset to delete"
# 上传新的 asset
gh api repos/${{ github.repository }}/releases/$RELEASE_ID/assets \
-F name=gost \
-F "data=@./go-gost/gost" \
-H "Content-Type: application/octet-stream"
build-vite:
if: contains(github.event.head_commit.message, '[skip vite]') == false
name: Build & Push Vite Frontend
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Check for Vite changes
uses: dorny/paths-filter@v2
id: vite-changes
with:
filters: |
vite:
- 'vite-frontend/**'
- name: Set up Node.js
if: steps.vite-changes.outputs.vite == 'true'
uses: actions/setup-node@v4
with:
node-version: 20
- name: Set up Docker Buildx
if: steps.vite-changes.outputs.vite == 'true'
uses: docker/setup-buildx-action@v2
- name: Log in to DockerHub
if: steps.vite-changes.outputs.vite == 'true'
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKER_HUB_USERNAME }}
password: ${{ secrets.DOCKER_HUB_TOKEN }}
- name: Build and push Vite multi-arch image
if: steps.vite-changes.outputs.vite == 'true'
run: |
docker buildx build \
--platform linux/amd64 \
--push \
-t bqlpfy/vite-frontend \
./vite-frontend
build-java:
if: contains(github.event.head_commit.message, '[skip java]') == false
name: Build & Push Spring Boot Backend
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Check for Java changes
uses: dorny/paths-filter@v2
id: java-changes
with:
filters: |
java:
- 'springboot-backend/**'
- name: Set up JDK and Maven
if: steps.java-changes.outputs.java == 'true'
uses: actions/setup-java@v3
with:
java-version: 21
distribution: 'temurin'
- name: Cache Maven dependencies
if: steps.java-changes.outputs.java == 'true'
uses: actions/cache@v3
with:
path: ~/.m2
key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }}
restore-keys: ${{ runner.os }}-m2
- name: Build Java JAR
if: steps.java-changes.outputs.java == 'true'
working-directory: ./springboot-backend
run: mvn clean package -DskipTests
- name: Set up Docker Buildx
if: steps.java-changes.outputs.java == 'true'
uses: docker/setup-buildx-action@v2
- name: Log in to DockerHub
if: steps.java-changes.outputs.java == 'true'
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKER_HUB_USERNAME }}
password: ${{ secrets.DOCKER_HUB_TOKEN }}
- name: Build and push Java multi-arch image
if: steps.java-changes.outputs.java == 'true'
run: |
docker buildx build \
--platform linux/amd64 \
--push \
-t bqlpfy/springboot-backend \
./springboot-backend