35

I am trying to build an CI pipeline for my node.js server using github actions.

I just need to solve one issue. I need to set environment variable, so that my node.js server can access the env variable via process.env

Below is the github action workflow file.

name: Build and Deploy to GKE

on:
  pull_request:
    branches:
      - master

# Environment variables available to all jobs and steps in this workflow
env:
  ENGINE_API_KEY: ${{ secrets.ENGINE_API_KEY }}

jobs:
  setup-build-publish-deploy:
    name: Setup, Build, Publish, and Deploy
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v2

      - name: Apollo Schema Update
        env:
          ENGINE_API_KEY: ${{ secrets.ENGINE_API_KEY }}
        run: |
          sudo npm install
          sudo npm install -g apollo
          sudo npm run dev &
          sleep 3
          sudo apollo service:push --serviceURL=http://auth-cluster-ip-service --serviceName=auth --tag=master --endpoint=http://localhost:3051

I have tried declaring environment variable both workflow level and job's level, but when I console.log(process.env.ENGINE_API_KEY), it returns undefined.

I also tried ENGINE_API_KEY=$ENGINE_API_KEY npm run dev & instead of npm run dev &. This works on my macbook, but with github action, it still returns undefined.

(I did store ENGINE_API_KEY in settings -> secret. worked fine for other variables)

Sihoon Kim
  • 1,481
  • 2
  • 13
  • 32

3 Answers3

47

Create an .env file that can be read by your node server and pass in your repository secret that way. This should be done after your checkout step:

    - name: create env file
      run: |
        touch .env
        echo ENGINE_API_KEY=${{ secrets.ENGINE_API_KEY }} >> .env
kachow6
  • 1,134
  • 12
  • 15
  • Is there a reason that using the `env` directive in the workflow does not work? Is this a limitation with dotenv? – Ian H. Mar 09 '21 at 15:03
  • Dotenv is a zero-dependency module that loads environment variables from a .env file into process.env. This is the intended design. Source: https://www.npmjs.com/package/dotenv – kachow6 Mar 10 '21 at 18:55
  • I am familiar with that, but doesn't dotenv also specify that it does not override "regularly" set environment variables? Or does the `env` directive function differently than what I'm thinking? – Ian H. Mar 11 '21 at 08:26
  • 2
    The `env` directive has specific behaviour that is localized to Github Actions workflows (this is a limitation of Github Actions and not dotenv): To use the value of an environment variable in a workflow file, you should use the `env` context. If you want to use the value of an environment variable inside a runner, you can use the runner operating system's normal method for reading environment variables. https://docs.github.com/en/actions/reference/environment-variables – kachow6 Mar 11 '21 at 17:47
  • I see, I was under the impression that the `env` directive would set the variable on the running system. Thanks for clearing up! – Ian H. Mar 11 '21 at 18:31
  • ".env" file is used to provide env. variables in the development machine because many projects may depend on the same variable with different values. In production real environment variables should be used instead of ".env" file. – özüm Mar 13 '21 at 06:40
1
  - name: Create and populate .env file
    env:
      ...
      AWS_HOST: ${{ secrets.AWS_HOST }}
      DBNAME: ${{ secrets.DBNAME }}
      ...
    run: |
      touch .env
      ...
      echo aws_host="$AWS_HOST" >> .Renviron
      echo dbname="$DBNAME" >> .Renviron
      ...

      echo "cat .env"
      cat .env

      echo "ls -a ."
      ls -a .

      echo "ls -a ${{ github.workspace }}"
      ls -a ${{ github.workspace }}
    shell: bash
Anderson Laverde
  • 316
  • 1
  • 4
  • 15
0

To run a Node.js script that accesses variables from process.env in your Github Action, use the following syntax:

- name: Create .env with Github Secrets and run script
  run: |
    touch .env
    echo ENV_VAR_1=$ENV_VAR_1 >> .env
    echo ENV_VAR_2=$ENV_VAR_2 >> .env
    npm run script-that-accesses-env-vars
  env:
    ENV_VAR_1: ${{ secrets.ENV_VAR_1 }}
    ENV_VAR_2: ${{ secrets.ENV_VAR_2 }}

Echoing the ${{ secrets.ENV_VAR_1 }} directly to the .env file, instead of declaring the variables with the env key, didn't copy the correct value for me.