From a8ff1cabad44e23055ad9581b7df98cabea8cce3 Mon Sep 17 00:00:00 2001 From: siujamo Date: Mon, 25 May 2026 15:52:54 +0800 Subject: [PATCH] chore: rewrite GitHub Actions to build, publish release JAR, and push image to GHCR 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). --- .github/workflows/build-and-deploy.yml | 108 ++++++++----------------- 1 file changed, 33 insertions(+), 75 deletions(-) diff --git a/.github/workflows/build-and-deploy.yml b/.github/workflows/build-and-deploy.yml index 5e09721..e275d22 100644 --- a/.github/workflows/build-and-deploy.yml +++ b/.github/workflows/build-and-deploy.yml @@ -6,15 +6,16 @@ on: env: APP_NAME: delta-force-guide-server - IMAGE_REGISTRY: ${{ vars.GITLAB_REGISTRY }} - IMAGE_NAME: ${{ vars.GITLAB_IMAGE_NAME }} jobs: # ================================================================ - # Job 1 — Package: build the JAR with Gradle + # Single Job: Build, Upload JAR to Release, and Push to GHCR # ================================================================ - package: + build-and-release: runs-on: ubuntu-latest + permissions: + contents: write + packages: write steps: - uses: actions/checkout@v4 @@ -28,54 +29,47 @@ jobs: - name: Set up Gradle uses: gradle/actions/setup-gradle@v4 + # 使用 Release Tag 做为 Gradle 属性传入 - name: Build with Gradle - run: ./gradlew build - - - name: Upload JAR artifact - uses: actions/upload-artifact@v4 - with: - name: app-jar - path: build/libs/delta-force-guide-server-*.jar - retention-days: 1 - - # ================================================================ - # Job 2 — Build & push Docker image to GitHub Container Registry - # ================================================================ - build-and-push: - needs: package - runs-on: ubuntu-latest - permissions: - contents: read - packages: write - steps: - - uses: actions/checkout@v4 - - - name: Download JAR artifact - uses: actions/download-artifact@v4 - with: - name: app-jar - path: build/libs + run: ./gradlew bootJar -x test -PartefactVersion="${{ github.event.release.tag_name }}" - name: Resolve JAR file path id: jar - run: echo "file=$(ls build/libs/delta-force-guide-server-*.jar | head -1)" >> "$GITHUB_OUTPUT" + 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 - - name: Log in to GitLab Container Registry + # 登录到 GitHub Container Registry (GHCR) + - name: Log in to GHCR uses: docker/login-action@v3 with: - registry: ${{ env.IMAGE_REGISTRY }} - username: ${{ vars.GITLAB_REGISTRY_USER }} - password: ${{ secrets.GITLAB_REGISTRY_PASSWORD }} + registry: ghcr.io + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + # 镜像打标签准备 - name: Generate image tags id: meta run: | - echo "version=${{ env.IMAGE_NAME }}:${{ github.event.release.tag_name }}" >> "$GITHUB_OUTPUT" - echo "latest=${{ env.IMAGE_NAME }}:latest" >> "$GITHUB_OUTPUT" + 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: @@ -84,44 +78,8 @@ jobs: build-args: JAR_FILE=${{ steps.jar.outputs.file }} push: true tags: | - ${{ steps.meta.outputs.version }} - ${{ steps.meta.outputs.latest }} + ${{ steps.meta.outputs.tag_version }} + ${{ steps.meta.outputs.tag_latest }} cache-from: type=gha cache-to: type=gha,mode=max - # ================================================================ - # Job 3 — Deploy on the target server via SSH - # ================================================================ - deploy: - needs: build-and-push - runs-on: ubuntu-latest - steps: - - name: Deploy via SSH - uses: appleboy/ssh-action@v1.2.0 - with: - host: ${{ secrets.DEPLOY_HOST }} - username: ${{ secrets.DEPLOY_USER }} - key: ${{ secrets.DEPLOY_SSH_KEY }} - script: | - set -e - - echo '=== Pulling image ===' - echo '${{ secrets.GITLAB_REGISTRY_PASSWORD }}' | docker login ${{ env.IMAGE_REGISTRY }} \ - -u ${{ vars.GITLAB_REGISTRY_USER }} --password-stdin - docker pull ${{ env.IMAGE_NAME }}:${{ github.event.release.tag_name }} - - echo '=== Stopping old container ===' - docker stop ${{ env.APP_NAME }} || true - docker rm ${{ env.APP_NAME }} || true - - echo '=== Starting new container ===' - docker run -d \ - --name ${{ env.APP_NAME }} \ - --restart unless-stopped \ - -p ${DEPLOY_PORT:-8080}:8080 \ - ${{ env.IMAGE_NAME }}:${{ github.event.release.tag_name }} - - echo '=== Cleaning up old images ===' - docker image prune -f - - echo '=== Deployment complete ==='