-
Notifications
You must be signed in to change notification settings - Fork 0
188 lines (159 loc) · 5.88 KB
/
cd_release.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
182
183
184
185
186
187
188
name: Release Packages
on:
release:
types: [released]
env:
NODE_VERSION: "20.16.0"
PHP_VERSION: "8.2.22"
permissions:
contents: write
pull-requests: write
defaults:
run:
working-directory: ./krait-ui
concurrency:
group: release
cancel-in-progress: false
jobs:
bump_version:
name: Build UI
runs-on: "ubuntu-22.04"
outputs:
version_branch: ${{ steps.get-version-details.outputs.major_version}}.x
steps:
- name: Get Github token
id: get-github-token
uses: peter-murray/workflow-application-token-action@v3
with:
application_id: ${{ vars.GH_MTR_PACKAGE_VERSIONS_APP_ID }}
application_private_key: ${{ secrets.GH_MTR_PACKAGE_VERSIONS_APP_SECRET }}
- name: Get release version details
id: get-version-details
run: |
# Extract major version (handles v1.2.3-beta.0 or 1.2.3-beta.0)
MAJOR_VERSION=$(echo "${{ github.event.release.tag_name }}" | sed 's/^v\{0,1\}\([0-9]\+\)[^0-9].*/\1/')
echo "major_version=$MAJOR_VERSION" >> $GITHUB_OUTPUT
- name: Checkout the repository
uses: actions/checkout@v4
with:
ref: ${{ steps.get-version-details.outputs.major_version}}.x
token: ${{ steps.get-github-token.outputs.token }}
- name: Verify the release title
env:
RELEASE_TITLE: ${{ github.event.release.name }}
run: |
if [[ -z "$RELEASE_TITLE" ]]; then
echo "Release title cannot be empty"
exit 1
fi
TITLE_VERSION_REGEX="^v[0-9]+\.[0-9]+\.[0-9]+$"
if [[ ! $RELEASE_TITLE =~ $TITLE_VERSION_REGEX ]]; then
echo "Invalid release title format: $RELEASE_TITLE"
exit 1
else
echo "Release title $RELEASE_TITLE is valid"
fi
- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: ${{ env.NODE_VERSION }}
- name: Update package.json version
env:
VERSION: ${{ github.event.release.tag_name }}
run: |
npm version $VERSION
- name: Commit the version bump update
env:
VERSION: ${{ github.event.release.tag_name }}
GITHUB_TOKEN: ${{ steps.get_workflow_token.outputs.token }}
run: |
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
git config user.name "github-actions[bot]"
git add .
git commit -am "[GHA] Update package version to $VERSION"
git push origin HEAD:$CLEAN_VERSION
release:
name: Release Package
runs-on: ubuntu-22.04
needs: [bump_version]
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
ref: ${{ needs.bump_version.outputs.version_branch }}
- name: Download the production build
id: download-production-build
uses: actions/github-script@v6
with:
script: |
const fs = require('fs').promises;
const artifact_name = 'distribution-package.zip';
const download_path = 'krait-ui/distribution-package.zip';
// Get the release assets
const release = await github.rest.repos.getReleaseByTag({
owner: context.repo.owner,
repo: context.repo.repo,
tag: context.payload.release.tag_name
});
// Find the specific asset
const asset = release.data.assets.find(asset => asset.name === artifact_name);
if (!asset) throw new Error(`Asset ${artifact_name} not found in release`);
// Download the asset
const response = await github.rest.repos.getReleaseAsset({
owner: context.repo.owner,
repo: context.repo.repo,
asset_id: asset.id,
headers: {
Accept: 'application/octet-stream'
}
});
// Save the file
await fs.writeFile(download_path, Buffer.from(response.data));
console.log(`Downloaded ${artifact_name} to ${download_path}`);
return asset.id
- name: Update zip with new package.json
working-directory: krait-ui
run: |
zip -u distribution-package.zip package.json package-lock.json
- name: Delete existing release asset
uses: actions/github-script@v6
env:
ASSET_ID: ${{ steps.download-production-build.outputs.result }}
with:
script: |
const {ASSET_ID} = process.env
await github.rest.repos.deleteReleaseAsset({
owner: context.repo.owner,
repo: context.repo.repo,
asset_id: ASSET_ID
});
- name: Upload an Asset in GitHub Release
uses: actions/github-script@v6
env:
RELEASE_ID: ${{ github.event.release.id }}
with:
script: |
const {RELEASE_ID} = process.env
const fs = require('fs').promises;
await github.rest.repos.uploadReleaseAsset({
name: 'distribution-package.zip',
owner: context.repo.owner,
repo: context.repo.repo,
release_id: ${{ env.RELEASE_ID }},
data: await fs.readFile('./krait-ui/distribution-package.zip')
});
- name: Unzip the distribution build
working-directory: krait-ui
run: |
unzip -n distribution-package.zip
# - name: Publish the NPM package
# run: npm publish --access public
# env:
# NODE_AUTH_TOKEN: ${{ secrets.NPM_AUTH_TOKEN }}
- name: Release cleanup
id: update-release
uses: ./.github/actions/release-cleanup
with:
release_id: ${{ github.event.release.id }}
tag_name: ${{ needs.bump_version.outputs.release_version }}
node_auth_token: ${{ secrets.NPM_AUTH_TOKEN }}