-
-
Notifications
You must be signed in to change notification settings - Fork 0
167 lines (164 loc) · 6.56 KB
/
many--publish-npm-package.yaml
File metadata and controls
167 lines (164 loc) · 6.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
name: Publish package to registries
# Publishes the newest version of the package to both NPM and GitHub packages.
# If the workflow is triggered because of a tag push it will push that tag,
# but if the trigger is a regular commit push, then it will push an unstable
# tag.
on:
push:
tags: [v*.*.*]
branches: [main, master, release, development]
paths:
- .github/workflows/many--publish-npm-package.yaml
- test/**
- lib/**
- src/**
- browser/**
- '*package*'
- '*tsconfig*'
- '*vitest*'
- '*eslint*'
- '*prettier*'
jobs:
build:
name: Build new package version
runs-on: ubuntu-latest
outputs:
is_release: ${{ steps.version.outputs.is_release }}
is_prerelease: ${{ steps.version.outputs.is_prerelease }}
npm_tag: ${{ steps.version.outputs.npm_tag }}
package_version: ${{ steps.package.outputs.version }}
package_name: ${{ steps.package.outputs.name }}
artifact_name: ${{ steps.artifact-name.outputs.name }}
steps:
- name: Checkout repository
uses: actions/checkout@v6.0.1
- name: Set up Node.js
uses: actions/setup-node@v6.1.0
with:
node-version: 24
cache-dependency-path: package-lock.json
cache: 'npm'
- name: Read `package.json` file and output contents
id: package
run: |
version=$(node --print "require('./package.json').version")
echo "version=$version" >> $GITHUB_OUTPUT
echo "PACKAGE_VERSION=$version" >> $GITHUB_ENV
name=$(node --print "require('./package.json').name")
echo "name=$name" >> $GITHUB_OUTPUT
echo "PACKAGE_NAME=$name" >> $GITHUB_ENV
- name: Determine version and tag
id: version
run: |
if [[ "${GITHUB_REF}" == refs/tags/v*.*.*-* ]]; then
# prerelease for example: v1.2.3-alpha
echo "npm_tag=${GITHUB_REF##*-}" >> $GITHUB_OUTPUT
echo "is_release=false" >> $GITHUB_OUTPUT
echo "is_prerelease=true" >> $GITHUB_OUTPUT
elif [[ "${GITHUB_REF}" == refs/tags/v*.*.* ]]; then
# release for example: v1.2.3
echo "npm_tag=latest" >> $GITHUB_OUTPUT
echo "is_release=true" >> $GITHUB_OUTPUT
echo "is_prerelease=false" >> $GITHUB_OUTPUT
else
# regular commit
echo "npm_tag=unstable" >> $GITHUB_OUTPUT
echo "is_release=false" >> $GITHUB_OUTPUT
echo "is_prerelease=false" >> $GITHUB_OUTPUT
fi
- name: Verify tag matches package.json
if: steps.version.outputs.is_release == 'true'
run: |
TAG_VERSION="${GITHUB_REF#refs/tags/v}"
if [ "$PACKAGE_VERSION" != "$TAG_VERSION" ]; then
echo -n "Error: package.json version ($PACKAGE_VERSION)"
echo " does not match tag ($TAG_VERSION)"
exit 1
fi
- name: Install all dependencies
run: npm ci
- name: Build the project
run: npm run build
- name: Ensure we are not pushing a broken version
run: npm run test
- name: Turn links into permalinks on the README
run: |
file="README.md"
domain="github.com"
repo="${{ github.repository }}"
commit="${{ github.sha }}"
link="https://$domain/$repo/blob/$commit"
sed --in-place "s#\(\[.*\]\)(\./\([^)]*\))#\1($link/\2)#g" "$file"
sed --in-place "s#\(src\|srcset\)=\"\./#\1=\"$link/#g" "$file"
- name: Remove sections from readme.
run: |
sed --in-place '/<badges-container/,/<\/badges-container>/d' README.md
sed --in-place '/<toc-section/,/<\/toc-section>/d' README.md
sed --in-place '/<contributing-section/,/<\/contributing-section>/d' README.md
- name: Remove test files from final package
run: rm dist/vitest.* --recursive dist/test --force
- name: artifact name
id: artifact-name
run: echo "name=${{ github.sha }}-${{ github.ref_name }}" >> "$GITHUB_OUTPUT"
- name: Upload build artifact
uses: actions/upload-artifact@v6.0.0
with:
name: ${{ steps.artifact-name.outputs.name }}
if-no-files-found: 'error'
retention-days: 1
include-hidden-files: true
path: |
dist
package.json
package-lock.json
README.md
LICENSE
publish:
name: Publish to ${{ matrix.registry_name }}
runs-on: ubuntu-latest
environment: ${{ matrix.environment }}
needs: build
strategy:
matrix:
include:
- registry_name: NPM
registry_url: https://registry.npmjs.org
environment: ${{ github.ref_type == 'tag' && 'production' || 'staging' }}-npm
- registry_name: GitHub
registry_url: https://npm.pkg.github.com
environment: ${{ github.ref_type == 'tag' && 'production' || 'staging' }}-github-npm
fail-fast: true
permissions:
id-token: write
contents: read
packages: write
env:
PACKAGE_NAME: ${{ needs.build.outputs.package_name }}
PACKAGE_VERSION: ${{ needs.build.outputs.package_version }}
steps:
- name: Set up Node.js
uses: actions/setup-node@v6.1.0
with:
node-version: 24
registry-url: ${{ matrix.registry_url }}
- name: Download build artifact
uses: actions/download-artifact@v7.0.0
with: { name: "${{ needs.build.outputs.artifact_name }}" }
- name: Configure npm auth
if: ${{ matrix.registry_name == 'GitHub' }}
env: { NODE_AUTH_TOKEN: "${{ secrets.GITHUB_TOKEN }}" }
run: |
echo "//npm.pkg.github.com/:_authToken=$NODE_AUTH_TOKEN" > ~/.npmrc
echo "NODE_AUTH_TOKEN=$NODE_AUTH_TOKEN" >> "$GITHUB_ENV"
- name: Publish package
run: |
if [ "${{ needs.build.outputs.is_release }}" = "true" ]; then
npm publish --registry ${{ matrix.registry_url }} --tag ${{ needs.build.outputs.npm_tag }}
elif [ "${{ needs.build.outputs.is_prerelease }}" = "true" ]; then
npm version prerelease --preid "${{ needs.build.outputs.npm_tag }}" --no-git-tag-version
npm publish --registry ${{ matrix.registry_url }} --tag ${{ needs.build.outputs.npm_tag }}
else
SHORT_SHA=${GITHUB_SHA:0:7}
npm version prerelease --preid "${{ needs.build.outputs.npm_tag }}-$SHORT_SHA" --no-git-tag-version
npm publish --registry ${{ matrix.registry_url }} --tag ${{ needs.build.outputs.npm_tag }}
fi