基于更新路径的条件作业和步骤

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

我们有:

  • 我们希望对每个请求请求进行的最少测试;和
  • 一组仅在给定模块(或其依赖项)已更新时才要运行的综合测试

我发现我可以通过定义多个工作流程并使用https://help.github.com/en/actions/automating-your-workflow-with-github-actions/workflow-syntax-for-github-actions#onpushpull_requestpaths

中记录的内置on.pull_request.paths属性来完成类似的工作

我想知道的是,我是否/如何能够在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功能的实现?

jiemy666 回答:基于更新路径的条件作业和步骤

此社区论坛上的帖子可能会有所帮助。 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!"
,

我设法通过使用git diff用最新的PR提交(github.base_ref来区分${GITHUB_SHA}分支并过滤输出以选择路径来完成需要执行的步骤。陷阱在于:

  • 我需要使用github['base_ref']语法(大概是由于下划线?)
  • 克隆的存储库中不存在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动作方面没有足够的经验,无法知道我们是否最好将这种逻辑封装在自定义动作中,但是现在我将继续使用它,以避免额外的外部依赖性

本文链接:https://www.f2er.com/3121315.html

大家都在问