|
1 | | -name: Deploy |
| 1 | +name: Deploy to GitHub Pages with Tests |
2 | 2 |
|
3 | 3 | on: |
4 | 4 | push: |
5 | 5 | branches: |
6 | | - - master |
| 6 | + - main |
7 | 7 | pull_request: |
8 | 8 | branches: |
9 | | - - master |
| 9 | + - main |
10 | 10 | workflow_dispatch: |
11 | 11 |
|
12 | 12 | jobs: |
| 13 | + test: |
| 14 | + name: Run Jest Tests |
| 15 | + runs-on: ubuntu-latest |
| 16 | + permissions: |
| 17 | + pull-requests: write |
| 18 | + contents: read |
| 19 | + issues: write |
| 20 | + |
| 21 | + steps: |
| 22 | + - name: Checkout Repository |
| 23 | + uses: actions/checkout@v4 |
| 24 | + |
| 25 | + - name: Setup Node.js |
| 26 | + uses: actions/setup-node@v4 |
| 27 | + with: |
| 28 | + node-version: '20' |
| 29 | + |
| 30 | + - name: Install Dependencies |
| 31 | + run: npm install |
| 32 | + |
| 33 | + - name: Run Jest Tests |
| 34 | + id: jest |
| 35 | + run: | |
| 36 | + npm test -- --json --outputFile=jest-results.json || echo "TESTS_FAILED=true" >> $GITHUB_ENV |
| 37 | +
|
| 38 | + - name: Read Jest Results |
| 39 | + id: results |
| 40 | + run: | |
| 41 | + if [ -z "$TESTS_FAILED" ]; then |
| 42 | + TESTS_FAILED=false |
| 43 | + fi |
| 44 | + if [ ! -f jest-results.json ]; then |
| 45 | + echo "TESTS_FAILED=true" >> $GITHUB_ENV |
| 46 | + fi |
| 47 | + if [ "$TESTS_FAILED" = "true" ]; then |
| 48 | + echo "TESTS_PASSED=false" >> $GITHUB_ENV |
| 49 | + else |
| 50 | + echo "TESTS_PASSED=true" >> $GITHUB_ENV |
| 51 | + fi |
| 52 | + PR_NUMBER=$(jq --raw-output .pull_request.number "$GITHUB_EVENT_PATH" 2>/dev/null || echo "0") |
| 53 | + echo "PR_NUMBER=$PR_NUMBER" >> $GITHUB_OUTPUT |
| 54 | +
|
| 55 | + - name: Comment on PR |
| 56 | + if: ${{ github.event_name == 'pull_request' }} |
| 57 | + uses: thollander/actions-comment-pull-request@v3 |
| 58 | + with: |
| 59 | + pr-number: ${{ steps.results.outputs.PR_NUMBER }} |
| 60 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 61 | + message: | |
| 62 | + ${{ steps.results.outputs.PR_NUMBER }} |
| 63 | + ${{ env.TESTS_FAILED == 'true' && '❌ Some Jest tests failed.' || '✅ All Jest tests passed!'}} |
| 64 | +
|
13 | 65 | deploy: |
| 66 | + name: Deploy to GitHub Pages |
14 | 67 | runs-on: ubuntu-latest |
| 68 | + needs: test |
| 69 | + if: github.repository == 'sugarlabs/musicblocks' && needs.test.outputs.TESTS_PASSED == 'true' |
15 | 70 |
|
16 | 71 | steps: |
17 | 72 | - name: Checkout repository |
18 | 73 | uses: actions/checkout@v3 |
19 | | - with: |
20 | | - token: ${{ secrets.GITHUB_TOKEN }} |
21 | | - |
22 | | - - name: Set up Git |
| 74 | + |
| 75 | + - name: Configure Git |
23 | 76 | run: | |
24 | 77 | git remote set-url origin https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }}.git |
25 | 78 | git config --global user.name "GitHub Actions" |
26 | 79 | git config --global user.email "github-actions[bot]@users.noreply.github.com" |
27 | | - |
28 | | - - name: Deploy to GitHub Pages |
| 80 | +
|
| 81 | + - name: Prepare deployment directory |
29 | 82 | run: | |
30 | 83 | mkdir -p ../deploy |
31 | 84 | cp -r * ../deploy/ |
| 85 | +
|
| 86 | + - name: Switch to gh-pages and pull latest changes |
| 87 | + run: | |
32 | 88 | git checkout gh-pages || git checkout --orphan gh-pages |
33 | | - git rm -rf . > /dev/null 2>&1 || true |
| 89 | + git pull origin gh-pages || true |
| 90 | + git rm -rf . || true |
34 | 91 | git clean -fxd |
| 92 | +
|
| 93 | + - name: Copy files to gh-pages |
| 94 | + run: | |
35 | 95 | cp -r ../deploy/* . |
36 | 96 | rm -rf ../deploy |
| 97 | +
|
| 98 | + - name: Commit and push changes |
| 99 | + run: | |
37 | 100 | git add . |
38 | 101 | git commit -m "Deploy updates from $(date)" || echo "No changes to commit" |
39 | | - git push origin gh-pages |
40 | | -
|
| 102 | + git pull origin gh-pages || true |
| 103 | + git push origin gh-pages || true |
0 commit comments