|
| 1 | +--- |
| 2 | +name: CI & Release |
| 3 | + |
| 4 | +# Workflow name based on selected inputs. Fallback to default Github naming when expression evaluates to empty string |
| 5 | +run-name: >- |
| 6 | + ${{ |
| 7 | + inputs.release && inputs.test && format('Build {0} ➤ Test ➤ Publish to NPM', github.ref_name) || |
| 8 | + inputs.release && !inputs.test && format('Build {0} ➤ Skip Tests ➤ Publish to NPM', github.ref_name) || |
| 9 | + github.event_name == 'workflow_dispatch' && inputs.test && format('Build {0} ➤ Test', github.ref_name) || |
| 10 | + github.event_name == 'workflow_dispatch' && !inputs.test && format('Build {0} ➤ Skip Tests', github.ref_name) || |
| 11 | + '' |
| 12 | + }} |
| 13 | +
|
| 14 | +on: |
| 15 | + # Build on pushes branches that have a PR (including drafts) |
| 16 | + pull_request: |
| 17 | + # Build on commits pushed to branches without a PR if it's in the allowlist |
| 18 | + push: |
| 19 | + branches: [master, legacy-v2] |
| 20 | + # https://docs.github.com/en/actions/managing-workflow-runs/manually-running-a-workflow |
| 21 | + workflow_dispatch: |
| 22 | + inputs: |
| 23 | + test: |
| 24 | + description: Run tests |
| 25 | + required: true |
| 26 | + default: true |
| 27 | + type: boolean |
| 28 | + release: |
| 29 | + description: Release new version |
| 30 | + required: true |
| 31 | + default: false |
| 32 | + type: boolean |
| 33 | + |
| 34 | +concurrency: |
| 35 | + # On PRs builds will cancel if new pushes happen before the CI completes, as it defines `github.head_ref` and gives it the name of the branch the PR wants to merge into |
| 36 | + # Otherwise `github.run_id` ensures that you can quickly merge a queue of PRs without causing tests to auto cancel on any of the commits pushed to main. |
| 37 | + group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }} |
| 38 | + cancel-in-progress: true |
| 39 | + |
| 40 | +jobs: |
| 41 | + build: |
| 42 | + runs-on: ubuntu-latest |
| 43 | + name: Lint & Build |
| 44 | + steps: |
| 45 | + - uses: actions/checkout@v3 |
| 46 | + - uses: actions/setup-node@v3 |
| 47 | + with: |
| 48 | + cache: npm |
| 49 | + node-version: lts/* |
| 50 | + - run: npm ci |
| 51 | + # Linting can be skipped |
| 52 | + - run: npm run lint --if-present |
| 53 | + if: github.event.inputs.test != 'false' |
| 54 | + # But not the build script, as semantic-release will crash if this command fails so it makes sense to test it early |
| 55 | + - run: npm run prepublishOnly --if-present |
| 56 | + |
| 57 | + test: |
| 58 | + needs: build |
| 59 | + # The test matrix can be skipped, in case a new release needs to be fast-tracked and tests are already passing on main |
| 60 | + if: github.event.inputs.test != 'false' |
| 61 | + runs-on: ${{ matrix.os }} |
| 62 | + name: Node.js ${{ matrix.node }} / ${{ matrix.os }} |
| 63 | + strategy: |
| 64 | + # A test failing on windows doesn't mean it'll fail on macos. It's useful to let all tests run to its completion to get the full picture |
| 65 | + fail-fast: false |
| 66 | + matrix: |
| 67 | + # Run the testing suite on each major OS with the latest LTS release of Node.js |
| 68 | + os: [macos-latest, ubuntu-latest, windows-latest] |
| 69 | + node: [lts/*] |
| 70 | + # It makes sense to also test the oldest, and latest, versions of Node.js, on ubuntu-only since it's the fastest CI runner |
| 71 | + include: |
| 72 | + - os: ubuntu-latest |
| 73 | + # Test the oldest LTS release of Node that's still receiving bugfixes and security patches, versions older than that have reached End-of-Life |
| 74 | + node: lts/-2 |
| 75 | + - os: ubuntu-latest |
| 76 | + # Test the actively developed version that will become the latest LTS release next October |
| 77 | + node: current |
| 78 | + steps: |
| 79 | + # It's only necessary to do this for windows, as mac and ubuntu are sane OS's that already use LF |
| 80 | + - name: Set git to use LF |
| 81 | + if: matrix.os == 'windows-latest' |
| 82 | + run: | |
| 83 | + git config --global core.autocrlf false |
| 84 | + git config --global core.eol lf |
| 85 | + - uses: actions/checkout@v3 |
| 86 | + - uses: actions/setup-node@v3 |
| 87 | + with: |
| 88 | + cache: npm |
| 89 | + node-version: ${{ matrix.node }} |
| 90 | + - run: npm i |
| 91 | + - run: npm test --if-present |
| 92 | + |
| 93 | + release: |
| 94 | + needs: [build, test] |
| 95 | + # only run if opt-in during workflow_dispatch |
| 96 | + if: always() && github.event.inputs.release == 'true' && needs.build.result != 'failure' && needs.test.result != 'failure' && needs.test.result != 'cancelled' |
| 97 | + runs-on: ubuntu-latest |
| 98 | + name: Semantic release |
| 99 | + steps: |
| 100 | + - uses: actions/checkout@v3 |
| 101 | + with: |
| 102 | + # Need to fetch entire commit history to |
| 103 | + # analyze every commit since last release |
| 104 | + fetch-depth: 0 |
| 105 | + - uses: actions/setup-node@v3 |
| 106 | + with: |
| 107 | + cache: npm |
| 108 | + node-version: lts/* |
| 109 | + - run: npm ci |
| 110 | + # Branches that will release new versions are defined in .releaserc.json |
| 111 | + # @TODO remove --dry-run after verifying everything is good to go |
| 112 | + - run: npx semantic-release --dry-run |
| 113 | + # Don't allow interrupting the release step if the job is cancelled, as it can lead to an inconsistent state |
| 114 | + # e.g. git tags were pushed but it exited before `npm publish` |
| 115 | + if: always() |
| 116 | + env: |
| 117 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 118 | + NPM_TOKEN: ${{ secrets.NPM_PUBLISH_TOKEN }} |
| 119 | + # Re-run semantic release with rich logs if it failed to publish for easier debugging |
| 120 | + - run: npx semantic-release --dry-run --debug |
| 121 | + if: failure() |
| 122 | + env: |
| 123 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 124 | + NPM_TOKEN: ${{ secrets.NPM_PUBLISH_TOKEN }} |
0 commit comments