Search code examples
github-actions

NODE_ENV environment variable in Github action is not changed in separated jobs


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.


Solution

  • 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)

    Cf. GitHub Action - Define Workflow Level Environment Variable Between Jobs