Objective
A repository at /home/interview/repo contains a Node.js application with a test suite that needs to be tested across multiple Node.js versions (18, 20, and 22). The repository has the upload-artifact action available locally at .github/actions/upload-artifact.
Task
Complete the workflow at .github/workflows/pr-tests.yml that runs the test suite across Node.js versions 18, 20, and 22 using a matrix strategy with container images (node:${{ matrix.node-version }}-slim). Each matrix job should create a node-version.txt file containing its Node.js version number and upload it as an artifact named node-<version>-version (e.g., node-18-version) using the local upload-artifact action. The workflow should trigger on pull requests.
File Path
- Workflow:
/home/interview/repo/.github/workflows/pr-tests.yml
- Test suite:
/home/interview/repo/run-tests.sh
name: Matrix Test
on:
pull_request:
jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [18, 20, 22]
container:
image: node:${{ matrix.node-version }}-slim
steps:
- uses: actions/checkout@v4
- name: Run tests
run: ./run-tests.sh
- name: Create version artifact
run: echo "${{ matrix.node-version }}" > node-version.txt
- name: Upload artifact
uses: ./.github/actions/upload-artifact
with:
name: node-${{ matrix.node-version }}-version
path: node-version.txt
Explanation
Step 1: Matrix Strategy
strategy:
matrix:
node-version: [18, 20, 22]
This tells GitHub Actions to run the same job three times, once for each value in the array. Each run gets its own matrix.node-version variable (18, 20, or 22). The jobs run in parallel by default.
Step 2: Container Image
container:
image: node:${{ matrix.node-version }}-slim
Instead of installing Node.js on the runner, we run the entire job inside a Docker container with Node.js pre-installed. node:18-slim, node:20-slim, and node:22-slim are official Node.js images. This is faster and more reliable than using setup actions.
Step 3: Per-Version Artifacts
- name: Create version artifact
run: echo "${{ matrix.node-version }}" > node-version.txt
- name: Upload artifact
uses: ./.github/actions/upload-artifact
with:
name: node-${{ matrix.node-version }}-version
path: node-version.txt
Each matrix job writes its version number to node-version.txt and uploads it with a unique name (node-18-version, node-20-version, node-22-version). The unique name prevents matrix jobs from overwriting each other's artifacts.