You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Github Actions部署时无法获取更新后的Git版本号问题求助

问题分析

你遇到的核心问题是:automatic-version-update任务更新版本并推送到分支后,build-deploy任务检出的仍是PR合并后的旧版本代码。这是因为GitHub Actions的每个Job都运行在独立的Runner环境中,checkout步骤默认会拉取触发当前工作流的原始提交(即PR合并后的版本,还未更新版本号),而非刚推送的新版本提交。

解决方案

推荐两种可靠的修复方式,按需选择:

方式一:在部署任务中拉取最新分支代码

修改build-deploy任务的代码拉取逻辑,确保获取development分支的最新代码:

build-deploy:
    needs: automatic-version-update
    name: Deployment jobs
    runs-on: ubuntu-latest
    steps:
        - name: Check Out Repository
          uses: actions/checkout@v3
          with:
              fetch-depth: 0  # 拉取完整仓库历史,避免浅克隆导致无法拉取最新代码
              ref: ${{ github.event.pull_request.base.ref }}  # 指定目标分支
        - name: Pull Latest Changes
          run: git pull origin ${{ github.event.pull_request.base.ref }}
        # 后续安装、构建、部署步骤保持不变
        - name: Install Node.js
          uses: actions/setup-node@v1
          with:
              node-version: '16.15'
        - name: Install packages
          run: yarn install
        - name: React build
          run: yarn run build:dev
        - name: Deploy to Server
          uses: easingthemes/ssh-deploy@main
          env:
              SSH_PRIVATE_KEY: ${{ secrets.SSH_KEY }}
              SOURCE: '.'
              REMOTE_HOST: ${{ secrets.SERVER_HOST }}
              REMOTE_USER: ${{ secrets.SERVER_USERNAME }}
              TARGET: ${{ secrets.SERVER_ROOT_FOLDER }}

方式二:合并版本更新与部署任务(更推荐)

将版本更新和部署流程合并到同一个Job中,所有步骤在同一个Runner环境执行,直接使用版本更新后的代码构建部署,完全避免跨Job的代码同步问题:

jobs:
    version-update-and-deploy:
        name: Version Update & Deployment
        runs-on: ubuntu-latest
        if: github.event.pull_request.merged == true
        steps:
            - name: Check Out Repository
              uses: actions/checkout@v3
              with:
                  fetch-depth: 0
            - name: Setup Node.js
              uses: actions/setup-node@v2
              with:
                  node-version: '16.15'
            - name: Update Version
              run: |
                  git config --local user.email "action@github.com"
                  git config --local user.name "GitHub Action"
                  PR_LABELS=$(jq --raw-output '.pull_request.labels[].name' $GITHUB_EVENT_PATH)

                  if [[ $PR_LABELS == *"minor version"* ]]; then
                    npm version minor -m "Updated to minor version %s"

                  elif [[ $PR_LABELS == *"major version"* ]]; then
                    npm version major -m "Updated to major version %s"

                  elif [[ $PR_LABELS == *"patch version"* ]]; then
                    npm version patch -m "Updated to patch version %s"
                  fi

            - name: Push Changes
              uses: ad-m/github-push-action@master
              with:
                  github_token: ${{ secrets.GITHUB_TOKEN }}
                  branch: ${{ github.event.pull_request.base.ref }}
            # 直接使用当前环境的更新后代码执行构建部署
            - name: Install packages
              run: yarn install
            - name: React build
              run: yarn run build:dev
            - name: Deploy to Server
              uses: easingthemes/ssh-deploy@main
              env:
                  SSH_PRIVATE_KEY: ${{ secrets.SSH_KEY }}
                  SOURCE: '.'
                  REMOTE_HOST: ${{ secrets.SERVER_HOST }}
                  REMOTE_USER: ${{ secrets.SERVER_USERNAME }}
                  TARGET: ${{ secrets.SERVER_ROOT_FOLDER }}
额外优化(可选)

如果保留原双Job结构,为避免版本更新的推送触发重复部署,可以在工作流触发条件中排除版本文件的变更:

on:
    pull_request:
        types:
            - closed
        branches:
            - development
    push:
        branches:
            - development
        paths-ignore:
            - 'package.json'
            - 'package-lock.json'

内容的提问来源于stack exchange,提问作者phoique

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.20 10:33:09