skip to Main Content

I have a .yml file for my CI/CD pipeline in Github Actions. I have an env variable that gets set at the job level, and then reset at the step level for a single step. However, the env at the step level doesn’t get registered.

The following is the entire yml file:

# This workflow will do a clean install of node dependencies, build the source code and run tests across different versions of node
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions

name: Build, Lint and Test

on:
  push:
    branches:
      - dev
      - main
  pull_request:
    branches:
      - dev
      - main
      - CU-*
  workflow_dispatch:

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

jobs:
  build_lint_test:
    runs-on: ubuntu-latest
    timeout-minutes: 20
    environment: staging
    env:
      CI: true
      NPM_AUTH_TOKEN: ${{ secrets.NPM_AUTH_TOKEN }}
      NEXT_PUBLIC_MAPBOX_PUBLIC_TOKEN: ${{ secrets.NEXT_PUBLIC_MAPBOX_PUBLIC_TOKEN }}
      ALLOWED_IMAGE_DOMAINS: ${{ vars.ALLOWED_IMAGE_DOMAINS }}
      NEXT_PUBLIC_SERVER_DOMAIN: ${{ vars.NEXT_PUBLIC_SERVER_DOMAIN }}
      NEXT_PUBLIC_API_TIMEOUT: ${{ vars.NEXT_PUBLIC_API_TIMEOUT }}
      NEXT_PUBLIC_API_RETRY_COUNT: ${{ vars.NEXT_PUBLIC_API_RETRY_COUNT }}
      NEXT_PUBLIC_SITE_URL: ${{ vars.NEXT_PUBLIC_SITE_URL }}
      NEXT_PUBLIC_AUTH0_DOMAIN: ${{ vars.NEXT_PUBLIC_AUTH0_DOMAIN }}
      NEXT_PUBLIC_AUTH0_CLIENT_ID: ${{ vars.NEXT_PUBLIC_AUTH0_CLIENT_ID }}
      NEXT_PUBLIC_SENTRY_DSN: ${{ vars.NEXT_PUBLIC_SENTRY_DSN }}
      NEXT_PUBLIC_HEAP_ID: ${{ vars.NEXT_PUBLIC_HEAP_ID }}
      NEXT_PUBLIC_APP_ENV: ${{ vars.NEXT_PUBLIC_APP_ENV }} // ENV VARIABLE HERE
      NEXT_PUBLIC_STAGING_URL: ${{ vars.NEXT_PUBLIC_STAGING_URL }}
      NEXT_PUBLIC_PRODUCTION_URL: ${{ vars.NEXT_PUBLIC_PRODUCTION_URL }}
      NEXT_PUBLIC_DEVELOPMENT_URL: ${{ vars.NEXT_PUBLIC_DEVELOPMENT_URL }}
      NEXT_PUBLIC_GOOGLE_MAPS_API_KEY: ${{ secrets.NEXT_PUBLIC_GOOGLE_MAPS_API_KEY }}
      CYPRESS_TEST_LOGIN_EMAIL: ${{ secrets.TEST_LOGIN_EMAIL }}
      CYPRESS_TEST_LOGIN_PASSWORD: ${{ secrets.TEST_LOGIN_PASSWORD }}

    steps:
      - uses: actions/checkout@v4
        with:
          clean: true

      - uses: actions/checkout@v4
        with:
          repository: 'akadenia/PerimeterTypes'
          path: PerimeterTypes
          token: ${{ secrets.CI_TOKEN }}
          clean: true

      - name: Git Submodule Update
        run: |
          git submodule update --init --recursive

      - uses: actions/setup-node@v3
        with:
          node-version-file: '.nvmrc'

      - run: npm ci

      - name: Run Linter
        run: npm run lint

      - name: Build code
        run: npm run build

      - name: Echo Environment Variable
        run: echo $NEXT_PUBLIC_APP_ENV
        env:
          NEXT_PUBLIC_APP_ENV: test

      - name: Cypress run
        uses: cypress-io/github-action@v6
        with:
          start: npm start
          env: NEXT_PUBLIC_APP_ENV=test // ENV HERE AGAIN

The env variable in question is NEXT_PUBLIC_APP_ENV. It gets set at the job level to be ‘staging’ with:

${{ vars.NEXT_PUBLIC_APP_ENV }}

However, it’s reset at the step level for the Cypress run step, which uses:

env:
   NEXT_PUBLIC_APP_ENV: test

However, within cypress testing, the APP_ENV variable gets registered as staging instead of test like it’s supposed to. Is there a reason for this? The previous echo correctly logs APP_ENV as being test, and the docs for the GitHub Actions say to use that separate syntax for env variables in the Cypress run step. I’ve also tried using start: npm run dev instead of start: npm start which works since I directly set the env variable in my dev script, but this causes cypress testing to take almost twice as long. Setting the env variable in my start script doesn’t have any effect.

2

Answers


  1. It looks like the formatting of the last line is incorrect – although further down in the question it looks correct.

    Change the indenting and the key/value pair formatting:

          - name: Cypress run
            uses: cypress-io/github-action@v6
            with:
              start: npm start
            env: NEXT_PUBLIC_APP_ENV: test 
    
    Login or Signup to reply.
  2. I think what’s happening is that by setting

    env:
         NEXT_PUBLIC_APP_ENV: test
    

    in the Echo Environment Variable step you are providing a local variable that temporarily shadows the one you set at the job level. It is a different variable that only exists in that step. If you want to actually change the value of the one at the job level I would try with

    run: echo "NEXT_PUBLIC_APP_ENV=test" >> $GITHUB_ENV
    

    Based on this other question and this documentation.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search