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

Github Actions多文件夹部署:仅变更对应目录时触发部署

解决Github Actions按文件夹变更触发对应部署的问题

你之前用if: contains(github.event.head_commit.message, './Function1')无效的原因很明确:这个条件是检查提交消息里是否包含指定字符串,而非该文件夹下的文件是否实际发生变更。要实现「文件夹变更才触发对应部署」的需求,有两种实用方案:

方案1:拆分独立Job(推荐)

把每个函数的部署拆成单独的Job,通过paths字段指定触发该Job的文件夹路径。这样只有对应文件夹发生变更时,该Job才会执行,既高效又逻辑清晰。

示例代码:

jobs:
  deploy-function1:
    runs-on: ubuntu-latest
    paths:
      - './Function1/**'
    steps:
    - name: Checkout code
      uses: actions/checkout@v3

    - name: Authenticate to Google Cloud
      uses: google-github-actions/auth@v1
      with:
        credentials_json: ${{ secrets.GCP_SA_DEPLOY_KEY }}

    - name: Set up Google Cloud SDK
      uses: google-github-actions/setup-gcloud@v1
      with:
        project_id: ${{ secrets.GCP_PROJECT_ID }}
        install_components: 'beta'

    - name: Deploy Function 1
      run: ... # 替换为你的实际部署命令

  deploy-function2:
    runs-on: ubuntu-latest
    paths:
      - './Function2/**'
    steps:
    - name: Checkout code
      uses: actions/checkout@v3

    - name: Authenticate to Google Cloud
      uses: google-github-actions/auth@v1
      with:
        credentials_json: ${{ secrets.GCP_SA_DEPLOY_KEY }}

    - name: Set up Google Cloud SDK
      uses: google-github-actions/setup-gcloud@v1
      with:
        project_id: ${{ secrets.GCP_PROJECT_ID }}
        install_components: 'beta'

    - name: Deploy Function 2
      run: ... # 替换为你的实际部署命令

# 按相同格式复制deploy-function3和deploy-function4的Job配置

方案2:在原Job的Step中添加条件判断

如果不想拆分Job,可以在每个部署Step中添加if条件,检查本次提交是否修改了对应文件夹下的文件。github.event.paths会列出所有变更的文件路径,用contains判断是否有路径包含目标文件夹即可:

示例代码:

deploy:
    runs-on: ubuntu-latest
    steps:
    - name: Checkout code
      uses: actions/checkout@v3

    - name: Authenticate to Google Cloud
      uses: google-github-actions/auth@v1
      with:
        credentials_json: ${{ secrets.GCP_SA_DEPLOY_KEY }}

    - name: Set up Google Cloud SDK
      uses: google-github-actions/setup-gcloud@v1
      with:
        project_id: ${{ secrets.GCP_PROJECT_ID }}
        install_components: 'beta'

    - name: Deploy Function 1
      if: contains(github.event.paths, './Function1/')
      run: ...

    - name: Deploy Function 2
      if: contains(github.event.paths, './Function2/')
      run: ...
          
    - name: Deploy Function 3
      if: contains(github.event.paths, './Function3/')
      run: ...

    - name: Deploy Function 4
      if: contains(github.event.paths, './Function4/')
      run: ...

补充说明

如果需要支持手动触发Workflow(workflow_dispatch事件),可以给if条件加额外判断,比如:

if: contains(github.event.paths, './Function1/') || github.event_name == 'workflow_dispatch'

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.18 23:22:03