Repository navigation
160 lines (150 loc) · 6.25 KB
/
Copy pathpublish.yml
File metadata and controls
160 lines (150 loc) · 6.25 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
name: Publish to npm
on:
push:
branches:
- main
paths:
- 'package.json'
workflow_dispatch:
inputs:
mode:
description: How this run should exercise npm publishing
required: true
type: choice
options:
- dry-run
- publish
default: dry-run
npm_tag:
description: Dist-tag to use for manual runs
required: true
type: string
default: next
jobs:
publish:
runs-on: ubuntu-latest
permissions:
contents: write
id-token: write
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7
with:
# Full history + tags: release notes are generated from the commits
# between the previous tag and this one
fetch-depth: 0
- uses: pnpm/action-setup@73e46992e05b19abd31f2f3c20e0e7370d762115 # v6
- uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7
with:
node-version: 24.x
registry-url: https://registry.npmjs.org
cache: pnpm
env:
FORCE_COLOR: 0
- name: Resolve publish mode
id: publish_mode
run: |
if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then
echo "mode=${{ inputs.mode }}" >> "$GITHUB_OUTPUT"
echo "tag=${{ inputs.npm_tag }}" >> "$GITHUB_OUTPUT"
echo "manual=true" >> "$GITHUB_OUTPUT"
else
echo "mode=publish" >> "$GITHUB_OUTPUT"
echo "tag=latest" >> "$GITHUB_OUTPUT"
echo "manual=false" >> "$GITHUB_OUTPUT"
fi
- name: Resolve release commit
id: release_commit
env:
HEAD_COMMIT_MESSAGE: ${{ github.event.head_commit.message }}
run: |
if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then
echo "is_release_commit=true" >> "$GITHUB_OUTPUT"
exit 0
fi
if ! git rev-parse HEAD^ >/dev/null 2>&1; then
echo "Unable to inspect the previous commit for release detection"
exit 1
fi
if ! git diff --unified=0 HEAD^ HEAD -- package.json | grep -qE '^[+-][[:space:]]+"version":'; then
echo "is_release_commit=false" >> "$GITHUB_OUTPUT"
exit 0
fi
if [[ ! "$HEAD_COMMIT_MESSAGE" =~ ^chore\(release\):[[:space:]]+(.+)$ ]]; then
echo "package.json version changed, but HEAD is not a release commit"
exit 1
fi
echo "is_release_commit=true" >> "$GITHUB_OUTPUT"
echo "version=${BASH_REMATCH[1]}" >> "$GITHUB_OUTPUT"
- name: Check if version is already published
id: check
run: |
LOCAL_VERSION=$(node -p "require('./package.json').version")
echo "version=$LOCAL_VERSION" >> "$GITHUB_OUTPUT"
if [ "${{ steps.publish_mode.outputs.manual }}" != "true" ] && [ "${{ steps.release_commit.outputs.is_release_commit }}" != "true" ]; then
echo "should_publish=false" >> "$GITHUB_OUTPUT"
exit 0
fi
RELEASE_VERSION="${{ steps.release_commit.outputs.version }}"
if [ -n "$RELEASE_VERSION" ] && [ "$LOCAL_VERSION" != "$RELEASE_VERSION" ]; then
echo "Release commit version $RELEASE_VERSION does not match package.json version $LOCAL_VERSION"
exit 1
fi
NPM_VERSION=$(npm view ghost-cli version 2>/dev/null || echo "0.0.0")
if [ "${{ steps.publish_mode.outputs.manual }}" = "true" ] || [ "$LOCAL_VERSION" != "$NPM_VERSION" ]; then
echo "should_publish=true" >> "$GITHUB_OUTPUT"
else
echo "should_publish=false" >> "$GITHUB_OUTPUT"
fi
- name: Upgrade npm for trusted publishing support
run: npm install -g npm@latest
- name: Print runtime versions
run: |
node --version
npm --version
- name: Install dependencies
if: steps.check.outputs.should_publish == 'true'
run: pnpm install --frozen-lockfile
- name: Preview publish tarball
if: steps.check.outputs.should_publish == 'true'
run: npm pack --dry-run
- name: Publish to npm
if: steps.check.outputs.should_publish == 'true'
run: |
PUBLISH_ARGS=(--access public --tag "${{ steps.publish_mode.outputs.tag }}" --provenance)
if [ "${{ steps.publish_mode.outputs.mode }}" = "dry-run" ]; then
PUBLISH_ARGS=(--dry-run "${PUBLISH_ARGS[@]}")
fi
npm publish "${PUBLISH_ARGS[@]}"
- name: Create git tag
if: github.event_name == 'push' && steps.release_commit.outputs.is_release_commit == 'true' && steps.check.outputs.should_publish == 'true'
run: |
TAG="v${{ steps.check.outputs.version }}"
if git ls-remote --tags origin "$TAG" | grep -q "$TAG"; then
echo "Tag $TAG already exists on remote, skipping"
else
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
git tag -a "$TAG" -m "$TAG"
git push origin "$TAG"
fi
- name: Create GitHub release
if: github.event_name == 'push' && steps.release_commit.outputs.is_release_commit == 'true' && steps.check.outputs.should_publish == 'true'
env:
GH_TOKEN: ${{ github.token }}
run: |
TAG="v${{ steps.check.outputs.version }}"
if gh release view "$TAG" >/dev/null 2>&1; then
echo "Release $TAG already exists, skipping"
exit 0
fi
# The tag immediately before this one
PREV_TAG=$(git tag --list 'v[0-9]*' --sort=-version:refname | grep -v -- '-' | grep -v "^${TAG}$" | head -n 1)
if [ -n "$PREV_TAG" ]; then
echo "Previous tag: $PREV_TAG"
node scripts/release-notes.js "$PREV_TAG" "$TAG" > /tmp/release-notes.md
else
echo "::warning::No previous tag found — using the fallback release notes"
echo "This release contains fixes for minor bugs and issues reported by Ghost users." > /tmp/release-notes.md
fi
cat /tmp/release-notes.md
gh release create "$TAG" --title "$TAG" --notes-file /tmp/release-notes.md