Skip to content

Commit 9cfab18

Browse files
committed
chore: better CI setup
1 parent 8554d22 commit 9cfab18

File tree

1 file changed

+64
-28
lines changed

1 file changed

+64
-28
lines changed

.github/workflows/main.yml

Lines changed: 64 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,104 @@
1+
---
12
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+
214
on:
3-
# Build on pushes to release branches
4-
push:
5-
branches: [main]
6-
# Build on pull requests targeting release branches
715
pull_request:
16+
push:
817
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
1118
workflow_dispatch:
1219
inputs:
20+
test:
21+
description: 'Run tests'
22+
required: true
23+
default: true
24+
type: boolean
1325
release:
14-
description: 'Release new version'
26+
description: 'Publish new release'
1527
required: true
1628
default: false
1729
type: boolean
1830

31+
concurrency:
32+
group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
33+
cancel-in-progress: true
34+
1935
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+
2047
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 }}
2351
strategy:
2452
fail-fast: false
2553
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/*]
2956
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
3561
steps:
3662
- name: Set git to use LF
63+
if: matrix.os == 'windows-latest'
3764
run: |
3865
git config --global core.autocrlf false
3966
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
4269
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
4673
- run: npm test
4774

4875
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'
5179
runs-on: ubuntu-latest
52-
if: inputs.release == true
5380
steps:
54-
- uses: actions/checkout@2541b1294d2704b0964813337f33b291d3f8596b # tag=v3
81+
- uses: actions/checkout@93ea575cb5d8a053eaa0ac8fa3b40d7e05a33cc8 # tag=v3
5582
with:
5683
# Need to fetch entire commit history to
5784
# analyze every commit since last release
5885
fetch-depth: 0
59-
- uses: actions/setup-node@969bd2663942d722d85b6a8626225850c2f7be4b # tag=v3
86+
- uses: actions/setup-node@8c91899e586c5b171469028077307d293428b516 # tag=v3
6087
with:
6188
node-version: lts/*
6289
cache: npm
6390
- 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
6592
- 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()
66102
env:
67103
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
68104
NPM_TOKEN: ${{ secrets.NPM_PUBLISH_TOKEN }}

0 commit comments

Comments
 (0)