Skip to content

Commit cb14781

Browse files
committed
ci: init
1 parent 9fa61b9 commit cb14781

File tree

5 files changed

+192
-0
lines changed

5 files changed

+192
-0
lines changed

.github/workflows/ci.yml

+109
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
branches:
6+
- 'main'
7+
tags:
8+
- 'v*.*.*'
9+
pull_request:
10+
branches:
11+
- 'main'
12+
13+
env:
14+
NODE_VERSION: '21'
15+
16+
jobs:
17+
deps:
18+
timeout-minutes: 30
19+
runs-on: ubuntu-22.04
20+
steps:
21+
- name: Checkout
22+
uses: actions/checkout@v4
23+
with:
24+
fetch-depth: 1
25+
persist-credentials: false
26+
- name: Setup Pnpm
27+
uses: pnpm/action-setup@v4
28+
with:
29+
version: 9.3.0
30+
- name: Setup Node
31+
uses: actions/setup-node@v4
32+
with:
33+
node-version: '${{ env.NODE_VERSION }}'
34+
cache: 'pnpm'
35+
- name: Deps
36+
run: scripts/deps
37+
env:
38+
LOCK: true
39+
- name: Archive Modules
40+
timeout-minutes: 5
41+
uses: actions/cache/save@v4
42+
with:
43+
key: node-modules-${{ runner.os }}-${{ hashFiles('pnpm-lock.yaml') }}
44+
path: |
45+
${{ github.workspace }}/node_modules
46+
47+
build:
48+
needs:
49+
- deps
50+
if: github.event_name == 'pull_request'
51+
timeout-minutes: 30
52+
runs-on: ubuntu-22.04
53+
steps:
54+
- name: Checkout
55+
uses: actions/checkout@v4
56+
with:
57+
fetch-depth: 1
58+
persist-credentials: false
59+
- name: Unarchive Node Modules
60+
timeout-minutes: 5
61+
uses: actions/cache/restore@v4
62+
with:
63+
key: node-modules-${{ runner.os }}-${{ hashFiles('pnpm-lock.yaml') }}
64+
path: |
65+
${{ github.workspace }}/node_modules
66+
- name: Build
67+
run: scripts/build
68+
69+
build-publish:
70+
needs:
71+
- deps
72+
if: github.event_name == 'push'
73+
timeout-minutes: 30
74+
runs-on: ubuntu-22.04
75+
strategy:
76+
matrix:
77+
version: ['latest', '${{ github.ref_name }}']
78+
exclude:
79+
- version: 'main'
80+
steps:
81+
- name: Checkout
82+
uses: actions/checkout@v4
83+
with:
84+
fetch-depth: 1
85+
persist-credentials: false
86+
- name: Unarchive Node Modules
87+
timeout-minutes: 5
88+
uses: actions/cache/restore@v4
89+
with:
90+
key: node-modules-${{ runner.os }}-${{ hashFiles('pnpm-lock.yaml') }}
91+
path: |
92+
${{ github.workspace }}/node_modules
93+
- name: Build
94+
run: scripts/build
95+
- name: Package
96+
run: scripts/package
97+
env:
98+
VERSION: '${{ matrix.version }}'
99+
- name: Release
100+
uses: TencentCloud/cos-action@b0aa648235fb35a1bdd6a77f529eb0ac4c2f1c25
101+
with:
102+
secret_id: ${{ secrets.CI_TECENTCOS_SECRET_ID }}
103+
secret_key: ${{ secrets.CI_TECENTCOS_SECRET_KEY }}
104+
cos_bucket: ${{ secrets.COS_BUCKET }}
105+
cos_region: ${{ secrets.COS_REGION }}
106+
local_path: dist/${{ matrix.version }}.tar.gz
107+
remote_path: releases/${{ matrix.version }}.tar.gz
108+
accelerate: true
109+
clean: false

scripts/build

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/bash
2+
set -ex
3+
4+
npm run build ${BASE_ARGS:-}

scripts/deps

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/bin/bash
2+
set -ex
3+
4+
if [[ -n "${LOCK}" ]]; then
5+
BASE_ARGS="--frozen-lockfile"
6+
fi
7+
8+
pnpm install ${BASE_ARGS:-}

scripts/package

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/bin/bash
2+
set -ex
3+
4+
source scripts/version
5+
6+
version::get_version_vars
7+
8+
TARBALL=${GIT_VERSION}.tar.gz
9+
echo "Compressing to ${TARBALL}..."
10+
tar -czf ${TARBALL} dist
11+
mv ${TARBALL} dist

scripts/version

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#!/bin/bash
2+
3+
function version::get_version_vars() {
4+
BUILD_DATE=$(date -u '+%Y-%m-%dT%H:%M:%SZ')
5+
GIT_TREE_STATE="unknown"
6+
GIT_COMMIT="unknown"
7+
GIT_VERSION="unknown"
8+
9+
# get the git tree state if the source was exported through git archive.
10+
# shellcheck disable=SC2016,SC2050
11+
if [[ '$Format:%%$' == "%" ]]; then
12+
GIT_TREE_STATE="archive"
13+
GIT_COMMIT='$Format:%H$'
14+
# when a 'git archive' is exported, the '$Format:%D$' below will look
15+
# something like 'HEAD -> release-1.8, tag: v1.8.3' where then 'tag: '
16+
# can be extracted from it.
17+
if [[ '$Format:%D$' =~ tag:\ (v[^ ,]+) ]]; then
18+
GIT_VERSION="${BASH_REMATCH[1]}"
19+
else
20+
GIT_VERSION="${GIT_COMMIT:0:7}"
21+
fi
22+
# respect specified version.
23+
GIT_VERSION="${VERSION:-${GIT_VERSION}}"
24+
return
25+
fi
26+
27+
# return directly if not found git client.
28+
if [[ -z "$(command -v git)" ]]; then
29+
# respect specified version.
30+
GIT_VERSION=${VERSION:-${GIT_VERSION}}
31+
return
32+
fi
33+
34+
# find out git info via git client.
35+
if GIT_COMMIT=$(git rev-parse "HEAD^{commit}" 2>/dev/null); then
36+
# specify as dirty if the tree is not clean.
37+
if git_status=$(git status --porcelain 2>/dev/null) && [[ -n ${git_status} ]]; then
38+
GIT_TREE_STATE="dirty"
39+
else
40+
GIT_TREE_STATE="clean"
41+
fi
42+
43+
# specify with the tag if the head is tagged.
44+
if GIT_VERSION="$(git rev-parse --abbrev-ref HEAD 2>/dev/null)"; then
45+
if git_tag=$(git tag -l --contains HEAD 2>/dev/null | head -n 1 2>/dev/null) && [[ -n ${git_tag} ]]; then
46+
GIT_VERSION="${git_tag}"
47+
fi
48+
fi
49+
50+
# specify to dev if the tree is dirty.
51+
if [[ "${GIT_TREE_STATE:-dirty}" == "dirty" ]]; then
52+
GIT_VERSION="dev"
53+
elif ! [[ "${GIT_VERSION}" =~ ^v([0-9]+)\.([0-9]+)(\.[0-9]+)?(-[0-9A-Za-z.-]+)?(\+[0-9A-Za-z.-]+)?$ ]]; then
54+
GIT_VERSION="dev"
55+
fi
56+
57+
# respect specified version
58+
GIT_VERSION=${VERSION:-${GIT_VERSION}}
59+
fi
60+
}

0 commit comments

Comments
 (0)