Objective
A repository at /home/interview/repo has a Node.js application with a test suite. The team needs a two-stage pipeline where the first job runs tests and the second job creates a summary report from those results. The repository has upload-artifact and download-artifact actions available locally at .github/actions/.
Task
Complete the workflow at .github/workflows/artifact-handoff.yml that triggers on pull_request events using container images (node:20-slim). Job A (test-job) should check out code, run ./run-tests.sh, and upload test-results.txt as an artifact named test-results. Job B (report-job) should depend on Job A using needs, download the test-results artifact, count passed tests with grep -c "PASS:", save the count to summary.txt as "Total Passed Tests: X", and upload it as a test-summary artifact.
File Path
- Workflow:
/home/interview/repo/.github/workflows/artifact-handoff.yml
- Test suite:
/home/interview/repo/run-tests.sh
name: Artifact Handoff Workflow
on:
pull_request:
jobs:
test-job:
runs-on: ubuntu-latest
container:
image: node:20-slim
steps:
- uses: actions/checkout@v4
- name: Run tests
run: ./run-tests.sh
- name: Upload test results
uses: ./.github/actions/upload-artifact
with:
name: test-results
path: test-results.txt
report-job:
runs-on: ubuntu-latest
container:
image: node:20-slim
needs: test-job
steps:
- uses: actions/checkout@v4
- name: Download test results
uses: ./.github/actions/download-artifact
with:
name: test-results
- name: Create summary report
run: |
PASSED_COUNT=$(grep -c "PASS:" test-results.txt || echo "0")
echo "Total Passed Tests: $PASSED_COUNT" > summary.txt
cat summary.txt
- name: Upload summary
uses: ./.github/actions/upload-artifact
with:
name: test-summary
path: summary.txt
Explanation
Step 1: Test Job Uploads Artifact
- name: Upload test results
uses: ./.github/actions/upload-artifact
with:
name: test-results
path: test-results.txt
After running the tests, the test job uploads test-results.txt as an artifact. This file is stored by GitHub Actions and can be downloaded by other jobs in the same workflow.
Step 2: Report Job Downloads Artifact
report-job:
needs: test-job
steps:
- uses: ./.github/actions/download-artifact
with:
name: test-results
needs: test-job ensures the report job waits for the test job to finish. Then download-artifact retrieves the test-results artifact that was uploaded in the previous job. The file appears in the working directory as test-results.txt.
Step 3: Create Summary
- name: Create summary report
run: |
PASSED_COUNT=$(grep -c "PASS:" test-results.txt || echo "0")
echo "Total Passed Tests: $PASSED_COUNT" > summary.txt
grep -c "PASS:" counts the number of lines containing "PASS:" in the test results. The -c flag returns the count instead of the matching lines. || echo "0" is a fallback: if grep finds no matches it exits with code 1, so || echo "0" catches that and outputs "0" instead of failing the step. The count is then written to summary.txt.