Tarun Singh
Tarun Singh

Reputation: 480

Github actions - Workflow fails to access env variable

I have the .env file saved with my firebase web API key saved in my local working directory. I use it in my project as

const firebaseConfig = {
       apiKey : process.env.REACT_APP_API_KEY,
}

Now I set up firebase hosting and use GitHub actions to automatically deploy when changes are pushed to GitHub.
However the deployed app by github gives me an error in my console saying Your API key is invalid, please check you have copied it correctly .And my app won't work as it is missing api key.
It seems like github actions is unable to access process.env.REACT_APP_API_KEY
I feel the problem is
As .env file is not pushed to github repo that's why my the build server is unable to access the command process.env.REACT_APP_API_KEY

How can I tackle this issue ?
Github workflow was automatically setup while setting up firebase hosting. Is this the error or there is something else to take care ?
Below is my firebase-hosting-merge.yml

# This file was auto-generated by the Firebase CLI
# https://github.com/firebase/firebase-tools

name: Deploy to Firebase Hosting on merge
'on':
  push:
    branches:
      - master
jobs:
  build_and_deploy:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - run: npm ci && npm run build --prod
      - uses: FirebaseExtended/action-hosting-deploy@v0
        with:
          repoToken: '${{ secrets.GITHUB_TOKEN }}'
          firebaseServiceAccount: '${{ secrets.FIREBASE_SERVICE_ACCOUNT_EVENTS_EASY }}'
          channelId: live
          projectId: myprojectname
        env:
          FIREBASE_CLI_PREVIEWS: hostingchannels

How can I make my .env file variables accessible to github build server ? Do I need to change my firebaseConfig ? Or there is any way so that I can make my .env file available to build server and later delete it once the build finishes ?

const firebaseConfig = {
     apiKey : process.env.REACT_APP_API_KEY,
}

Upvotes: 3

Views: 3601

Answers (1)

sugarcane
sugarcane

Reputation: 2730

a quick solution here could be having a step in github actions to manually create the .env file before you need it.

- name: Create env file
        run: |
          touch .env
          echo API_ENDPOINT="https://xxx.execute-api.us-west-2.amazonaws.com" >> .env
          echo API_KEY=${{ secrets.API_KEY }} >> .env
          cat .env

Upvotes: 3

Related Questions