forked from minorcell/memo-code
-
Notifications
You must be signed in to change notification settings - Fork 0
121 lines (104 loc) · 4.7 KB
/
Copy pathrelease.yml
File metadata and controls
121 lines (104 loc) · 4.7 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
name: Release
on:
push:
branches: [main]
paths:
- 'package.json'
workflow_dispatch:
permissions:
contents: write
jobs:
tag:
name: Create tag from version
runs-on: ubuntu-latest
outputs:
version_changed: ${{ steps.version.outputs.changed }}
version: ${{ steps.version.outputs.current }}
tag_name: ${{ format('v{0}', steps.version.outputs.current) }}
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Detect package version change
id: version
run: |
CURRENT_VERSION=$(node -p "require('./package.json').version")
BEFORE_SHA="${{ github.event.before }}"
if [ "$BEFORE_SHA" = "0000000000000000000000000000000000000000" ]; then
PREVIOUS_VERSION=""
else
PREVIOUS_VERSION=$(git show "$BEFORE_SHA:package.json" 2>/dev/null | node -e "const fs = require('fs'); const input = fs.readFileSync(0, 'utf8'); try { process.stdout.write(String(JSON.parse(input).version ?? '')) } catch { process.stdout.write('') }")
fi
if [ "$CURRENT_VERSION" = "$PREVIOUS_VERSION" ]; then
VERSION_CHANGED=false
else
VERSION_CHANGED=true
fi
echo "current=$CURRENT_VERSION" >> "$GITHUB_OUTPUT"
echo "previous=$PREVIOUS_VERSION" >> "$GITHUB_OUTPUT"
echo "changed=$VERSION_CHANGED" >> "$GITHUB_OUTPUT"
- name: Check tag existence
if: steps.version.outputs.changed == 'true'
id: tag
run: |
TAG_NAME="v${{ steps.version.outputs.current }}"
if git rev-parse "$TAG_NAME" >/dev/null 2>&1; then
TAG_EXISTS=true
elif git ls-remote --exit-code --tags origin "refs/tags/$TAG_NAME" >/dev/null 2>&1; then
TAG_EXISTS=true
else
TAG_EXISTS=false
fi
echo "name=$TAG_NAME" >> "$GITHUB_OUTPUT"
echo "exists=$TAG_EXISTS" >> "$GITHUB_OUTPUT"
- name: Create and push tag
if: steps.version.outputs.changed == 'true' && steps.tag.outputs.exists != 'true'
run: |
TAG_NAME="${{ steps.tag.outputs.name }}"
git config user.name "github-actions[bot]"
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
git tag "$TAG_NAME" "${GITHUB_SHA}"
git push origin "$TAG_NAME"
- name: Skip when version is unchanged
if: steps.version.outputs.changed != 'true'
run: echo "package.json version unchanged; skip tag creation."
- name: Skip when tag already exists
if: steps.version.outputs.changed == 'true' && steps.tag.outputs.exists == 'true'
run: echo "Tag ${{ steps.tag.outputs.name }} already exists; skip tag creation."
publish:
name: Publish to npm
if: needs.tag.outputs.version_changed == 'true'
needs: tag
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- uses: actions/checkout@v4
- uses: pnpm/action-setup@v2
with:
version: 9
- uses: actions/setup-node@v4
with:
node-version: '20'
cache: 'pnpm'
registry-url: 'https://registry.npmjs.org/'
- name: Install ripgrep
run: sudo apt-get update && sudo apt-get install -y ripgrep
- run: pnpm install
- run: pnpm run build
- name: Check if npm version exists
id: npm
run: |
PACKAGE_NAME=$(node -p "require('./package.json').name")
PACKAGE_VERSION="${{ needs.tag.outputs.version }}"
if npm view "${PACKAGE_NAME}@${PACKAGE_VERSION}" version >/dev/null 2>&1; then
echo "already_published=true" >> "$GITHUB_OUTPUT"
echo "Version ${PACKAGE_NAME}@${PACKAGE_VERSION} already exists on npm; skip publish."
else
echo "already_published=false" >> "$GITHUB_OUTPUT"
fi
- name: Publish to npm
if: steps.npm.outputs.already_published != 'true'
run: pnpm publish --no-git-checks
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}