如何从 GitHub 操作中获取 git 引用?
How to get a git ref from within GitHub Actions?
这就是我正在尝试的:
- name: Create tag
uses: actions/github-script@v3
with:
script: |
console.log(await github.git.getRef({
owner: context.repo.owner,
ref: "refs/tags/v0.0.1",
repo: context.repo.repo,
}));
但是,它产生了“未找到”错误。
如果我使用 git 检查,我可以看到标签存在:
$ git ls-remote
From git@github.com:contra/contra-deploy-tools.git
fc857fff2008eca7c4a547ad4bb35cd7ef5f3891 HEAD
fc857fff2008eca7c4a547ad4bb35cd7ef5f3891 refs/heads/main
b4e20f75a51aac030e3d8ca1360ee1ff84f10c18 refs/tags/v0.0.1
这似乎是一个权限问题。但是,我能够创建标签,并且工作流权限配置为“读写权限”。
更令人惊讶的是,我能够在工作流程中列出引用:
Run git ls-remote
From https://github.com/contra/contra-deploy-tools
f0191b469168aa48ee177d270fa2af507d3f7710 HEAD
f0191b469168aa48ee177d270fa2af507d3f7710 refs/heads/main
b4e20f75a51aac030e3d8ca1360ee1ff84f10c18 refs/tags/v0.0.1
这似乎是 API 特定问题。
我错过了什么?
API 中不需要 refs/
前缀。查看 docs,它表示:
Returns a single reference from your Git database. The :ref in the URL must be formatted as heads/ for branches and tags/ for tags.
因此,如果您将步骤更改为以下,它应该会起作用。
- name: Create tag
uses: actions/github-script@v3
with:
script: |
console.log(await github.git.getRef({
owner: context.repo.owner,
ref: "tags/v0.0.1",
repo: context.repo.repo,
}));
注意:您使用的是 github-script
版本 3,但已经有 v5。如果你想让你的操作保持最新,我之前描述了如何使用 dependabot 来完成:
这就是我正在尝试的:
- name: Create tag
uses: actions/github-script@v3
with:
script: |
console.log(await github.git.getRef({
owner: context.repo.owner,
ref: "refs/tags/v0.0.1",
repo: context.repo.repo,
}));
但是,它产生了“未找到”错误。
如果我使用 git 检查,我可以看到标签存在:
$ git ls-remote
From git@github.com:contra/contra-deploy-tools.git
fc857fff2008eca7c4a547ad4bb35cd7ef5f3891 HEAD
fc857fff2008eca7c4a547ad4bb35cd7ef5f3891 refs/heads/main
b4e20f75a51aac030e3d8ca1360ee1ff84f10c18 refs/tags/v0.0.1
这似乎是一个权限问题。但是,我能够创建标签,并且工作流权限配置为“读写权限”。
更令人惊讶的是,我能够在工作流程中列出引用:
Run git ls-remote
From https://github.com/contra/contra-deploy-tools
f0191b469168aa48ee177d270fa2af507d3f7710 HEAD
f0191b469168aa48ee177d270fa2af507d3f7710 refs/heads/main
b4e20f75a51aac030e3d8ca1360ee1ff84f10c18 refs/tags/v0.0.1
这似乎是 API 特定问题。
我错过了什么?
API 中不需要 refs/
前缀。查看 docs,它表示:
Returns a single reference from your Git database. The :ref in the URL must be formatted as heads/ for branches and tags/ for tags.
因此,如果您将步骤更改为以下,它应该会起作用。
- name: Create tag
uses: actions/github-script@v3
with:
script: |
console.log(await github.git.getRef({
owner: context.repo.owner,
ref: "tags/v0.0.1",
repo: context.repo.repo,
}));
注意:您使用的是 github-script
版本 3,但已经有 v5。如果你想让你的操作保持最新,我之前描述了如何使用 dependabot 来完成: