在 github 操作脚本中迭代 github.event.commits
我已经在 Github actions 中编写了action。在推送事件中,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]
I've written action in Github actions. In push events, a variable is provided by GitHub that starts with github
.
Json preview of the variable is:
{
"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/"
}
}
I want to write a script that iterates on github.event.commits
and creates something like this:
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
But the id
doesn't work. I got this error:
Unrecognized named-value: 'i'. Located at position 22 within expression: github.event.commits[i]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一定要bash吗?您可以使用
jq
,但是如果它变得更复杂,这可能会变得有点难看:否则我可以推荐 github-script 操作 这应该会让你的代码变得更好:
Does it have to be bash? You could use
jq
, but this might become a bit ugly if it gets more complex:Otherwise I can recommend the github-script action which should make your code much nicer: