a8ff1cabad
Update build-and-deploy.yml workflow to: 1. Run single job 'build-and-release' to bypass artifact transfers. 2. Build JAR with -PartefactVersion parameter. 3. Upload the compiled JAR asset directly into GitHub Releases. 4. Build and push the Docker image directly to GitHub Container Registry (ghcr.io).
86 lines
2.7 KiB
YAML
86 lines
2.7 KiB
YAML
name: Build and Deploy
|
|
|
|
on:
|
|
release:
|
|
types: [published]
|
|
|
|
env:
|
|
APP_NAME: delta-force-guide-server
|
|
|
|
jobs:
|
|
# ================================================================
|
|
# Single Job: Build, Upload JAR to Release, and Push to GHCR
|
|
# ================================================================
|
|
build-and-release:
|
|
runs-on: ubuntu-latest
|
|
permissions:
|
|
contents: write
|
|
packages: write
|
|
steps:
|
|
- uses: actions/checkout@v4
|
|
|
|
- name: Set up JDK 21 (Corretto)
|
|
uses: actions/setup-java@v4
|
|
with:
|
|
java-version: 21
|
|
distribution: corretto
|
|
cache: gradle
|
|
|
|
- name: Set up Gradle
|
|
uses: gradle/actions/setup-gradle@v4
|
|
|
|
# 使用 Release Tag 做为 Gradle 属性传入
|
|
- name: Build with Gradle
|
|
run: ./gradlew bootJar -x test -PartefactVersion="${{ github.event.release.tag_name }}"
|
|
|
|
- name: Resolve JAR file path
|
|
id: jar
|
|
run: |
|
|
JAR_PATH=$(find build/libs -name '*.jar' | head -1)
|
|
echo "file=$JAR_PATH" >> "$GITHUB_OUTPUT"
|
|
|
|
# 上传 JAR 包到 GitHub Release 中
|
|
- name: Upload JAR to GitHub Release
|
|
uses: svenstaro/upload-release-action@v2
|
|
with:
|
|
repo_token: ${{ secrets.GITHUB_TOKEN }}
|
|
file: ${{ steps.jar.outputs.file }}
|
|
asset_name: ${{ github.event.repository.name }}-${{ github.event.release.tag_name }}.jar
|
|
tag: ${{ github.event.release.tag_name }}
|
|
overwrite: true
|
|
|
|
- name: Set up Docker Buildx
|
|
uses: docker/setup-buildx-action@v3
|
|
|
|
# 登录到 GitHub Container Registry (GHCR)
|
|
- name: Log in to GHCR
|
|
uses: docker/login-action@v3
|
|
with:
|
|
registry: ghcr.io
|
|
username: ${{ github.actor }}
|
|
password: ${{ secrets.GITHUB_TOKEN }}
|
|
|
|
# 镜像打标签准备
|
|
- name: Generate image tags
|
|
id: meta
|
|
run: |
|
|
OWNER_LC=$(echo "${{ github.repository_owner }}" | tr '[:upper:]' '[:lower:]')
|
|
REPO_LC=$(echo "${{ github.event.repository.name }}" | tr '[:upper:]' '[:lower:]')
|
|
echo "tag_version=ghcr.io/$OWNER_LC/$REPO_LC:${{ github.event.release.tag_name }}" >> "$GITHUB_OUTPUT"
|
|
echo "tag_latest=ghcr.io/$OWNER_LC/$REPO_LC:latest" >> "$GITHUB_OUTPUT"
|
|
|
|
# 构建并上传镜像到 GHCR
|
|
- name: Build and push Docker image
|
|
uses: docker/build-push-action@v6
|
|
with:
|
|
context: .
|
|
file: Dockerfile.ci
|
|
build-args: JAR_FILE=${{ steps.jar.outputs.file }}
|
|
push: true
|
|
tags: |
|
|
${{ steps.meta.outputs.tag_version }}
|
|
${{ steps.meta.outputs.tag_latest }}
|
|
cache-from: type=gha
|
|
cache-to: type=gha,mode=max
|
|
|