Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Github Actions - How can I make my env variable(stored in .env file) available in my workflow

I'll try to be as clear as possible. I have also asked about related issues but didn't receive a convincing response.
I'm using React and firebase for hosting.
Also, I'm storing my firebase web API key in my .env file.
I set up firebase hosting using Firebase CLI and chose to automatically deploy on merge or pull request.
After the setup finished a .github folder with .yml file was created in my working directory.

   .github
      - workflows
          -firebase-hosting-merge.yml
          -firebase-hosting-pull-request.yml

So now when I deploy my project(without pushing to GitHub) manually to firebase by running firebase deploy everything works fine and my app is up and running.
However when I make changes and push my changes to Github. Github actions are triggered and the automatic deployment to the firebase process starts. The build passes all the checks.enter image description here

However, when I visit the hosted URL there is an error I get in the console saying Your API key is invalid, please check you have copied it correctly.
I tried few workarounds like storing my firebase web API key into the Github secrets and accessing it in my .yml file.

# 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: my-project
        env:
          REACT_APP_API_KEY: ${{secrets.REACT_APP_API_KEY}}
          FIREBASE_CLI_PREVIEWS: hostingchannels

But I am still getting the error. I feel that the error is definitely due to the environment variables.
I have stored my firebase web API key in my .env.production file located in the root directory. Somehow GitHub actions are not using my environment variables defined.
Please let me know how can I manage my env variables so that it can be accessed by my workflow.

like image 918
Tarun Singh Avatar asked Feb 17 '21 17:02

Tarun Singh


People also ask

How do I pass an environment variable in GitHub Actions?

To set a custom environment variable, you must define it in the workflow file. The scope of a custom environment variable is limited to the element in which it is defined. You can define environment variables that are scoped for: The entire workflow, by using env at the top level of the workflow file.

Can I use variables in .env file?

The . env file contains the individual user environment variables that override the variables set in the /etc/environment file. You can customize your environment variables as desired by modifying your . env file.

How to add environment variables to GitHub actions workflows?

When automating processes with GitHub Actions workflow, you may come across a need to attach environment variables to your workflows. How? You first need to create and specify custom environment variables in the workflow with the env keyword. 1. Create a directory named .github/workflows where you’ll store your workflow file. 2.

How do I create an ENV file from a GitHub action?

The easiest way to do this is to create the.env file as a github secret and then create the.env file in your action. So step 1 is to create the.env files as a secret in github as a base64 encoded string: openssl base64 -A -in qa.env -out qa.txt

How to create encrypted environment variables in GitHub?

In GitHub Actions, we can create encrypted environment variables as well. We can use GitHub Secrets to store API keys and passwords kind of things. Click on the settings in the repository Click on the secrets

Is there a way to define env variables via a file?

Is there a way to define env variables via a .env file? I have been able to workaround it looping the file and setting the env vars with the set-env command: set-env sets job-wide env variables, so they will be available in the subsequent steps. NOTE: if the above one-liner grabs your attention, this post explains how key/values are extracted.


1 Answers

The answer is put custom env vars in first level before jobs:

name: Deploy to Firebase Hosting on merge
'on':
  push:
    branches:
      - master

env: # <--- here
  REACT_APP_API_KEY: ${{secrets.REACT_APP_API_KEY}} # <--- here

jobs:
  build_and_deploy:
...

And add this secrets in Github > Your project > Settings > Secrets

like image 60
Camila Eyzaguirre Villarroel Avatar answered Nov 15 '22 11:11

Camila Eyzaguirre Villarroel