|
| 1 | +# This workflow will do a clean install of node dependencies, build the source code and run tests across different versions of node |
| 2 | +# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions |
| 3 | + |
| 4 | +name: CI |
| 5 | + |
| 6 | +on: |
| 7 | + push: |
| 8 | + branches: |
| 9 | + - main |
| 10 | + pull_request: |
| 11 | + branches: |
| 12 | + - main |
| 13 | + workflow_dispatch: |
| 14 | + |
| 15 | +jobs: |
| 16 | + build: |
| 17 | + runs-on: ubuntu-latest |
| 18 | + strategy: |
| 19 | + matrix: |
| 20 | + node-version: [20, 22] |
| 21 | + |
| 22 | + steps: |
| 23 | + - uses: actions/checkout@v6 |
| 24 | + - name: Use Node.js ${{ matrix.node-version }} |
| 25 | + uses: actions/setup-node@v6 |
| 26 | + with: |
| 27 | + node-version: ${{ matrix.node-version }} |
| 28 | + - run: npm ci |
| 29 | + - run: npm run lint --if-present |
| 30 | + - run: npm test |
| 31 | + - run: npm run build --if-present |
| 32 | + - name: Save build |
| 33 | + if: matrix.node-version == 20 |
| 34 | + uses: actions/upload-artifact@v6 |
| 35 | + with: |
| 36 | + name: build |
| 37 | + path: | |
| 38 | + . |
| 39 | + !node_modules |
| 40 | + retention-days: 1 |
| 41 | + |
| 42 | + dependabot: |
| 43 | + name: 'Dependabot' |
| 44 | + needs: build # After the E2E and build jobs, if one of them fails, it won't merge the PR. |
| 45 | + runs-on: ubuntu-latest |
| 46 | + if: ${{ github.actor == 'dependabot[bot]' && github.event_name == 'pull_request'}} # Detect that the PR author is dependabot |
| 47 | + permissions: |
| 48 | + contents: write |
| 49 | + pull-requests: write |
| 50 | + steps: |
| 51 | + - name: Enable auto-merge for Dependabot PRs |
| 52 | + run: gh pr merge --auto --merge "$PR_URL" # Use Github CLI to merge automatically the PR |
| 53 | + env: |
| 54 | + PR_URL: ${{github.event.pull_request.html_url}} |
| 55 | + GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}} |
| 56 | + |
| 57 | + npm-publish-build: |
| 58 | + needs: build |
| 59 | + runs-on: ubuntu-latest |
| 60 | + permissions: |
| 61 | + id-token: write |
| 62 | + contents: read |
| 63 | + steps: |
| 64 | + - uses: actions/download-artifact@v7 |
| 65 | + with: |
| 66 | + name: build |
| 67 | + - uses: actions/setup-node@v6 |
| 68 | + with: |
| 69 | + node-version: 20 |
| 70 | + registry-url: 'https://registry.npmjs.org' |
| 71 | + - name: Update npm to latest (required for OIDC) |
| 72 | + run: npm install -g npm@latest |
| 73 | + - uses: rlespinasse/github-slug-action@v3.x |
| 74 | + - name: Append commit hash to package version |
| 75 | + run: 'sed -i -E "s/(\"version\": *\"[^\"]+)/\1-${GITHUB_SHA_SHORT}/" package.json' |
| 76 | + - name: Disable pre- and post-publish actions |
| 77 | + run: 'sed -i -E "s/\"((pre|post)publish)/\"ignore:\1/" package.json' |
| 78 | + - name: Publish to npm |
| 79 | + if: github.actor != 'dependabot[bot]' && github.actor != 'dependabot-preview[bot]' |
| 80 | + run: npm publish --tag ${{ env.GITHUB_REF_SLUG }} --ignore-scripts |
| 81 | + |
| 82 | + npm-publish-latest: |
| 83 | + needs: [build, npm-publish-build] |
| 84 | + runs-on: ubuntu-latest |
| 85 | + if: github.ref == 'refs/heads/main' |
| 86 | + permissions: |
| 87 | + id-token: write # Required for OIDC |
| 88 | + contents: read |
| 89 | + steps: |
| 90 | + - uses: actions/download-artifact@v7 |
| 91 | + with: |
| 92 | + name: build |
| 93 | + - uses: actions/setup-node@v6 |
| 94 | + with: |
| 95 | + node-version: 20 |
| 96 | + registry-url: 'https://registry.npmjs.org' |
| 97 | + - name: Update npm to latest (required for OIDC) |
| 98 | + run: npm install -g npm@latest |
| 99 | + - name: Disable pre- and post-publish actions |
| 100 | + run: 'sed -i -E "s/\"((pre|post)publish)/\"ignore:\1/" package.json' |
| 101 | + - name: Publish to npm |
| 102 | + if: github.actor != 'dependabot[bot]' && github.actor != 'dependabot-preview[bot]' |
| 103 | + run: npm publish --tag latest --ignore-scripts |
0 commit comments