从git中提取分支名称的Node.js包ref.



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

GitHub设置GITHUB_REF为:

refs/heads/staging

我从git了解到:"branchname"one_answers"refs/头/branchname"需要refs/heads/前缀,以便git具有"完整路径";到分支。

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

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

const { execSync } = require('child_process');
function executeGitCommand(command) {
return execSync(command)
.toString('utf8')
.replace(/[nrs]+$/, '');
}
const BRANCH = executeGitCommand('git rev-parse --abbrev-ref HEAD');
const COMMIT_SHA = executeGitCommand('git rev-parse HEAD');

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

最新更新