Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set environment variable in node.js process when deploying with github action

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)

like image 681
Sihoon Kim Avatar asked Apr 09 '20 09:04

Sihoon Kim


People also ask

How do I set 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.

Where do I put environment variables in GitHub?

Setting for GitHub Actions Environment Variables 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.

How do you set environment as production in node JS?

You can signal Node. js that you are running in production by setting the NODE_ENV=production environment variable. in the shell, but it's better to put it in your shell configuration file (e.g. . bash_profile with the Bash shell) because otherwise the setting does not persist in case of a system restart.


1 Answers

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
like image 86
kachow6 Avatar answered Oct 09 '22 01:10

kachow6