1

我在 GitHub Actions 上运行我的 CI,并且我使用GITHUB_REF默认环境变量作为下载 URL 的一部分。

GitHub 设置GITHUB_REF为:

refs/heads/staging

我从 git 中了解到:“branchname”和“refs/heads/branchname”之间的区别refs/heads/需要前缀,以便 git 具有分支的“完整路径”。

现在,我想知道是否有人知道部署到像npmjs这样的注册表的现成 node.js 包,我可以使用它从长引用字符串中提取分支名称?似乎可能有许多不同的前缀:refs/tags/,refs/remotes等,如果有其他可用的解决方案,我不想编写我的自定义脚本。

4

1 回答 1

1

感谢 Lawrence Cherone 的评论,我找到了这个GitHub 要点

const { execSync } = require('child_process');

function executeGitCommand(command) {
  return execSync(command)
    .toString('utf8')
    .replace(/[\n\r\s]+$/, '');
}

const BRANCH = executeGitCommand('git rev-parse --abbrev-ref HEAD');
const COMMIT_SHA = executeGitCommand('git rev-parse HEAD');

它不是一个 npm 包,但现在已经足够好了。

于 2021-09-13T18:34:19.427 回答