在github操作脚本中迭代github.event.commits



我已经在Github操作中编写了操作。在推送事件中,GitHub提供了一个以github开头的变量。

Json预览的变量是:

{
"event_name": "push",
"event": {
"after": "aaaaaaaaaaaaaaaaa",
"base_ref": null,
"before": "bbbbbbbbbbbbbbbb",
"commits": [
{
"author": {
"username": "myUser"
},
"id": "bbbbbbbbbbbbbbbb",
"message": "myCommitMessage",
"url": "https://github.com/myUser/myRepo/commit/bbbbbbbbbbbbbbbb"
}
],
"compare": "https://github.com/myUser/myRepo/compare/" 
}
}

我想写一个在github.event.commits上迭代的脚本,并创建这样的东西:

commits=${{github.event.commits}}
length=${#commits[@]}
for (( i = 0; i < length; i++ )); do
commit=${{github.event.commits[i]}}
echo url=${{commit.url}}
echo sha=${{commit.id}}
echo actor=${{commit.author.username}}
echo message=${{commit.message}}
done

但是id不起作用。我得到了这个错误:

Unrecognized named-value: 'i'. Located at position 22 within expression: github.event.commits[i]

它必须是bash吗?你可以使用jq,但如果它变得更复杂,这可能会变得有点难看:

- run: echo '${{ toJSON(github.event.commits) }}' | jq ".[0].author"

否则,我可以推荐github脚本操作,这应该会让你的代码变得更好:

- uses: actions/github-script@v6
with:
script: |
const commits = ${{ toJSON(github.event.commits) }}
for (const commit of commits) {
console.log(commit);
}

最新更新