PR Comment Test Runner #25
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: PR Comment Test Runner | |
on: | |
issue_comment: | |
types: [created] | |
workflow_dispatch: | |
permissions: | |
statuses: write | |
pull-requests: read | |
jobs: | |
test: | |
if: ${{ github.event.issue.pull_request && contains(github.event.comment.body, 'run tests') }} | |
runs-on: ubuntu-latest | |
steps: | |
- uses: actions/checkout@v3 | |
with: | |
# Fetch all history and tags | |
fetch-depth: 0 | |
# Checkout PR branch | |
ref: refs/pull/${{ github.event.issue.number }}/head | |
# Get PR SHA | |
- name: Get PR SHA | |
id: get-pr-info | |
run: | | |
PR_SHA=$(git rev-parse HEAD) | |
echo "sha=$PR_SHA" >> $GITHUB_OUTPUT | |
# Install pnpm first | |
- name: Setup pnpm | |
uses: pnpm/action-setup@v2 | |
with: | |
version: 8 | |
run_install: false | |
# Then setup Node.js with pnpm caching | |
- name: Setup Node.js | |
uses: actions/setup-node@v3 | |
with: | |
node-version: '18' | |
cache: 'pnpm' | |
- name: Get target branch | |
id: get-target-branch | |
run: | | |
TARGET_BRANCH=$(gh pr view ${{ github.event.issue.number }} --json baseRefName -q .baseRefName) | |
echo "target_branch=$TARGET_BRANCH" >> $GITHUB_OUTPUT | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
- name: Install dependencies | |
run: pnpm install --frozen-lockfile | |
- name: List directory contents | |
run: | | |
pwd | |
ls -la | |
ls -la scripts/ | |
- name: Run tests | |
id: run-tests | |
continue-on-error: true | |
run: | | |
set +e # Don't exit on error | |
if [ -f "./scripts/runMinimalTests.js" ]; then | |
echo "Found test script" | |
pnpm test:minimal:pr ${{ steps.get-target-branch.outputs.target_branch }} ${{ github.event.pull_request.head.ref }} 2>&1 | tee test_output.txt | |
else | |
echo "Error: Cannot find test script at ./scripts/runMinimalTests.js" | |
echo "Current directory: $(pwd)" | |
ls -la | |
ls -la scripts/ | |
exit 1 | |
fi | |
TEST_EXIT_CODE=${PIPESTATUS[0]} | |
# Prepare the output | |
echo "output<<EOF" >> $GITHUB_OUTPUT | |
cat test_output.txt >> $GITHUB_OUTPUT | |
echo "EOF" >> $GITHUB_OUTPUT | |
# Set the status based on exit code | |
if [ $TEST_EXIT_CODE -eq 0 ]; then | |
echo "status=success" >> $GITHUB_OUTPUT | |
else | |
echo "status=failure" >> $GITHUB_OUTPUT | |
fi | |
echo "exit_code=$TEST_EXIT_CODE" >> $GITHUB_OUTPUT | |
- name: Create Status Check | |
uses: actions/github-script@v6 | |
with: | |
script: | | |
const testStatus = '${{ steps.run-tests.outputs.status }}'; | |
const testOutput = `${{ steps.run-tests.outputs.output }}`; | |
const exitCode = '${{ steps.run-tests.outputs.exit_code }}'; | |
await github.rest.repos.createCommitStatus({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
sha: '${{ steps.get-pr-info.outputs.sha }}', | |
state: testStatus === 'success' ? 'success' : 'failure', | |
target_url: `${process.env.GITHUB_SERVER_URL}/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}`, | |
description: testStatus === 'success' | |
? 'Tests passed successfully!' | |
: `Tests failed with exit code ${exitCode}`, | |
context: 'PR Tests' | |
}); |