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: poc workflow
run-name: ${{ github.workflow }} run by ${{ github.actor }}
on:
  workflow_dispatch:
jobs:
  job1:
    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: poc 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:
  job1:
    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: poc workflow
run-name: ${{ github.workflow }} run by ${{ github.actor }}
on:
  workflow_dispatch:
jobs:
  job1:
    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}}

 

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 1fee4c in the box below so that we can be sure you are a human.