skip to Main Content

I am using the very simple workflow:

name: Deploy Frontend


# env:
#   REACT_APP_API_URL: ${{ vars.REACT_APP_API_URL }}
#   CORS_ALLOWED_ORIGINS: ${{ vars.CORS_ALLOWED_ORIGINS }}
env:
  REACT_APP_API_URL: MY_REACT_APP_API_URL
  CORS_ALLOWED_ORIGINS: MY_CORS_ALLOWED_ORIGINS


on:
  push:
    branches: [main, staging, frontend]
    paths:
      - 'frontend/**'
      - '.github/workflows/deploy_frontend.yml'
  workflow_dispatch:

jobs:
  build-and-deploy:
    runs-on: ubuntu-latest
    permissions:
      contents: write
      pages: write       # Added for GitHub Pages deployment
      id-token: write    # Necessary for actions/deploy-pages
    environment:
      name: github-pages
      url: ${{ steps.deployment.outputs.page_url }}
    steps:
    - uses: actions/checkout@v4

    - name: Debug REACT_APP_API_URL
      shell: bash
      run: |
        echo -e "e[32m@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@e[0m"
        echo -e "e[32mREACT_APP_API_URL=${REACT_APP_API_URL}e[0m"
        echo -e "e[32mREACT_APP_API_URL=${CORS_ALLOWED_ORIGINS}e[0m"
        env

which writes MY_REACT_APP_API_URL=MY_MY_REACT_APP_API_URL (and so on) if it is hard coded, or MY_REACT_APP_API_URL= if I am trying to get it from vars.REACT_APP_API_URL.

This makes me think I defined it incorrectly in github.

Here is what my github looks like, and please note env doesn’t recognize anything defined in the variables section.

enter image description here

None of the environment variables are available during the github actions run.


What did I define wrong and how to make it right?

2

Answers


  1. Chosen as BEST ANSWER

    Answer found here (but really it took hours with GPT and a hint from @Bademeister).

    I had the environment set to gh-pages whereas it should be set to

     environment:
          name: ${{ github.ref == 'refs/heads/main' && 'prod' || 'staging' }}
    

    So the jobs section should be

    jobs:
      build-and-deploy:
        runs-on: ubuntu-latest
        permissions:
          contents: write
          pages: write       
          id-token: write   
        environment:
          name: ${{ github.ref == 'refs/heads/main' && 'prod' || 'staging' }} # <--- this
          url: ${{ steps.deployment.outputs.page_url }}
        steps:
        - uses: actions/checkout@v4
    

    And suddenly all variables go in the workflow


  2. What I can not see in your workflow is that you also set the environment name. I set the environment name via an input.

    Here is an example.

    name: Build and Deploy
    
    on:
      workflow_dispatch:
        inputs:
          environment:
            type: choice
            description: Choose environment
            options:
            - stage
            - production
    jobs:
      deploy:
        environment: ${{inputs.environment}}
        runs-on: ubuntu-latest
        steps:
        # ..
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search