Reputation: 1799
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)
Upvotes: 48
Views: 33297
Reputation: 1146
Moving the env variables from step-level to job-level, helped me fix this issue.
Previously, my workflow file looked like this:
jobs:
build:
name: Build
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v4
with:
node-version: 20
- run: npm install
- run: npm run test
- run: npm run build
env:
VAR_1: ${{ secrets.VAR_1 }}
VAR_2: ${{ secrets.VAR_2 }}
Although, these variables where accessible for the shell commands, it wasn't accessible inside process.env
of my node.js application.
To make it accessible, i've moved it a job level making it accessible for each step in that job. My assumption is that github actions only makes job-level env variables accessible via process.env
.
Solution:
jobs:
build:
name: Build
runs-on: ubuntu-latest
env:
VAR_1: ${{ secrets.VAR_1 }}
VAR_2: ${{ secrets.VAR_2 }}
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v4
with:
node-version: 20
- run: npm install
- run: npm run test
- run: npm run build
Upvotes: 1
Reputation: 395
- 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
Upvotes: 3
Reputation: 1731
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
Upvotes: 61
Reputation: 41
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.
Upvotes: 3