Passing GitHub Actions workflow step output to JavaScript

When you want your JavaScript code to access all of the data from a previous GitHub Actions workflow step.

Posted at by

Tags:,,,

GitHub Actions workflows allow each step to create output that is available in other steps, such as:

name:Test Steps Outputon:workflow_dispatchjobs:test:runs-on:ubuntu-lateststeps:      -id:releaserun:|          echo "release_created=true" >> "$GITHUB_OUTPUT"          echo "project_name=MyProject" >> "$GITHUB_OUTPUT"      -run:echo ${{ steps.release.outputs.release_created }}

This outputs therelease_created value from therelease step in the next step. You can pass this individually into a JavaScript file by creating environment variables:

name:Test Steps Outputon:workflow_dispatchjobs:test:runs-on:ubuntu-lateststeps:      -name:Check out repouses:actions/checkout@v4      -name:Set up Node.jsuses:actions/setup-node@v4      -id:releaserun:|          echo "release_created=true" >> "$GITHUB_OUTPUT"          echo "project_name=MyProject" >> "$GITHUB_OUTPUT"      -run:node extract.mjsenv:RELEASE_CREATED:${{ steps.release.outputs.release_created }}PROJECT_NAME:${{ steps.release.outputs.project_name }}

However, that can get unwieldy if there are more than a few step outputs, and especially if you’re unsure exactly which outputs will be created.

Instead, you can use thetoJSON() helper to serialize all of the step outputs into a JSON object and create one environment variable to capture it:

name:Test Steps Outputon:workflow_dispatchjobs:test:runs-on:ubuntu-lateststeps:      -name:Check out repouses:actions/checkout@v4      -name:Set up Node.jsuses:actions/setup-node@v4      -id:releaserun:|          echo "release_created=true" >> "$GITHUB_OUTPUT"          echo "project_name=MyProject" >> "$GITHUB_OUTPUT"      -run:node extract.mjsenv:RELEASE_OUTPUTS:${{ toJSON(steps.release.outputs) }}

Now, inside ofextract.mjs, you can use retrieve all of the data from therelease step by parsing theRELEASE_OUTPUTS environment variable:

constreleaseOutputs=JSON.parse(process.env.RELEASE_OUTPUTS||"{}");console.log(releaseOutputs.released_created);// trueconsole.log(releaseOutputs.project_name);// MyProject

This makes it a lot easier to create JavaScript files to work in your GitHub Actions workflow.

Master JavaScript Promises

Free E-book - Understanding JavaScript Promises

Understanding JavaScript Promises E-book Cover

What You'll Learn

  • Promise fundamentals and concepts
  • Real-world promise implementations
  • Best practices and patterns
  • Error handling techniques

Demystify JavaScript promises with practical examples and expert insights.

Download Your Free Copy

The community edition arrives in your inbox within minutes.