Skip to content
103 changes: 103 additions & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
name: Deploy to GitHub Pages with Tests

on:
push:
branches:
- main
pull_request:
branches:
- main
workflow_dispatch:

jobs:
test:
name: Run Jest Tests
runs-on: ubuntu-latest
permissions:
pull-requests: write
contents: read
issues: write

steps:
- name: Checkout Repository
uses: actions/checkout@v4

- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '20'

- name: Install Dependencies
run: npm install

- name: Run Jest Tests
id: jest
run: |
npm test -- --json --outputFile=jest-results.json || echo "TESTS_FAILED=true" >> $GITHUB_ENV

- name: Read Jest Results
id: results
run: |
if [ -z "$TESTS_FAILED" ]; then
TESTS_FAILED=false
fi
if [ ! -f jest-results.json ]; then
echo "TESTS_FAILED=true" >> $GITHUB_ENV
fi
if [ "$TESTS_FAILED" = "true" ]; then
echo "TESTS_PASSED=false" >> $GITHUB_ENV
else
echo "TESTS_PASSED=true" >> $GITHUB_ENV
fi
PR_NUMBER=$(jq --raw-output .pull_request.number "$GITHUB_EVENT_PATH" 2>/dev/null || echo "0")
echo "PR_NUMBER=$PR_NUMBER" >> $GITHUB_OUTPUT

- name: Comment on PR
if: ${{ github.event_name == 'pull_request' }}
uses: thollander/actions-comment-pull-request@v3
with:
pr-number: ${{ steps.results.outputs.PR_NUMBER }}
github-token: ${{ secrets.GITHUB_TOKEN }}
message: |
${{ steps.results.outputs.PR_NUMBER }}
${{ env.TESTS_FAILED == 'true' && '❌ Some Jest tests failed.' || '✅ All Jest tests passed!'}}

deploy:
name: Deploy to GitHub Pages
runs-on: ubuntu-latest
needs: test
if: github.repository == 'sugarlabs/musicblocks' && needs.test.outputs.TESTS_PASSED == 'true'

steps:
- name: Checkout repository
uses: actions/checkout@v3

- name: Configure Git
run: |
git remote set-url origin https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }}.git
git config --global user.name "GitHub Actions"
git config --global user.email "github-actions[bot]@users.noreply.github.com"

- name: Prepare deployment directory
run: |
mkdir -p ../deploy
cp -r * ../deploy/

- name: Switch to gh-pages and pull latest changes
run: |
git checkout gh-pages || git checkout --orphan gh-pages
git pull origin gh-pages || true
git rm -rf . || true
git clean -fxd

- name: Copy files to gh-pages
run: |
cp -r ../deploy/* .
rm -rf ../deploy

- name: Commit and push changes
run: |
git add .
git commit -m "Deploy updates from $(date)" || echo "No changes to commit"
git pull origin gh-pages || true
git push origin gh-pages || true
Loading