-
Notifications
You must be signed in to change notification settings - Fork 0
144 lines (123 loc) · 4.7 KB
/
npm-deploy.yml
File metadata and controls
144 lines (123 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
name: Publish Package
on:
push:
branches:
- 1.x
workflow_dispatch:
concurrency:
group: publish-package-${{ github.ref }}
cancel-in-progress: false
jobs:
verify_version:
runs-on: ubuntu-latest
outputs:
should_publish: ${{ steps.check.outputs.should_publish }}
version: ${{ steps.check.outputs.version }}
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Check if package.json version changed
id: check
run: |
echo "Current branch: ${{ github.ref }}"
# Get current package metadata
PACKAGE_NAME=$(jq -r .name package.json)
CURRENT_VERSION=$(jq -r .version package.json)
echo "Package: $PACKAGE_NAME"
echo "Current version: $CURRENT_VERSION"
# Get previous commit hash and determine whether package.json changed.
if git rev-parse HEAD~1 >/dev/null 2>&1; then
PREV_COMMIT=$(git rev-parse HEAD~1)
PACKAGE_JSON_CHANGED=$(
git diff --name-only HEAD~1 HEAD | grep -q "^package.json$" && echo "true" || echo "false"
)
else
PREV_COMMIT=$(git rev-parse HEAD)
PACKAGE_JSON_CHANGED=$(
git show --pretty='' --name-only HEAD | grep -q "^package.json$" && echo "true" || echo "false"
)
fi
if [[ "$PACKAGE_JSON_CHANGED" == "true" ]]; then
echo "Package.json was changed in this commit"
# Get previous version if possible
if git show "$PREV_COMMIT:package.json" >/dev/null 2>&1; then
PREV_VERSION=$(git show "$PREV_COMMIT:package.json" | jq -r .version)
echo "Previous version: $PREV_VERSION"
if [ "$CURRENT_VERSION" != "$PREV_VERSION" ]; then
echo "Version changed from $PREV_VERSION to $CURRENT_VERSION"
echo "should_publish=true" >> "$GITHUB_OUTPUT"
else
echo "Version unchanged"
echo "should_publish=false" >> "$GITHUB_OUTPUT"
fi
else
echo "First commit with package.json, will publish"
echo "should_publish=true" >> "$GITHUB_OUTPUT"
fi
else
echo "Package.json not changed in this commit"
echo "should_publish=false" >> "$GITHUB_OUTPUT"
fi
echo "version=$CURRENT_VERSION" >> "$GITHUB_OUTPUT"
publish:
needs: verify_version
if: needs.verify_version.outputs.should_publish == 'true'
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Create Git tag
run: |
TAG_NAME="v${{ needs.verify_version.outputs.version }}"
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
if git ls-remote --exit-code --tags origin "refs/tags/${TAG_NAME}" >/dev/null 2>&1; then
echo "Tag ${TAG_NAME} already exists on origin; skipping tag creation"
elif git rev-parse "${TAG_NAME}" >/dev/null 2>&1; then
echo "Tag ${TAG_NAME} already exists locally; skipping tag push"
else
git tag -a "${TAG_NAME}" -m "Release ${TAG_NAME}"
git push origin "${TAG_NAME}"
fi
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Setup Bun
uses: oven-sh/setup-bun@v2
- name: Install dependencies
run: bun install
- name: Build package
run: bun run build
- name: Publish to npm
run: |
VERSION="${{ needs.verify_version.outputs.version }}"
DIST_TAG="latest"
if [[ "$VERSION" =~ -([A-Za-z0-9]+) ]]; then
DIST_TAG="${BASH_REMATCH[1]}"
fi
echo "Publishing ${VERSION} with dist-tag ${DIST_TAG}"
bun publish --access public --tag "${DIST_TAG}" --tolerate-republish
env:
NPM_CONFIG_TOKEN: ${{ secrets.NPM_TOKEN }}
create_release:
needs: [verify_version, publish]
if: needs.verify_version.outputs.should_publish == 'true'
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- name: Create GitHub Release
uses: actions/create-release@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: "v${{ needs.verify_version.outputs.version }}"
release_name: "v${{ needs.verify_version.outputs.version }}"
body: "Release v${{ needs.verify_version.outputs.version }}"
draft: false
prerelease: false