1
0
mirror of https://github.com/jbranchaud/til synced 2026-01-07 09:08:01 +00:00
Files
til/github-actions/capture-an-output-value-for-use-in-a-later-step.md
Karim Bouchez 15337dfd71 Update the old way to capture a GitHub Actions output
See [here](https://github.blog/changelog/2022-10-11-github-actions-deprecating-save-state-and-set-output-commands/) for more explanations:
> We are monitoring telemetry for the usage of these commands and plan to fully disable them on 31st May 2023. Starting 1st June 2023 workflows using save-state or set-output commands via stdout will fail with an error.
2023-02-12 10:36:45 +01:00

1.5 KiB

Capture An Output Value For Use In A Later Step

GitHub Actions has a workflow command called set-output. This can be used to capture the output from a shell command in step. That output value can then be used in a later step.

A useful example of this is reading the version of a tool from a dot-file to tell a later step what version of that tool to install.

Here's the .tool-versions file included in my repository:

postgres 13.1
ruby 3.0.0
nodejs 15.4.0

Assuming I've already checked out my repo, I can find and read the nodejs version from my .tool-versions file with a step that uses set-output.

  - name: Read Node.js version to install from `.tool-versions`
    id: nodejs
    run: >-
      echo "NODE_VERSION=$(
        cat .tool-versions |
        grep nodejs |
        sed 's/nodejs \(.*\)$/\1/'
      )" >> $GITHUB_OUTPUT

echo runs the command in the string which sets NODE_VERSION as an output value to what ends up being 15.4.0.

This output value can be referenced in a later step.

  - name: Install required Node.js version
    uses: actions/setup-node@v1
    with:
      node-version: "${{ steps.nodejs.outputs.NODE_VERSION }}"

steps has a reference to the nodejs step (note the id above) which then has outputs like the NODE_VERSION.

source