|
| 1 | +name: Test and Publish Workflow |
| 2 | + |
| 3 | +permissions: |
| 4 | + contents: write |
| 5 | + |
| 6 | +on: |
| 7 | + push: |
| 8 | + branches: |
| 9 | + - main |
| 10 | + - '!gh-pages' |
| 11 | + pull_request: |
| 12 | + |
| 13 | +jobs: |
| 14 | + build: |
| 15 | + runs-on: ubuntu-latest |
| 16 | + steps: |
| 17 | + - uses: actions/checkout@v5 |
| 18 | + - uses: actions/setup-node@v6 |
| 19 | + with: |
| 20 | + node-version: lts/* |
| 21 | + - run: npm i |
| 22 | + - run: npm run prepare -- --force |
| 23 | + - run: npm run build |
| 24 | + - uses: actions/upload-artifact@v6 |
| 25 | + with: |
| 26 | + name: build |
| 27 | + path: | |
| 28 | + index.js |
| 29 | + index.mjs |
| 30 | + index.iife.js |
| 31 | + index.d.ts |
| 32 | + browser.global.js |
| 33 | + browser.global.js.map |
| 34 | + retention-days: 1 |
| 35 | + |
| 36 | + tests: |
| 37 | + runs-on: ubuntu-latest |
| 38 | + steps: |
| 39 | + - uses: actions/checkout@v5 |
| 40 | + - uses: actions/setup-node@v6 |
| 41 | + with: |
| 42 | + node-version: lts/* |
| 43 | + - run: npm i |
| 44 | + - run: npm run build |
| 45 | + - run: npm run format |
| 46 | + - name: Push autofixes |
| 47 | + if: always() && github.event_name == 'push' |
| 48 | + run: | |
| 49 | + if ! git diff --quiet; then |
| 50 | + git config --global user.email omrilotan@users.noreply.github.com |
| 51 | + git config --global user.name omrilotan |
| 52 | + git add -A |
| 53 | + git commit -m "Autofix" |
| 54 | + git push origin HEAD:${GITHUB_REF#refs/heads/} |
| 55 | + fi |
| 56 | + - run: npm t |
| 57 | + |
| 58 | + compatibility: |
| 59 | + needs: build |
| 60 | + runs-on: ubuntu-latest |
| 61 | + strategy: |
| 62 | + matrix: |
| 63 | + node-version: |
| 64 | + - '18.20' |
| 65 | + - '20.19' |
| 66 | + - '22.18' |
| 67 | + - '24.6' |
| 68 | + steps: |
| 69 | + - uses: actions/checkout@v5 |
| 70 | + - uses: actions/setup-node@v6 |
| 71 | + with: |
| 72 | + node-version: ${{ matrix.node-version }} |
| 73 | + - run: node -v |
| 74 | + - run: npm i |
| 75 | + - uses: actions/download-artifact@v8 |
| 76 | + with: |
| 77 | + name: build |
| 78 | + - run: npm t -- spec |
| 79 | + |
| 80 | + publish: |
| 81 | + needs: |
| 82 | + - tests |
| 83 | + - compatibility |
| 84 | + runs-on: ubuntu-latest |
| 85 | + if: github.ref == 'refs/heads/main' && github.event_name == 'push' |
| 86 | + outputs: |
| 87 | + already_published: ${{ steps.check_tag.outputs.already_published }} |
| 88 | + steps: |
| 89 | + - uses: actions/checkout@v5 |
| 90 | + - uses: actions/setup-node@v6 |
| 91 | + with: |
| 92 | + node-version: lts/* |
| 93 | + - run: node -v |
| 94 | + - name: Check if remote tag already exists |
| 95 | + id: check_tag |
| 96 | + run: | |
| 97 | + TAG="v$(jq -r '.version' package.json)" |
| 98 | + if git ls-remote --exit-code --tags origin "refs/tags/${TAG}" >/dev/null; then |
| 99 | + echo "Tag ${TAG} already exists on origin. Skipping publish and pages." |
| 100 | + echo "already_published=true" >> "$GITHUB_OUTPUT" |
| 101 | + else |
| 102 | + echo "already_published=false" >> "$GITHUB_OUTPUT" |
| 103 | + fi |
| 104 | + - name: Set git user |
| 105 | + if: steps.check_tag.outputs.already_published == 'false' |
| 106 | + run: | |
| 107 | + git config --global user.name "$(git show -s --format=%an)" |
| 108 | + git config --global user.email "$(git show -s --format=%ae)" |
| 109 | + - run: npm i |
| 110 | + if: steps.check_tag.outputs.already_published == 'false' |
| 111 | + - name: Add publish token |
| 112 | + if: steps.check_tag.outputs.already_published == 'false' |
| 113 | + run: echo "//registry.npmjs.org/:_authToken=${{ secrets.NPM_TOKEN }}" > ~/.npmrc |
| 114 | + - run: npm publish |
| 115 | + if: steps.check_tag.outputs.already_published == 'false' |
| 116 | + - name: Add git tag |
| 117 | + if: steps.check_tag.outputs.already_published == 'false' |
| 118 | + run: git tag -a "v$(cat package.json | jq '.version' -r)" -m "$(git show -s --format=%B | tr -d '\n')" |
| 119 | + - run: git push origin --tags |
| 120 | + if: steps.check_tag.outputs.already_published == 'false' |
| 121 | + |
| 122 | + pages: |
| 123 | + needs: publish |
| 124 | + runs-on: ubuntu-latest |
| 125 | + if: github.ref == 'refs/heads/main' && github.event_name == 'push' |
| 126 | + steps: |
| 127 | + - uses: actions/checkout@v5 |
| 128 | + - uses: actions/setup-node@v6 |
| 129 | + with: |
| 130 | + node-version: lts/* |
| 131 | + - run: npm i |
| 132 | + - uses: actions/download-artifact@v8 |
| 133 | + with: |
| 134 | + name: build |
| 135 | + - run: npm run page |
| 136 | + - name: Copy github workflows config |
| 137 | + run: mkdir -p docs/.github/workflows && cp -r .github/workflows/* docs/.github/workflows/ |
| 138 | + - name: Post to gh-pages |
| 139 | + env: |
| 140 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 141 | + run: bash ./scripts/gh-pages/procedure.sh |
0 commit comments