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
- Go to your repository
- Go to settings
- In the left panel select "Secrets and variables"
- Click on "Actions"
- Store any variables and secrets you want to use in your actions files.
- 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'
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'
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"
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
In the .js
files you can get the state by:
process.env.STATE_variableName
Top comments (0)