Reputation: 5734
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]
Upvotes: 1
Views: 2325
Reputation: 8423
Does it have to be bash? You could use jq
, but this might become a bit ugly if it gets more complex:
- run: echo '${{ toJSON(github.event.commits) }}' | jq ".[0].author"
Otherwise I can recommend the github-script action which should make your code much nicer:
- uses: actions/github-script@v6
with:
script: |
const commits = ${{ toJSON(github.event.commits) }}
for (const commit of commits) {
console.log(commit);
}
Upvotes: 1