${GITHUB_SHA} isn't parsed

I have a GitHub Action set up, which automatically pushes changes from a documentation to the gh-pages branch.
In that action do I use ${GITHUB_SHA:0:7} to display the first 7 characters of the commit SHA. This works for the first job, but for the second one, which sends a notification to a Discord channel using webhooks, is it not working.
The placeholder remains as is without any changes while other placeholders like ${{ github.event.compare }} gets translated properly.

Could an issue be, that ${GITHUB_SHA:0:7} is surrounded by back ticks (`)?
The code in general looks like this: [`${GITHUB_SHA:0:7}`](${{ github.event.compare }})

Or would I need to first checkout the job again in order to set the env variable first?

Hi @andre601,
Based on my test, when using back-ticks (`), ${GITHUB_SHA:0:7} could not get the expected value.


Remove back-ticks could work fine.

If remove back-ticks could not work in your scenario. Could you share your whole workflow yml here?

1 Like

It does work when removing the backticks, but my point is, that I want to have those backticks to format the sha.
Is there any alternative to display the first 7 characters of the commit sha, which would work with the backticks provided?

If not would I hope that a GitHub dev/employe could look into fixing this rather annoying issue as I see no reason why the variable shouldn’t be translated here.

The problem is that backticks are part of shell syntax, and bash will try to execute the string between the backticks and substitute the output. Hence the “1503d45: command not found” in the example with backticks posted by @yanjingzhu.

Try escaping the backticks by placing backslashes in front of them, like this:

echo "[\`${GITHUB_SHA:0:7}\`](${{ github.event.compare }})"

Thanks for the suggestion.

Unfortunately does this not work, because It causes an error:
##[error]Unexpected token ` in JSON at position 264

So escaping it won’t really work it seems.

Here is my full deploy.yml and the particular part I use to send notifications.

Well, a Bash escape sequence isn’t necessarily going to work in JSON. In JSON

\`

isn’t a valid escape sequence. :wink: You should be able to use a literal backtick, but if you really need to escape it you can use a Unicode escape: "\u0060"

However, after fixing that you’ll have another problem: The shell variable substitution ${GITHUB_SHA:0:7} is probably not going to work outside a shell context. I don’t see anything in the function documentation that would cut the SHA string the way you need it, but you could make a separate shell script that sets it as an environment variable or output.

I now tried it without any backticks and even that doesn’t seem to make it translate into the commit-hash.

Does it perhaps require a checkout first, or would the checkout from the previous job work as-well?
Also, ${GITHUB_SHA:0:7} does work since I have it used in the job before that one and there it successfully translates into the first 7 characters of the commit sha used.

I really want nothing more than using the commit sha inside a Discord webhook while having it in a inline code block, but this entire shell thing messes that up, which is nothing less than annoying.

I now moved to use ${{github.event.after}} since those kind of placeholders seem to work no matter what and while it isn’t the result I want (Only first 7 characters of a commit) is it more than good enough for it.

1 Like