[GitHub Actions] Get branch sha inside action

I would like to access the sha of the git branch I am currently working with (inside job triggered by push for example).

I want to use the sha to create something like  CI_ENVIRONMENT_SLUG in GitLab CI which can be used to create URL of Pull Request preview for example.

I want to use the sha to uniquely identify a particular branch, so if somebody creates a branch with the same name (but different sha) again after the first one was deleted, the SLUG won’t collide with the old one.

I tried to access it with something like this: git rev-parse $GIT_BRANCH  where $GIT_BRANCH  was an ENV var with ${{ github.ref }} inside, but with no luck (fatal: ambiguous argument ‘<branch_name>’: unknown revision or path not in the working tree.).
Thanks in advance for your help.

1 Like

Our help documentation for the new Actions architecture lists the environment variables that are available to your Actions, including GITHUB_SHA, which sounds like is what you want.

I hope that helps!

1 Like

Thanks for your reply @lee-dohm, but unfortunately, that did not help. I need some identity of a branch inside a workflow. But not just a branch name, but ideally some unique identifier which will be different when a branch with the same name will be created in the future, but will be the same no matter if the workflow was run because of the branch was just created or a first or tenth commit was commited to that branch.

I had an idea, that the SHA of a commit to which the branch points to would be enough. Maybe there’s a better suitable approach, I don’t know. I am trying to replicate GitLab CI environments which have URL usually constructed from $CI_ENVIRONMENT_SLUG.