|
| 1 | +--- |
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 && 'Build ➤ Test ➤ Publish to NPM' || |
| 8 | + inputs.release && !inputs.test && 'Build ➤ Skip Tests ➤ Publish to NPM' || |
| 9 | + github.event_name == 'workflow_dispatch' && inputs.test && 'Build ➤ Test' || |
| 10 | + github.event_name == 'workflow_dispatch' && !inputs.test && 'Build ➤ Skip Tests' || |
| 11 | + '' |
| 12 | + }} |
| 13 | +
|
2 | 14 | on: |
3 | | - # Build on pushes to release branches |
4 | | - push: |
5 | | - branches: [main] |
6 | | - # Build on pull requests targeting release branches |
7 | 15 | pull_request: |
| 16 | + push: |
8 | 17 | branches: [main] |
9 | | - # https://docs.github.com/en/actions/managing-workflow-runs/manually-running-a-workflow |
10 | | - # https://github.com/sanity-io/semantic-release-preset/actions/workflows/ci.yml |
11 | 18 | workflow_dispatch: |
12 | 19 | inputs: |
| 20 | + test: |
| 21 | + description: 'Run tests' |
| 22 | + required: true |
| 23 | + default: true |
| 24 | + type: boolean |
13 | 25 | release: |
14 | | - description: 'Release new version' |
| 26 | + description: 'Publish new release' |
15 | 27 | required: true |
16 | 28 | default: false |
17 | 29 | type: boolean |
18 | 30 |
|
| 31 | +concurrency: |
| 32 | + group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }} |
| 33 | + cancel-in-progress: true |
| 34 | + |
19 | 35 | jobs: |
| 36 | + build: |
| 37 | + runs-on: ubuntu-latest |
| 38 | + steps: |
| 39 | + - uses: actions/checkout@93ea575cb5d8a053eaa0ac8fa3b40d7e05a33cc8 # tag=v3 |
| 40 | + - uses: actions/setup-node@8c91899e586c5b171469028077307d293428b516 # tag=v3 |
| 41 | + with: |
| 42 | + node-version: lts/* |
| 43 | + cache: npm |
| 44 | + - run: npm ci |
| 45 | + - run: npm run prepublishOnly |
| 46 | + |
20 | 47 | test: |
21 | | - runs-on: ${{ matrix.platform }} |
22 | | - name: Node.js ${{ matrix.node-version }} / ${{ matrix.platform }} |
| 48 | + needs: build |
| 49 | + if: github.event.inputs.test != 'false' |
| 50 | + runs-on: ${{ matrix.os }} |
23 | 51 | strategy: |
24 | 52 | fail-fast: false |
25 | 53 | matrix: |
26 | | - platform: [ubuntu-latest] |
27 | | - # Test the oldest Node.js version we support, the latest LTS release and the current version in development |
28 | | - node-version: [12, lts/*, current] |
| 54 | + os: [macos-latest, ubuntu-latest, windows-latest] |
| 55 | + node: [lts/*] |
29 | 56 | include: |
30 | | - # mac and windows only need to test the LTS version of node |
31 | | - - platform: macos-latest |
32 | | - node-version: lts/* |
33 | | - - platform: windows-latest |
34 | | - node-version: lts/* |
| 57 | + - os: ubuntu-latest |
| 58 | + node: lts/-2 |
| 59 | + - os: ubuntu-latest |
| 60 | + node: current |
35 | 61 | steps: |
36 | 62 | - name: Set git to use LF |
| 63 | + if: matrix.os == 'windows-latest' |
37 | 64 | run: | |
38 | 65 | git config --global core.autocrlf false |
39 | 66 | git config --global core.eol lf |
40 | | - - uses: actions/checkout@2541b1294d2704b0964813337f33b291d3f8596b # tag=v3 |
41 | | - - uses: actions/setup-node@969bd2663942d722d85b6a8626225850c2f7be4b # tag=v3 |
| 67 | + - uses: actions/checkout@93ea575cb5d8a053eaa0ac8fa3b40d7e05a33cc8 # tag=v3 |
| 68 | + - uses: actions/setup-node@8c91899e586c5b171469028077307d293428b516 # tag=v3 |
42 | 69 | with: |
43 | | - node-version: ${{ matrix.node-version }} |
44 | | - - run: npm install |
45 | | - - run: npm run build |
| 70 | + node-version: ${{ matrix.node }} |
| 71 | + cache: npm |
| 72 | + - run: npm i |
46 | 73 | - run: npm test |
47 | 74 |
|
48 | 75 | release: |
49 | | - name: 'Semantic release' |
50 | | - needs: test |
| 76 | + needs: [build, test] |
| 77 | + # only run if opt-in during workflow_dispatch |
| 78 | + if: always() && github.event.inputs.release == 'true' && needs.build.result != 'failure' && needs.test.result != 'failure' && needs.test.result != 'cancelled' |
51 | 79 | runs-on: ubuntu-latest |
52 | | - if: inputs.release == true |
53 | 80 | steps: |
54 | | - - uses: actions/checkout@2541b1294d2704b0964813337f33b291d3f8596b # tag=v3 |
| 81 | + - uses: actions/checkout@93ea575cb5d8a053eaa0ac8fa3b40d7e05a33cc8 # tag=v3 |
55 | 82 | with: |
56 | 83 | # Need to fetch entire commit history to |
57 | 84 | # analyze every commit since last release |
58 | 85 | fetch-depth: 0 |
59 | | - - uses: actions/setup-node@969bd2663942d722d85b6a8626225850c2f7be4b # tag=v3 |
| 86 | + - uses: actions/setup-node@8c91899e586c5b171469028077307d293428b516 # tag=v3 |
60 | 87 | with: |
61 | 88 | node-version: lts/* |
62 | 89 | cache: npm |
63 | 90 | - run: npm ci |
64 | | - # Branches that will release new versions are defined in .releaserc.json |
| 91 | + # Branches that will release new versions are defined in .releaserc.json |
65 | 92 | - run: npx semantic-release |
| 93 | + # Don't allow interrupting the release step if the job is cancelled, as it can lead to an inconsistent state |
| 94 | + # e.g. git tags were pushed but it exited before `npm publish` |
| 95 | + if: always() |
| 96 | + env: |
| 97 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 98 | + NPM_TOKEN: ${{ secrets.NPM_PUBLISH_TOKEN }} |
| 99 | + # Re-run semantic release with rich logs if it failed to publish for easier debugging |
| 100 | + - run: npx semantic-release --dry-run --debug |
| 101 | + if: failure() |
66 | 102 | env: |
67 | 103 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
68 | 104 | NPM_TOKEN: ${{ secrets.NPM_PUBLISH_TOKEN }} |
0 commit comments