GitHub Action中Pull Request事件Terraform Plan不执行问题排查
问题分析与解决
直接原因:事件类型不匹配
合并PR到master后,GitHub Action触发的是push事件(github.event_name == 'push'),但你在Terraform Plan步骤里设置的条件是if: github.event_name == 'pull_request',条件不满足,所以该步骤被跳过。
同时,当前Workflow的触发规则里没有监听pull_request事件:
on: push: branches: [ "master" ] workflow_dispatch:
所以创建feature到master的PR时,这个Workflow根本不会自动运行,你预期的PR时执行Plan的逻辑也无法生效。
修复方案
1. 让PR触发时执行Terraform Plan
在on字段中添加pull_request触发规则,指定监听针对master分支的PR:
on: push: branches: [ "master" ] pull_request: branches: [ "master" ] # 监听指向master的PR workflow_dispatch:
2. 调整Terraform步骤的条件逻辑
如果希望PR时仅执行Plan,合并到master后执行Apply(常规部署流程):
保留Terraform Plan的if条件,新增Terraform Apply步骤并设置条件为push事件:- name: Terraform Plan id: plan if: github.event_name == 'pull_request' run: | cd terraform_with_ALB terraform plan -no-color -input=false continue-on-error: true - name: Terraform Apply if: github.event_name == 'push' run: | cd terraform_with_ALB terraform apply -auto-approve -input=false如果希望合并到master时也执行Plan(不推荐,通常合并后直接部署),可以修改
Terraform Plan的条件:- name: Terraform Plan id: plan if: github.event_name == 'pull_request' || github.event_name == 'push' run: | cd terraform_with_ALB terraform plan -no-color -input=false continue-on-error: true
额外优化点
你的IMAGE_TAG使用github.event.head_commit.message,可能包含特殊字符导致镜像标签无效,建议改用更稳定的标识(比如提交短哈希值):
IMAGE_TAG: ${{ github.sha }}
内容的提问来源于stack exchange,提问作者surbhi singhal
相关产品推荐
相关产品推荐

