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

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Dive into an ocean of knowledge with this thought-provoking post, revered deeply within the supportive DEV Community. Developers of all levels are welcome to join and enhance our collective intelligence.

Saying a simple "thank you" can brighten someone's day. Share your gratitude in the comments below!

On DEV, sharing ideas eases our path and fortifies our community connections. Found this helpful? Sending a quick thanks to the author can be profoundly valued.

Okay