I've separated Github action jobs in two.
One is for setting GITHUB_ENV variables, named set-env-variables
and another is for deployment, named deploy
.
However every variable except "NODE_ENV" is set perfectly in deploy
job as I intended, and "NODE_ENV" is remained to be a default value.
Here is my simplified workflow file.
name: Deploy
on:
push:
branches:
- main
- staging
defaults:
run:
shell: bash
env:
NODE_ENV: development
DEPLOY_ENV: development
jobs:
set-env-variables:
runs-on: ubuntu-latest
steps:
- name: 'Check Pushed Branch'
run: |
BRANCH=$(echo ${GITHUB_REF##*/})
if [ ${BRANCH} = staging ]; then
echo "DEPLOY_ENV=staging" >> $GITHUB_ENV
echo "NODE_ENV=staging" >> $GITHUB_ENV
fi
deploy:
runs-on: ubuntu-latest
steps:
- name: 'Deploy by environment'
run: |
echo $NODE_ENV
echo $DEPLOY_ENV
# do some deploying jobs...
when pushing to staging
branch, Deploy by environment
step in deploy
job shows the result below.
development
staging
I have no idea why NODE_ENV hasn't changed, but every other variables have changed well.
Mind the gap:
You can make an environment variable available to any subsequent steps in a workflow job by defining or updating the environment variable and writing this to the
GITHUB_ENV
environment file.
(from : Setting an environment variable)
You have a different job.
Use GITHUB_OUTPUT
instead:
Sets a step's output parameter. Note that the step will need an
id
to be defined to later retrieve the output value.
(from: Setting an output parameter)
Example of setting an output parameter
- name: Set color
id: random-color-generator
run: echo "SELECTED_COLOR=green" >> "$GITHUB_OUTPUT"
- name: Get color
run: echo "The selected color is ${{ steps.random-color-generator.outputs.SELECTED_COLOR }}"
Defining outputs for jobs - Create a map of outputs for your jobs.
Example: Defining outputs for a job
jobs:
job1:
runs-on: ubuntu-latest
# Map a step output to a job output
outputs:
output1: ${{ steps.step1.outputs.test }}
output2: ${{ steps.step2.outputs.test }}
steps:
- id: step1
run: echo "test=hello" >> "$GITHUB_OUTPUT"
- id: step2
run: echo "test=world" >> "$GITHUB_OUTPUT"
job2:
runs-on: ubuntu-latest
needs: job1
steps:
- env:
OUTPUT1: ${{needs.job1.outputs.output1}}
OUTPUT2: ${{needs.job1.outputs.output2}}
run: echo "$OUTPUT1 $OUTPUT2"
Cf. GitHub Action - Define Workflow Level Environment Variable Between Jobs