DevOps / GitHub Actions Interview Questions
How do you share data between steps within a job using step outputs?
Steps within the same job communicate by writing key-value pairs to the special file at the path stored in $GITHUB_OUTPUT. Any subsequent step in the same job can then read that value via ${{ steps.<step-id>.outputs.<name> }}.
jobs: pipeline: runs-on: ubuntu-latest steps: - name: Generate version id: versioning # id is required to reference outputs run: | VERSION="1.4.${{ github.run_number }}" echo "version=$VERSION" >> $GITHUB_OUTPUT - name: Use version run: echo "Building version ${{ steps.versioning.outputs.version }}" - name: Tag Docker image run: | docker build -t myapp:${{ steps.versioning.outputs.version }} . docker push myapp:${{ steps.versioning.outputs.version }}
The id: field on the producing step is mandatory — without it, later steps have no handle to reference its outputs. The echo "key=value" >> $GITHUB_OUTPUT syntax appends to the output file; you can write multiple outputs from the same step by appending multiple lines.
Important: The older ::set-output command (written directly to stdout) was deprecated in 2022 and disabled in 2023 due to injection vulnerabilities. Always use $GITHUB_OUTPUT.
For multi-line values, use the heredoc syntax:
run: | echo "NOTES<> $GITHUB_OUTPUT cat CHANGELOG.md >> $GITHUB_OUTPUT echo "EOF" >> $GITHUB_OUTPUT
More Related questions...