-
Notifications
You must be signed in to change notification settings - Fork 19
134 lines (120 loc) · 4.82 KB
/
release.yml
File metadata and controls
134 lines (120 loc) · 4.82 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
name: Release
on:
pull_request:
branches:
- master
- main
types:
- closed
workflow_dispatch:
inputs:
version:
description: 'Version to publish (e.g., 1.2.3)'
required: false
type: string
jobs:
release:
if: |
(github.event_name == 'pull_request' &&
github.event.pull_request.merged == true &&
contains(github.event.pull_request.labels.*.name, 'Type: Release')) ||
github.event_name == 'workflow_dispatch'
runs-on: ubuntu-latest
permissions:
contents: write
id-token: write # OIDC
pull-requests: write # PR comment
steps:
- name: Checkout
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
with:
fetch-depth: 0
- name: Get package info
id: package
run: |
if [ "${{ github.event_name }}" = "workflow_dispatch" ] && [ -n "${{ github.event.inputs.version }}" ]; then
VERSION="${{ github.event.inputs.version }}"
else
VERSION=$(node -p "require('./package.json').version")
fi
PACKAGE_NAME=$(node -p "require('./package.json').name")
echo "version=$VERSION" >> $GITHUB_OUTPUT
echo "name=$PACKAGE_NAME" >> $GITHUB_OUTPUT
- name: Check if tag exists
id: tag-check
run: |
if git rev-parse "v${{ steps.package.outputs.version }}" >/dev/null 2>&1; then
echo "exists=true" >> $GITHUB_OUTPUT
else
echo "exists=false" >> $GITHUB_OUTPUT
fi
- name: Setup Node.js
if: steps.tag-check.outputs.exists == 'false'
uses: actions/setup-node@3235b876344d2a9aa001b8d1453c930bba69e610 # v3.9.1
with:
node-version: 22
registry-url: 'https://registry.npmjs.org'
- name: Ensure npm 11.5.1 or later is installed
if: steps.tag-check.outputs.exists == 'false'
run: |
NPM_VERSION=$(npm -v)
echo "Current npm version: $NPM_VERSION"
if ! npx semver -r ">=11.5.1" "$NPM_VERSION"; then
echo "npm version $NPM_VERSION is too old. Installing latest npm..."
npm install -g npm@latest
echo "Updated npm version: $(npm -v)"
fi
- name: Install dependencies
if: steps.tag-check.outputs.exists == 'false'
run: npm ci
- name: Build package
if: steps.tag-check.outputs.exists == 'false'
run: npm run build
- name: Publish to npm with provenance
if: steps.tag-check.outputs.exists == 'false'
run: npm publish --provenance --access public
- name: Create GitHub Release with tag
id: create-release
if: steps.tag-check.outputs.exists == 'false'
run: |
if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then
RELEASE_URL=$(gh release create "v${{ steps.package.outputs.version }}" \
--title "v${{ steps.package.outputs.version }}" \
--target "${{ github.sha }}" \
--generate-notes)
else
RELEASE_URL=$(gh release create "v${{ steps.package.outputs.version }}" \
--title "v${{ steps.package.outputs.version }}" \
--target "${{ github.sha }}" \
--notes "${{ github.event.pull_request.body }}")
fi
echo "url=$RELEASE_URL" >> $GITHUB_OUTPUT
env:
GH_TOKEN: ${{ github.token }}
- name: Comment on PR - Success
if: |
always() &&
github.event_name == 'pull_request' &&
steps.tag-check.outputs.exists == 'false' &&
success()
run: |
gh pr comment ${{ github.event.pull_request.number }} \
--body "✅ **Release v${{ steps.package.outputs.version }} completed successfully!**
- 📦 npm package: https://www.npmjs.com/package/${{ steps.package.outputs.name }}/v/${{ steps.package.outputs.version }}
- 🏷️ GitHub Release: ${{ steps.create-release.outputs.url }}
- 🔗 Workflow run: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}"
env:
GH_TOKEN: ${{ github.token }}
- name: Comment on PR - Failure
if: |
always() &&
github.event_name == 'pull_request' &&
steps.tag-check.outputs.exists == 'false' &&
failure()
run: |
gh pr comment ${{ github.event.pull_request.number }} \
--body "❌ **Release v${{ steps.package.outputs.version }} failed**
Please check the workflow logs for details.
🔗 Workflow run: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}"
env:
GH_TOKEN: ${{ github.token }}