-
Notifications
You must be signed in to change notification settings - Fork 752
181 lines (162 loc) · 6.25 KB
/
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
name: publish
on:
# When Release Pull Request is merged
push:
branches:
- 'chore/**'
- 'feat/**'
pull_request:
branches:
- next
types: [closed]
env:
CI: true
jobs:
publish:
permissions:
contents: write # to create tags and refs
actions: write # to cancel running workflow (andymckay/cancel-action)
issues: write # to create comment
name: publish
runs-on: ubuntu-latest
steps:
# Setup
- name: Checkout
uses: actions/checkout@v2
- name: Setup Node ${{ matrix.node_version }}
uses: actions/setup-node@v1
with:
node-version: 16
registry-url: 'https://registry.npmjs.org'
- 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 }}
# Log meta
- name : GITHUB CONTEXT
env:
GITHUB_CONTEXT: ${{ toJson(github) }}
run: echo "$GITHUB_CONTEXT"
# Get commit message
- name: Get commit message
run: |
COMMIT_MESSAGE=$(git log --format=%B -n 1)
echo "commitmsg=${COMMIT_MESSAGE}" >> $GITHUB_ENV
- name: Show commit message
run : echo "$commitmsg"
- name: Commit message compliance verification
if: startsWith( env.COMMIT_MESSAGE , 'chore(release):' ) != true
uses: andymckay/cancel-action@0.3
- name: Publish push tag verification
if: github.event_name == 'push' && contains( env.COMMIT_MESSAGE , '--tag=' ) != true
uses: andymckay/cancel-action@0.3
# Get & check npm publish
- name: Get publish params
if: github.event_name == 'push'
run: |
PUBLISH_PARAMS=`echo $COMMIT_MESSAGE | grep -oE "^chore\(release\): publish \S*\s(.*)" | cut -d " " -f 4-`
echo "PUBLISH_PARAMS=${PUBLISH_PARAMS}" >> $GITHUB_ENV
- name: Show publish params
if: github.event_name == 'push'
run: echo "Publish pushing provide parameter [$PUBLISH_PARAMS]."
# Define ${CURRENT_VERSION}
- name: Set Current Version
if: startsWith( env.commitmsg , 'chore(release):' )
shell: bash -ex {0}
run: |
CURRENT_VERSION=$(node -p 'require("./packages/taro-ui/package.json").version')
echo "CURRENT_VERSION=${CURRENT_VERSION}" >> $GITHUB_ENV
# Check git tag
- name: Tag Check
if: startsWith( env.commitmsg , 'chore(release):' )
id: tag_check
shell: bash -ex {0}
run: |
GET_API_URL="https://api.github.com/repos/${GITHUB_REPOSITORY}/git/ref/tags/v${CURRENT_VERSION}"
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 "::set-output name=exists_tag::true"
else
echo "::set-output name=exists_tag::false"
fi
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# ------------------ If git tag already exists, skip -------------
- name: Git tag verification
if: steps.tag_check.outputs.exists_tag == false
uses: andymckay/cancel-action@0.3
# Bootstrap project
- name: Cache pnpm modules
uses: actions/cache@v3
with:
path: ~/.pnpm-store
key: ${{ runner.os }}-${{ hashFiles('**/pnpm-lock.yaml') }}
restore-keys: |
${{ runner.os }}-
- name: install
uses: pnpm/action-setup@v2.2.4
with:
version: 8
run_install: |
- recursive: true
args: [--frozen-lockfile]
- name: build
run: pnpm build
# Git stash
- name: Drop current changes
if: startsWith( env.commitmsg , 'chore(release):' ) && steps.tag_check.outputs.exists_tag == 'false'
run: |
git add .
git stash
# Create git tag
- name: Create Git Tag
if: startsWith( env.commitmsg , 'chore(release):' ) && steps.tag_check.outputs.exists_tag == 'false'
uses: azu/action-package-version-to-git-tag@v1
with:
version: ${{ env.CURRENT_VERSION }}
github_token: ${{ secrets.GITHUB_TOKEN }}
github_repo: ${{ github.repository }}
git_commit_sha: ${{ github.sha }}
git_tag_prefix: "v"
# Publish
- name: Publish
run: |
if [ '${{ github.event_name }}' == 'pull_request' ] ; then
pnpm publish --registry=https://registry.npmjs.org/ --publish-branch=${{ github.ref }} -r
elif [ '${{ github.event_name }}' == 'push' ] ; then
pnpm publish --registry=https://registry.npmjs.org/ --publish-branch=${{ github.ref_name }} -r ${{ env.PUBLISH_PARAMS }}
fi
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
# Create relase when event is PR
- name: Create Release
id: create_release
if: startsWith( env.commitmsg , 'chore(release):' ) && steps.tag_check.outputs.exists_tag == 'false' && github.event.pull_request.merged == true
uses: actions/create-release@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: v${{ env.CURRENT_VERSION }}
# Copy Pull Request's tile and body to Release Note
release_name: ${{ github.event.pull_request.title }}
body: |
${{ github.event.pull_request.body }}
draft: false
prerelease: false
# Create PR page comment when event is PR
- uses: actions/github-script@v6
if: github.event.pull_request.merged == true
with:
github-token: ${{secrets.GITHUB_TOKEN}}
script: |
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: 'https://github.com/${{ github.repository }}/releases/tag/v${{ env.CURRENT_VERSION }} is released 🎉'
})