github-actions

Github actions env is not the same as script's env


I have a workflow like this:

name: System Test

on:
  repository_dispatch:
    types:
      - test

jobs:
  run-tests:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout Testing Repository
        uses: actions/checkout@v3

      - name: setting env
        env:
          ert: hola
        run: |
          echo "test: $ert"
          echo "ert=hola" >> $GITHUB_ENV
          env

      - name: System testing (build + execution)
        run: |
          ls
          export ert=hola
          chmod +x testing.sh
          sudo ./testing.sh

I tried all the options, but no one gave me the result I want. In the testing.sh script, it appears only an env command, and in the 'setting env' step, it has another env command. The problem is that in the 'setting env' step, the env command shows me ert=hola, but in the script, it doesn't. Why the variable is not set as an environment variable?


Solution

  • There were a few subtle issues:

    1. Environment Variable Case Sensitivity

    2. Inconsistent Variable Setting

    The corrected version will be like this: Also, read more about the environment scope in workflow, job, and step level.

    name: System Test
    on:
      repository_dispatch:
        types:
          - test
    jobs:
      run-tests:
        runs-on: ubuntu-latest
        steps:
          - name: Checkout Testing Repository
            uses: actions/checkout@v3
          
          - name: Setting Environment Variables
            run: |
              echo "ERT=hola" >> $GITHUB_ENV
          
          - name: Verify Environment Variables
            run: |
              echo "Environment variable: $ERT"
              env | grep ERT
          
          - name: System testing (build + execution)
            run: |
              echo "In testing script: $ERT"
              chmod +x testing.sh
              ./testing.sh
    

    testing.sh

    #!/bin/bash
    echo "ERT in script: $ERT"
    env | grep ERT
    

    How I triggered it:

    gh api repos/myusername/mygithubrepo/dispatches --field event_type=test
    

    Output:

    enter image description here