GitHub Octodex (octocat) from https://octodex.github.com/

GitHub Action fails with “The GITHUB_TOKEN environment variable was not set”

This post was most recently updated on August 31st, 2022.

3 min read.

Another day, another thing that I am trying to do on GitHub (and partially failing), and another blog post! This time I was configuring a build and ran into trouble. This article describes one possible fix to an issue where an error is thrown along the lines of “The GITHUB_TOKEN environment variable was not set”.

As a backstory: I have a long history with Azure DevOps, and have maintained a kind of a love-hate -relationship (that maybe gravitates slightly more on the rosy side of things) for at least since it was called VSO (Visual Studio Online) or VSTS (Visual Studio Team Services). But now I’ve found myself moving my personal projects to GitHub more and more.

"All checks have failed" - pretty disheartening.
“All checks have failed” – pretty disheartening. Not what you want to see!
(I mean, it’s just one check but still!)

And that’s when I run into issues – obviously :)

Problem

Okay, so let’s dig in. What went wrong for me?

I was configuring a build for my .NET 5 application using GitHub Actions. When I added a step for publishing a release, I was using the gh-action-auto-release by CupOfTea696 – and ran into issues. The build started failing.

Release -step of my GitHub Action failing for my .NET 5 build.
Release -step of my GitHub Action failing for my .NET 5 build.

The log for this step – Release – contains something like the below (this sample is obviously for this particular step, and yours will vary):

D:\a_actions\CupOfTea696\gh-action-auto-release\v1.0.2\dist\main.js:2
(()=>{var e={5265:function(e,t,r){"use strict";var n=this&&this.__importStar||function(e){if(e&&e.__esModule)return e;var t={};if(null!=e)for(var r in e)Object.hasOwnProperty.call(e,r)&&(t[r]=e[r]);return t.default=e,t};Object.defineProperty(t,"__esModule",{value:!0});const o=n(r(2087)),s=r(4570);function i(e,t,r){const n=new a(e,t,r);process.stdout.write(n.toString()+o.EOL)}t.issueCommand=i,t.issue=function(e,t=""){i(e,{},t)};class a{constructor(e,t,r){e||(e="missing.command"),this.command=e,this.properties=t,this.message=r}toString(){let e="::"+this.command;if(this.properties&&Object.keys(this.properties).length>0){e+=" ";let r=!0;for(const n in this.properties)if(this.properties.hasOwnProperty(n)){const o=this.properties[n];o&&(r?r=!1:e+=",",e+=${n}=${t=o,s.toCommandValue(t).replace(/%/g,"%25").replace(/\r/g,"%0D").replace(/\n/g,"%0A").replace(/:/g,"%3A").replace(/,/g,"%2C")})}}var t;return e+=`::${function(e){return s.toCommandValu
The GITHUB_TOKEN environment variable was not set

So “The GITHUB_TOKEN environment variable was not set” – what’s that?

Reason

Obviously, there could be other reasons. I suppose. But here’s what I figured: The step you’re trying to perform is probably built by someone else, and that someone else (for whatever reason) decided to depend on the environment variable GITHUB_TOKEN instead of the built-in secrets.GITHUB_TOKEN (which is populated for GitHub Actions automatically).

This is probably implemented this way to avoid confusion and issues when having to use tokens with multiple scopes and permissions – and perhaps for backward compatibility or some conflict avoidance reasons.

Don’t get me wrong: I’m not trying to criticize the developers. I don’t know GitHub Actions development well enough to know whether there are a gazillion reasons to do this or not :)

And hey – fixing something like this is easy, right?

Solution

Okay, it so turns out it actually IS straightforward. Follow the steps below:

Time needed: 5 minutes

How to fix the “GITHUB_TOKEN environment variable” issue in GitHub Actions?

  1. Open your workflow definition file (.yml)

    So first you need to open up your workflow definition file, which has a “.yml” extension. It’s usually located in a folder called .github/workflows under your repository.

  2. Add your GITHUB_TOKEN as an environment variable

    You can usually do this either for the step or for your whole workflow. An example of the former might look something like this:
    steps:
    - name: mystep
    uses: ....
    env:
    GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}


    This – essentially – exposes the global, shared, built-in variable secrets.GITHUB_TOKEN for the step in your build as an environment token called GITHUB_TOKEN. Below is shown in a definition file:

    secrets.GITHUB_TOKEN mapped to an environment variable called - yes, you guessed it - GITHUB_TOKEN.

  3. (OPTIONAL) If any of your steps need the same variable, make it shared!

    This might look something like the one shown below:
    name: .NET 5 CI
    on: [push]
    env:
    GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}
    jobs:
    build:
    runs-on: windows-latest
    name: .NET 5 Application
    steps:
    - name: The step that relies on the token
    ...

And yeah, it was really that easy, at least for me.

Full sample

This is a full sample showing how to share the token:

name: .NET 5 CI

on: [push]

env:
  GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}

jobs:
  build:
    runs-on: windows-latest
    name: .NET 5 Application
    steps:
      - uses: actions/checkout@v2
      - name: Setup dotnet
        uses: actions/setup-dotnet@v1
        with:
          dotnet-version: '5.0.101' # Check for latest at link at .NET 5 download page
      - run: dotnet build
      - run: dotnet test
      - name: Publish
        run: dotnet publish
      - name: Upload WebApp1 Build Artifact
        uses: actions/upload-artifact@v2
        with:
         name: desktop
         path: <omitted>
      - name: Release
        uses: CupOfTea696/[email protected] # This step requires GITHUB_TOKEN

References

mm
0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments