3

When I do

${{ secrets.MY_SECRET }}

it returns empty string,

I am the person committing the changes and its my repository so there should be no issue regarding authorization of secrets, and also cloned it not fork,

this is how my actions job looks like

build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
        
      - name: Setup Node.js environment
        uses: actions/setup-node@v2.1.5

      - name: Download Modules
        run: npm ci
      - name: Test
        env:
          TEST_GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          TEST_SECRET: ${{ secrets.TEST_SECRET }}
        run: |
          echo ${#TEST_GITHUB_TOKEN}
          echo ${#TEST_SECRET}

      - name: React Build 
        run:  npm run build
        env:
          CI: true
          REACT_APP_FIREBASE_API_KEY: ${{ secrets.REACT_APP_FIREBASE_API_KEY }}
          REACT_APP_PIXABAY_API_KEY: ${{ secrets.REACT_APP_PIXABAY_API_KEY }}
          REACT_APP_TEST: 'TESTING'

      - name: Upload a Build Artifact
        uses: actions/upload-artifact@v2.2.3
        with:
          name: docs
          path: './build'

the TEST_GITHUB_TOKEN returns 40

and TEST_SECRET returns 0

and the REACT_APP_TEST environment variable is working as expected, it means the secrets is the thing that is not being passed

GitHub Repository

Yugank Singh
  • 401
  • 6
  • 14
  • Why are you using `{}` with your `echo` command? Did you try running only `echo "$TEST_SECRET"`? (it should print `* * *` as secrets values as encrypted and protected on your workflows. [Example in a personal repo](https://github.com/GuillaumeFalourd/poc-github-actions/blob/main/.github/workflows/2-secret-workflow.yml)) – GuiFalourd Jun 14 '21 at 14:41
  • 3
    @GuiFalourd `${#var}` prints the length of `var`, that shouldn't be the problem. – Benjamin W. Jun 14 '21 at 14:46
  • 3
    Just to be sure: you've added a `TEST_SECRET` to your repository, yes? `GITHUB_TOKEN` is special in that it's predefined. – Benjamin W. Jun 14 '21 at 14:47
  • 2
    Have you tried running with [debug logging](https://docs.github.com/en/actions/managing-workflow-runs/enabling-debug-logging) enabled? – Benjamin W. Jun 14 '21 at 14:50
  • 3
    Also, you mix up `MY_SECRET` and `TEST_SECRET` in the question, make sure you use the right one. – Benjamin W. Jun 14 '21 at 14:51
  • Didn't know _${#var} prints the length of var_, thanks Benjamin :) It might be related to the point you suggested then (secrets set with another name). – GuiFalourd Jun 14 '21 at 15:04
  • I have checked TEST_SECRET is there in my secrets, but there I can see wot environments, one is my-environment and other one is gh-pages-enviroment, does that make any difference – Yugank Singh Jun 14 '21 at 15:32
  • 2
    Have you read the following question and checked if you are using an *Environment secret* or a *Repository secret*? https://stackoverflow.com/questions/66521958/how-to-access-environment-secrets-from-a-github-workflow – riQQ Jun 14 '21 at 20:38
  • 1
    Does this answer your question? [How to access environment secrets from a Github workflow?](https://stackoverflow.com/questions/66521958/how-to-access-environment-secrets-from-a-github-workflow) – riQQ Jun 15 '21 at 06:02

1 Answers1

5

TL;DR Use Correct Environments to access secrets

basically, there are two places you can put your secrets there are environment secrets and repository secrets, the repository secrets are automatically given to the job but to access the environment you have to explicitly tell it to pass the environment like this

jobs:
  myJob:
    environment: myEnironmentName   
    runs-on: ubuntu-latest
  • you can use any OS

Go to

repo >> settings >> secrets

and check whether your secrets are stored in environment secrets or repo secrets, if they are stored in environment secrets than you have to explicitly access it like in the code above.

I really thank all the community members who commented and helped find the answer, Thanks :)

Yugank Singh
  • 401
  • 6
  • 14