DEV Community

Cover image for Understanding the GitHub Actions Environment
Daniel
Daniel

Posted on β€’ Edited on

1

Understanding the GitHub Actions Environment

Here's all that you need to know on how to handle:

  • Environment Variables
  • Outputs
  • States

Environment Variables

Use these when you're going to use them all across a GitHub Actions file.

For instance this could be the name of a host you want to connect to or a secret token.

There are 3 ways to set environment variables:

1. In GitHub if you're going to use them across different files

  1. Go to your repository
  2. Go to settings
  3. In the left panel select "Secrets and variables"
  4. Click on "Actions"
  5. Store any variables and secrets you want to use in your actions files.
  6. In the file you can get these by USING $NAME_OF_YOUR_VARIABLE_OR_SECRET

2. In the env section of an Action .yml file

Useful when you're going to use it across a whole file. This saves time if the value will ever change, only needing to change one line instead of multiple ones.

env:
  COLOR: yellow

...

steps:
  - name: Print my color
    run: printf '%s\n' "$COLOR" # This will output 'yellow'
Enter fullscreen mode Exit fullscreen mode

3. In a step in the Action .yml file

This is useful if the variable is going to be used across an Action but the variable is set dynamically in a step.

steps:
  - name: Set the value
    id: step-one
    run: |
      echo "COLOR=yellow" >> $GITHUB_ENV

  - name: Use the value
    id: step-two
    run: |
      printf '%s\n' "$COLOR" # This will output 'yellow'
Enter fullscreen mode Exit fullscreen mode

Outputs

Outputs are useful when you want to get data from specific steps.

- name: Set color
  id: set-color
  run: echo "SELECTED_COLOR=green" >> $GITHUB_OUTPUT

- name: Get color
  env: SELECTED_COLOR: ${{ steps.color-selector.outputs.SELECTED_COLOR }}
  run: echo "The selected color is $SELECTED_COLOR"
Enter fullscreen mode Exit fullscreen mode

States

States are a bit different in that they are state variables that can be shared between execution files.

If you're using pre:, main: and post: workflows in actions you can set a state to be shared in those files:

runs:
  using: 'node20'
  pre: 'setup.js'
  post: 'cleanup.js'

...

- name: Save state
  run: echo "{name}={value}" >> $GITHUB_STATE
Enter fullscreen mode Exit fullscreen mode

In the .js files you can get the state by:

process.env.STATE_variableName
Enter fullscreen mode Exit fullscreen mode

Heroku

This site is built on Heroku

Join the ranks of developers at Salesforce, Airbase, DEV, and more who deploy their mission critical applications on Heroku. Sign up today and launch your first app!

Get Started

Top comments (0)

The Most Contextual AI Development Assistant

Pieces.app image

Our centralized storage agent works on-device, unifying various developer tools to proactively capture and enrich useful materials, streamline collaboration, and solve complex problems through a contextual understanding of your unique workflow.

πŸ‘₯ Ideal for solo developers, teams, and cross-company projects

Learn more

πŸ‘‹ Kindness is contagious

Please leave a ❀️ or a friendly comment on this post if you found it helpful!

Okay