1

I am running my CI on GitHub Actions, and I am using the GITHUB_REF default environment variable as a part of a download URL.

GitHub sets the GITHUB_REF to:

refs/heads/staging

I understand from git: difference between "branchname" and "refs/heads/branchname" that the refs/heads/ prefix is needed so that git has the "full path" to the branch.

Now, I am wondering if anyone knows of an off-the-shelf node.js package deployed to a registry like npmjs that I could use to extract the branch name from the long ref string? It seems like there could be many different prefixes: refs/tags/, refs/remotes, etc., and I would prefer not to write my custom script if there is another solution available.

torek
  • 448,244
  • 59
  • 642
  • 775
Paul Razvan Berg
  • 16,949
  • 9
  • 76
  • 114

1 Answers1

2

Thanks to Lawrence Cherone's comment, I found this GitHub gist:

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

It's not an npm package but it is good enough for now.

Paul Razvan Berg
  • 16,949
  • 9
  • 76
  • 114