bashshellgithub-actions

How can I pass an environment variable set in GitHub Actions to a bash script?


I have a requirement where I need to pass an environment variable set in GitHub Actions to a shell script which utilizes that environment variable to configure few things in that script. This is my workflow:

jobs:
  extract:
    name: Extract Jira Ticket Number
    runs-on: [self-hosted, linux, x64]
    outputs:
      output: ${{steps.extract_id.outputs.JIRA_TICKET_ID}}
    steps:
      - name: Extract Id
        id: extract_id
        run: |
          TICKET_ID=`echo "${{ github.ref }}" | grep -Eo 'DRA-[0-9]{1,9}'`
          echo $TICKET_ID
          echo "JIRA_TICKET_ID=${TICKET_ID}" >> $GITHUB_OUTPUT

  prepare:
    name: Running Branch Build
    runs-on: [self-hosted, windows]
    needs: extract
    steps:
      - name: Checkout source code
        uses: actions/checkout@v4
      - name: Set environment variables
        run: |
          echo "JIRA_TICKET_ID=${{needs.extract.outputs.output}}" >> $GITHUB_ENV
      - name: Test script
        run: .\scripts\deployment\test-script.sh

This is my script:

//test-script.sh 

#!/usr/bin/env bash
set -xe

echo "Jira Ticket Id: $JIRA_TICKET_ID"

The workflow passes without any error but does not log out the environment variable. Am I missing something?


Solution

    1. There is no need to have a separate step populating GITHUB_ENV, you can use env field to declare the environment variable you want to use. It will be available in your script as well

    2. Second job has windows runner, while the script seems bashy, hence either:

      2.1 You may want to run everything on linux runner to avoid issues with different OSes

      2.2 Specify shell: bash and run ./scripts/deployment/test-script.sh instead as the scripts seems to be bash (I assume you have to change to forward slashes)

      2.3 Write the script to be powershell-centric as that's the default shell set for Windows runners

    Additionally fixed a few issues (e.g. using ${{ var }} in run which is best avoided whenever possible due to possible template injection issues), see code comments for additional information:

    jobs:
      extract:
        name: Extract Jira Ticket Number
        runs-on: [self-hosted, linux, x64]
        outputs:
          output: ${{ steps.extract_id.outputs.JIRA_TICKET_ID }}
        steps:
          - name: Extract Id
            id: extract_id
            # Do not use "${{ github.ref }}", see
            # here: https://docs.github.com/en/actions/writing-workflows/choosing-what-your-workflow-does/store-information-in-variables#default-environment-variables
            run: |
              TICKET_ID=`echo "$GITHUB_REF" | grep -Eo 'DRA-[0-9]{1,9}'`
              echo $TICKET_ID
              echo "JIRA_TICKET_ID=${TICKET_ID}" >> $GITHUB_OUTPUT
    
      prepare:
        name: Running Branch Build
        runs-on: [self-hosted, windows]
        needs: extract
        steps:
          - name: Checkout source code
            uses: actions/checkout@v4
          - name: Test script
            # This is enough to be available in the environment
            env:
              JIRA_TICKET_ID: ${{needs.extract.outputs.output}}
            # As this is windows, you might consider changing shell to bash explicitly as so:
            # shell: bash
            # run: ./scripts/deployment/test-script.sh
            run: .\scripts\deployment\test-script.sh
            # Altogether you can also do so directly:
            # run: echo "$JIRA_TICKET_ID"
    

    Additional references:

    BTW. These jobs could be one instead, but I assume it is just a MVC and not the actual code.