-
Notifications
You must be signed in to change notification settings - Fork 9
149 lines (132 loc) · 5.02 KB
/
create-release-draft-and-publish.yml
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
name: Create Release Draft and Publish
on:
push:
branches:
- main
- 'releases/**'
jobs:
create-release-draft-and-publish:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Git identity
run: |
git config --global user.name 'github-actions[bot]'
git config --global user.email 'github-actions[bot]@users.noreply.github.com'
git remote set-url origin https://x-access-token:${GITHUB_TOKEN}@github.com/$GITHUB_REPOSITORY
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# ------------------最新提交不是tag,取消当前工作流--------------------
- name: Check if latest commit is tagged
id: check_latest_tag
shell: bash -ex {0}
run: |
git fetch --tags
# 获取最新一次提交的hash
LATEST_COMMIT=$(git rev-parse HEAD)
# 检查最新一次tag是否在最新提交信息中
TAG=$(git tag --points-at $LATEST_COMMIT)
if [ -z "$TAG" ]; then
echo "正常中断: No tag found for the latest commit. Cancelling the workflow."
exit 1
else
echo "Latest commit is tagged as: $TAG"
fi
# ----------------------从最新一次提交信息中获取tag标签信息---------------------
- name: Extract tag version
shell: bash -ex {0}
run: |
git fetch --tags
COMMIT_TAG=$(git describe --tags --abbrev=0)
echo "COMMIT_TAG is $COMMIT_TAG"
echo "github.ref_name(current branch) is ${{ github.ref_name }}"
if [ -z "$COMMIT_TAG" ]; then
echo "No valid version tag found."
exit 1
fi
echo "COMMIT_TAG=${COMMIT_TAG}" >> $GITHUB_ENV
# ------------------检查tag在github中是否存在,不存在,取消当前工作流--------------------
- name: Check tag
id: check_tag
shell: bash -ex {0}
run: |
GET_API_URL="https://api.github.com/repos/${GITHUB_REPOSITORY}/git/ref/tags/${COMMIT_TAG}"
http_status_code=$(curl -LI $GET_API_URL -o /dev/null -w '%{http_code}\n' -s \
-H "Authorization: token ${GITHUB_TOKEN}")
if [ "$http_status_code" -ne "404" ] ; then
echo "this tag already exists. Continue..."
else
echo "正常中断: this tag does not exist. Canceling the workflow."
exit 1
fi
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Install Node.js
uses: actions/setup-node@v4
with:
node-version: '18'
registry-url: 'https://registry.npmjs.org/'
# ------------------安装依赖-----------------
- name: Install pnpm
uses: pnpm/action-setup@v4
with:
version: '9'
- name: Install dependencies
run: pnpm install
# ------------------构建-----------------
- name: Build
run: pnpm build
# ----------------------生成发布日志草稿---------------------
- name: Create compressed packages
run: |
cd packages
for dir in */; do
cd "$dir"
pnpm pack
cd ..
done
- name: Determine release type
id: release-type
run: |
if [[ "${{ env.COMMIT_TAG }}" =~ ^v[0-9]+\.[0-9]+\.[0-9]+-beta\.[0-9]+$ ]]; then
echo "RELEASE=pre-release" >> $GITHUB_ENV
echo "......pre-release......"
elif [[ "${{ env.COMMIT_TAG }}" =~ ^v[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
echo "RELEASE=latest" >> $GITHUB_ENV
echo "......latest......"
else
echo "Error: Invalid tag, only support like vX.X.X-beta.X or vX.X.X, plase check your tag."
exit 1
fi
- name: Upload release assets
uses: softprops/action-gh-release@v2
with:
tag: ${{ env.COMMIT_TAG }}
files: |
packages/*/*.tgz
name: ${{ github.event.repository.name }} ${{ env.COMMIT_TAG }}
draft: true
prerelease: ${{ env.RELEASE == 'pre-release' }}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# ----------------------检查git状态,将上一步生成未被git追踪的.tgz文件暂存起来---------------------
- name: Check git status
run: git status
- name: Stash untracked changes
run: |
git add .
git stash
# ----------------------发布到npm---------------------
- name: Publish to NPM
run: |
if [ '${{ env.RELEASE }}' == 'pre-release' ] ; then
pnpm -r publish --tag beta --publish-branch=${{ github.ref_name }}
elif [ '${{ env.RELEASE }}' == 'latest' ] ; then
pnpm -r publish --publish-branch=${{ github.ref_name }}
fi
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}