是否有一种方法可以触发每个目录更新的GH动作?



我试图创建一个GH动作工作流,压缩更新的目录,然后将每个Zip文件推送到源,具有不同的配置标志(例如name标志将不同于每个Zip文件)。

。假设我有4个目录。dir1/ dir2/ dir3/ dir4/如果我更新dir2和dir4中的代码,那么我希望我的GH动作压缩这两个,并执行一个动作,我将两个目录推到一个源,以及为两者配置不同的设置。

我对此的一个想法是触发GH动作的2次运行,1次运行dir2,它被压缩,并推动。并触发dir4的第二次运行,以压缩和推动。
这将使用[working-directory](https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#defaultsrun)属性来捕获需要压缩的目录。

我不确定如何触发GitHub Action每个目录更新的运行。任何帮助,这将是非常感激!

对于如何实现我正在寻找的其他解决方案也开放,在那里我可以压缩并执行其余的工作流程,对于每个已更新的目录。

@aknosis的答案是坚如磐石的基础。然而,它不能按原样工作(jobs部分)。基于这个文件,我提供了一个可行的解决方案:

jobs:
find-out-changes:
runs-on: ubuntu-latest
outputs:
changed_directories: ${{ steps.set-output.outputs.changed_directories }}  # The `dirs` doesn't exist in the outputs of changed-files@v35 action.
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0
- name: Get changed files
id: changed-files
uses: tj-actions/changed-files@v35
with:
dir_names: true
dir_names_max_depth: 2  # This is optional. If not provided, full subdirectories' paths will be provided. Use it if you need to trim the output. See docs for details: https://github.com/tj-actions/changed-files/tree/main#inputs.
json: true
quotepath: false
- name: 'Set output in the matrix format'
id: set-output
run: echo "changed_directories={"dir":${{ steps.changed-files.outputs.all_changed_files }}}" >> "$GITHUB_OUTPUT"
do-stuff:
runs-on: ubuntu-latest
if: ${{ needs.find-out-changes.outputs.changed_directories != '' }}  # Without it, the strategy parser will fail if the changed_directories is empty.
strategy:
matrix: ${{fromJson(needs.find-out-changes.outputs.changed_directories)}}
needs:
- find-out-changes
steps:
- uses: actions/checkout@v3
- run: zip ${{ matrix.dir }} etc.

您可以在单个工作流中实现这一点。

  1. 使用paths键根据目录更改运行作业
  2. 将更改后的目录信息转换为JSON格式,以便可以对
  3. 进行操作
  4. 执行相同的工作,通过矩阵做你的zip和push魔术

注意:这都是伪代码,它给你一个想法,但它可能不是语法正确的

  1. 这将在其中一个目录更新时运行此GHA工作流。参见模式匹配小抄
on:
push:
branches:
- 'main'
paths:
- 'dir1/**'
- 'dir2/**'

模式匹配小抄

这将在这些目录中的一个被更新时运行这个GHA工作流。

  1. 这需要两个作业,一个是提取数据以了解哪些目录发生了更改。然后,另一项工作是你想要完成的真正的肉和土豆。

第一个作业find-out-changes使用一个将输出相关数据的现有操作。第二个作业do-stuff将为find-out-changes输出的每个目录运行一次。

jobs:
find-out-changes:
runs-on: ubuntu-latest
outputs:
dirs: ${{ steps.changed-files.dirs }}
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0
- name: Get changed files
id: changed-files
uses: tj-actions/changed-files@v35
with:
dir_names: true
do-stuff:
runs-on: ubuntu-latest
strategy:
matrix: ${{fromJson(needs.find-out-changes.outputs.dirs)}}
needs:
- find-out-changes
steps:
- uses: actions/checkout@v3
- run: zip ${{ matrix.dir }} etc.

最新更新