从bash脚本获取github拉取请求中所有新创建/编辑的文件的列表



我有一个github动作设置在拉请求的创建/同步上运行。我希望能够获得所有JSON文件路径的列表,无论是创建,或在拉请求中编辑,并做一些与这个文件路径列表。

Github工作流
name: PR
on:
pull_request
jobs:
run-script:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: run the script
run:./.github/scripts/script.sh
shell: bash

script.sh

for filepath in $(...); do # get the list of filepaths that were created or edited in the PR
# do something with the filepaths
done

我的尝试
for file in $(git diff-tree --no-commit-id --name-only -r HEAD~1 HEAD | grep '.json$'); do
done

但是这给了我一个错误

fatal: ambiguous argument 'HEAD~1': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'

对于脚本,您可以获得json文件的diff,如下所示:

mergeBranch='main' # Change if PR merges into a different branch
oldCommit=$(git log $mergeBranch | head -n 1 | awk -F ' ' '{print $2}')
latestCommit=$(git log | head -n 1 | awk -F ' ' '{print $2}')
git diff --merge-base $oldCommit $latestCommit -- '*.json'

同样,这不是要求,而是建议编辑更少的文件。你可以把脚本的内容放在GitHub Action步骤中,如下所示:

- name: Check Diff
run: |
# Script Contents Below

相关内容

  • 没有找到相关文章

最新更新