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

发布于 2025-01-16 01:04:57 字数 1168 浏览 2 评论 0原文

我已经在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

盗梦空间 2025-01-23 01:04:57

一定要bash吗?您可以使用 jq,但是如果它变得更复杂,这可能会变得有点难看:

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

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

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

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);
      }

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文