Bootstrap FreeKB - GitHub Actions - Getting Started with Environment Variables
GitHub Actions - Getting Started with Environment Variables

Updated:   |  GitHub Actions articles

GitHub Actions can be used to do something whenever something happens in one of your GitHub repositories. If you are not familiar with GitHub Actions, check out my article Getting Started with GitHub Actions.

If using a Linux runner, such as Ubuntu, run can be used to issue the echo command to create variables that contain the default GitHub Environment Variables. https://docs.github.com/en/actions/learn-github-actions/variables has the various default Environment Variables that can be used.

name: my-workflow
run-name: ${{ github.workflow }} run by ${{ github.actor }}
on:
  workflow_dispatch:
jobs:
  my-job:
    runs-on: ubuntu-latest
    steps:    
      - name: builtin variables in job ${{ github.job }}
        run: |
          echo "REPOSITORY = $REPOSITORY"
          echo "REPOSITORY_OWNER = $REPOSITORY_OWNER"
        env:
          REPOSITORY: ${{ github.repository }}
          REPOSITORY_OWNER: ${{ github.repository_owner }}

 

Or like this, with the environment variables set outside of jobs.

name: my-workflow
run-name: ${{ github.workflow }} run by ${{ github.actor }}
on:
  workflow_dispatch:
env:
  REPOSITORY: ${{ github.repository }}
  REPOSITORY_NAME: ${{ github.event.repository.name }}
  REPOSITORY_OWNER: ${{ github.repository_owner }}  
jobs:
  my-job:
    runs-on: ubuntu-latest
    steps:    
      - name: REPOSITORY
        run: echo $REPOSITORY

      - name: REPOSITORY_NAME
        run: echo $REPOSITORY_NAME

      - name: REPOSITORY_OWNER
        run: echo $REPOSITORY_OWNER          

 

Or like this, using GITHUB_OUTPUT.

name: my-workflow
run-name: ${{ github.workflow }} run by ${{ github.actor }}
on:
  workflow_dispatch:
jobs:
  my-job:
    runs-on: ubuntu-latest
    steps:    
      - name: set repository
        id: repo
        run: echo "repository=${{ github.repository }}" >> $GITHUB_OUTPUT

      - name: get repository
        run: echo ${{ steps.repo.outputs.repository}}

 

Here is how you can create a variable that contains two or more whitespace separated values and loop through each item. This is just like looping through items in bash.

name: my-workflow
run-name: ${{ github.workflow }} run by ${{ github.actor }}
on:
  workflow_dispatch:
env:
  TARGET_SERVERS: "server1.example.com server2.example.com"
jobs:
  my-job:
    runs-on: ubuntu-latest
    steps:    
      - name: my loop
        run: |
          for SERVER in $(echo "$TARGET_SERVERS"); do
            echo $SERVER
           done

 

If you have two or more GitHub Actions that have identical variables, this is a scenario where it makes sense to create the variable in your repo at Settings > Secrets and variables > Actions > Variables. In this example, there are two variables, foo and bar.

 

And here is an example of how to use the foo and bar variables in a workflow.

name: my-workflow
run-name: ${{ github.workflow }} run by ${{ github.actor }}
on:
  workflow_dispatch:
jobs:
  my-job:
    runs-on: ubuntu-latest
    steps:    
      - name: this should print Hello
        run: echo ${{ vars.FOO }}
        
      - name: this should print World
        run: echo ${{ vars.BAR }}        

 

In this example, when looking at the workflow run, since run-name:  ${{ github.workflow }} run by ${{ github.actor }} was used, the run should include the name of the workflow and the user that ran the workflow.

 

And the job output should contain something like this.

 

 

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 7b37dd in the box below so that we can be sure you are a human.