【问题标题】:Conditional jobs and steps based on updated paths基于更新路径的条件作业和步骤
【发布时间】:2020-03-07 13:45:27
【问题描述】:

我们将 GitHub Actions 用于我们项目的多模块 Maven CI 构建,地址为 https://github.com/ibm/fhir

我们有:

  • 我们希望在每个拉取请求上运行的最小测试集;和
  • 我们希望仅在给定模块(或其依赖项)更新后才运行的一组综合测试

我发现我可以通过定义多个工作流并使用内置的on.pull_request.paths 属性来完成类似的事情,如记录在:https://help.github.com/en/actions/automating-your-workflow-with-github-actions/workflow-syntax-for-github-actions#onpushpull_requestpaths

我想知道是否/如何在jobstep 级别完成类似的事情。我发现作业支持基于https://help.github.com/en/actions/automating-your-workflow-with-github-actions/workflow-syntax-for-github-actions#jobsjob_idif 的条件执行,但我不知道是否有任何东西可以帮助获得类似于on.pull_request.paths 功能的行为。

假设没有,有没有人找到帮助解决这个问题的措施?或者也许有人可以指出on.pull_request.paths 功能的实现?

【问题讨论】:

    标签: maven github-actions


    【解决方案1】:

    我设法创建了一个步骤,通过使用git diff 来区分github.base_ref 分支与最新的 PR 提交 (${GITHUB_SHA}) 并过滤输出以选择路径。问题是:

    • 我需要使用github['base_ref'] 语法(大概是因为下划线?)
    • 克隆的 repo 中不存在 base_ref 分支,所以我需要在其前面加上 origin/
        - name: Conditional test
          env:
            BASE: origin/${{ github['base_ref'] }}
          run: |
            paths=(
              path1
              path2/src
              path3/src/main
            )
            if [ -n "$(git --no-pager diff --name-only ${BASE} ${GITHUB_SHA} -- ${paths[@]})" ]; then
              echo "Changes affect module, running ALL"
              mvn -B test -P all-tests --no-transfer-progress
            else
              echo "No changes affect module, running MINIMAL"
              mvn -B test -P minimal-tests --no-transfer-progress
            fi
    

    我没有足够的 GitHub 操作经验,无法知道我们是否最好将这种逻辑封装在自定义操作中,但现在我将继续这样做以避免额外的外部依赖.

    【讨论】:

      【解决方案2】:

      社区论坛上的这篇文章可能会有所帮助。 https://github.community/t5/GitHub-Actions/What-happened-to-github-event-head-commit-modified/m-p/37736#M3066

      那里的原始发布者也有类似的问题,他们创建了以下操作来确定是否对路径列表进行了修改。我不确定这是否同样适用于 on: pull_request,但你可以试试看。

      https://github.com/marketplace/actions/path-watcher-action

      来自path-watcher-action 存储库的示例:

      on: [push]
      
      jobs:
        job:
          runs-on: ubuntu-latest
          steps:
            - id: modified
              uses: pheel/path-watcher-action@v1
              with:
                github_token: ${{ secrets.GITHUB_TOKEN }}
                paths: 'dir1/**/*,dir2/**/*'
            - if: steps.modified.outputs.modified
              run: echo "Hey some change happened in one of your watched paths!"
      

      【讨论】:

      • 谢谢!我还设法在不引入自定义操作的情况下取得进展,并将为此编写一个单独的答案。最后,我在github.com/netlify/actions/tree/master/diff-includes 找到了netlify/actions/diff-includes 操作,但没有尝试,因为我已经找到了适合我的东西。
      猜你喜欢
      • 2020-03-06
      • 2015-05-29
      • 2018-05-22
      • 1970-01-01
      • 1970-01-01
      • 2015-10-18
      • 1970-01-01
      • 1970-01-01
      • 2011-05-29
      相关资源
      最近更新 更多