AI Hints
Home›CI/CD with GitHub Actions Tutorial›Secrets, Tokens and Outputs
CI/CD with GitHub Actions · Lesson 42 of 138

Step Outputs

Passing values on.

⏱ 2 min read▶ Video at 48:18🎓 Free
Watch this lesson in the CI/CD with GitHub Actions courseWatch on YouTube ↗Full CI/CD with GitHub Actions course

Often one step works something out, like a version number, and a later step needs it. Shell variables don't survive between steps, because each step is a new shell. Step outputs solve this.

Step Outputs
From the video: Step Outputs
  • One step works it out
  • A later step needs it
  • Step outputs

Writing and reading outputs

Give the step an i d. Then write a line, name equals value, into the file whose path is in GITHUB output. Later steps read it with steps dot the i d dot outputs dot the name. Here, one step makes a version number, another counts the Python files, and the last step uses both.

.github/workflows/outputs.ymlyaml
name: Outputs
on: push
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - id: version
        run: echo "number=1.4.$GITHUB_RUN_NUMBER" >> "$GITHUB_OUTPUT"
      - id: files
        run: echo "count=$(ls *.py | wc -l)" >> "$GITHUB_OUTPUT"
      - run: echo "Version ${{ steps.version.outputs.number }} has ${{ steps.files.outputs.count }} Python files"

Values flowing between steps

The last step prints both values, which came from the earlier steps. Version one point four point one, where one is the run number, and two Python files. This is how pipelines pass tags, versions, and file names from step to step.

Terminal
run-steps .github/workflows/outputs.yml
Workflow: Outputs  (event: push, branch: main)
Job: build
  ✓ Run echo "number=1.4.$GITHUB_RUN_NUMBER" >> "$GITHUB_OUTPUT"
  ✓ Run echo "count=$(ls *.py | wc -l)" >> "$GITHUB_OUTPUT"
  ✓ Run echo "Version 1.4.1 has 2 Python files"
      Version 1.4.1 has 2 Python files
  Job succeeded

Summary

Write name equals value to GITHUB output, and read it later with steps dot i d dot outputs dot name. Next, workflows with several jobs.

Key takeaways
  • id: on the step
  • >> "$GITHUB_OUTPUT"
  • steps.id.outputs.name
Next up: Multiple Jobs — parallel work