如何确保GitHub Actions中deploy-update在PR更新且build成功时运行?
解决方案
问题出在build作业的依赖逻辑上:默认情况下,needs: pre-build要求依赖作业必须成功,但PR更新时pre-build被跳过,虽然你给build加了always(),但依赖链的状态判断还是会影响后续的deploy-update作业。按下面的配置修改就能解决:
修改后的完整配置
jobs: pre-build: if: github.event.action == 'opened' || github.event.action == 'reopened' runs-on: ubuntu-latest name: Pre-Build Checks steps: # Azure resource creation steps here build: if: github.event.action != 'closed' && (needs.pre-build.result == 'success' || needs.pre-build.result == 'skipped') runs-on: ubuntu-latest needs: pre-build name: Build Job steps: # Build steps here deploy: if: (github.event.action == 'opened' || github.event.action == 'reopened') && needs.build.result == 'success' runs-on: ubuntu-latest needs: build name: Deploy Job steps: # Deploy steps here deploy-update: if: github.event.action == 'synchronize' && needs.build.result == 'success' runs-on: ubuntu-latest needs: build name: Deploy Update Job steps: # Deploy-update steps here
关键修改点说明
- pre-build:简化条件,只在PR首次打开或重新打开时运行,不用额外判断
!= 'closed',因为这两个动作本身就不是关闭状态。 - build:调整运行条件,PR未关闭时,只要
pre-build成功或者被跳过,就执行构建。这样首次PR时会等资源创建成功再构建,PR更新时直接跳过资源创建步骤去构建。 - deploy:加了
needs.build.result == 'success',确保只有构建成功才会执行首次部署。 - deploy-update:明确要求PR是更新状态(
synchronize动作)且构建成功才运行,彻底避免依赖链导致的跳过问题,同时保证更新部署只在构建成功后执行。
内容的提问来源于stack exchange,提问作者twister_void
相关产品推荐
相关产品推荐

