-
Notifications
You must be signed in to change notification settings - Fork 15
177 lines (152 loc) · 6.73 KB
/
Copy pathdeploy-playground-to-cdn.yml
File metadata and controls
177 lines (152 loc) · 6.73 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
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
name: Deploy Playground to CDN
on:
workflow_dispatch:
inputs:
is_latest_release:
description: '当前分支是否是最新release版本'
required: true
default: true
type: boolean
env:
HUAWEI_CLOUD_AK: ${{ secrets.HUAWEI_CLOUD_AK }}
HUAWEI_CLOUD_SK: ${{ secrets.HUAWEI_CLOUD_SK }}
HUAWEI_CLOUD_ENDPOINT: ${{ secrets.HUAWEI_CLOUD_ENDPOINT }}
HUAWEI_CLOUD_BUCKET: ${{ secrets.HUAWEI_CLOUD_BUCKET }}
jobs:
check-secrets:
runs-on: ubuntu-latest
outputs:
secrets-ready: ${{ steps.check.outputs.secrets-ready }}
steps:
- name: Check required secrets
id: check
run: |
if [[ -z "${{ secrets.HUAWEI_CLOUD_AK }}" ]] || \
[[ -z "${{ secrets.HUAWEI_CLOUD_SK }}" ]] || \
[[ -z "${{ secrets.HUAWEI_CLOUD_ENDPOINT }}" ]] || \
[[ -z "${{ secrets.HUAWEI_CLOUD_BUCKET }}" ]]; then
echo "secrets-ready=false" >> $GITHUB_OUTPUT
echo "::error::Required Huawei Cloud secrets are not configured."
echo "::error::Please set: HUAWEI_CLOUD_AK, HUAWEI_CLOUD_SK, HUAWEI_CLOUD_ENDPOINT, HUAWEI_CLOUD_BUCKET"
exit 1
fi
echo "secrets-ready=true" >> $GITHUB_OUTPUT
echo "✅ All required secrets are configured"
build:
# 指定运行环境为最新版本的ubuntu
runs-on: ubuntu-latest
outputs:
version-timestamp: ${{ steps.prepare-version.outputs.version_timestamp }}
cdn-base: ${{ steps.prepare-version.outputs.cdn_base }}
cdn-base-latest: ${{ steps.prepare-version.outputs.cdn_base_latest }}
obs-path: ${{ steps.prepare-version.outputs.obs_path }}
obs-path-latest: ${{ steps.prepare-version.outputs.obs_path_latest }}
steps:
# 步骤1: 检出代码
- name: CheckOut Code
uses: actions/checkout@v4
with:
ref: ${{ github.ref_name }}
# 步骤2: 设置pnpm包管理器
- name: Setup pnpm
uses: pnpm/action-setup@v4
# 步骤3: 设置Node.js环境
- name: Setup Node
uses: actions/setup-node@v4
with:
node-version: 20 # 使用Node.js 20版本
registry-url: 'https://registry.npmjs.org' # 设置npm registry地址
# 步骤4: 获取pnpm缓存目录路径
- name: Get pnpm store directory
id: pnpm-cache
run: |
echo "pnpm_cache_dir=$(pnpm store path)" >> $GITHUB_OUTPUT
# 步骤5: 配置pnpm缓存
- uses: actions/cache@v3
name: Setup pnpm cache
with:
path: ${{ steps.pnpm-cache.outputs.pnpm_cache_dir }}
# 使用操作系统类型和pnpm-lock.yaml的哈希值作为缓存键
key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }}
restore-keys: |
${{ runner.os }}-pnpm-store-
# 步骤6: 安装项目依赖
- name: Install dependencies
run: pnpm i --no-frozen-lockfile
- id: prepare-version
name: Prepare version-timestamp
run: |
# Extract version from package.json
VERSION=$(node -p "require('./packages/playground/package.json').version")
# Generate timestamp in YYYYMMDD-HHMMSS format
TIMESTAMP=$(TZ="Asia/Shanghai" date +%Y%m%d-%H%M%S)
# Combine for version-timestamp
VERSION_TIMESTAMP="${VERSION}-${TIMESTAMP}"
# Set CDN base path
CDN_BASE="https://res-static.opentiny.design/tiny-robot-playground/${VERSION_TIMESTAMP}/"
CDN_BASE_LATEST="https://res-static.opentiny.design/tiny-robot-playground/latest/"
OBS_PATH="tiny-robot-playground/${VERSION_TIMESTAMP}"
OBS_PATH_LATEST="tiny-robot-playground/latest"
# Export as environment variables for subsequent steps
echo "VERSION_TIMESTAMP=$VERSION_TIMESTAMP" >> $GITHUB_ENV
echo "CDN_BASE=$CDN_BASE" >> $GITHUB_ENV
echo "OBS_PATH=$OBS_PATH" >> $GITHUB_ENV
# Set outputs for job-level export
echo "version_timestamp=$VERSION_TIMESTAMP" >> $GITHUB_OUTPUT
echo "cdn_base=$CDN_BASE" >> $GITHUB_OUTPUT
echo "cdn_base_latest=$CDN_BASE_LATEST" >> $GITHUB_OUTPUT
echo "obs_path=$OBS_PATH" >> $GITHUB_OUTPUT
echo "obs_path_latest=$OBS_PATH_LATEST" >> $GITHUB_OUTPUT
echo "Version-Timestamp: $VERSION_TIMESTAMP"
echo "CDN Base: $CDN_BASE"
echo "OBS Path: $OBS_PATH"
# 构建Playground
- name: Build Playground
run: pnpm build:playground
env:
PLAYGROUND_BASE: ${{ env.CDN_BASE }}
- name: Verify build output
run: ls -la ./packages/playground/dist
- name: Upload build artifacts
uses: actions/upload-artifact@v4
with:
name: playground-dist
path: ./packages/playground/dist/
retention-days: 1
deploy-cdn:
runs-on: ubuntu-latest
needs: [check-secrets, build]
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Download build artifacts
uses: actions/download-artifact@v4
with:
name: playground-dist
path: ./packages/playground/dist/
- name: Install obsutil
run: |
curl -o obsutil.tar.gz https://obs-community.obs.cn-north-1.myhuaweicloud.com/obsutil/current/obsutil_linux_amd64.tar.gz
tar -xzf obsutil.tar.gz
chmod +x obsutil_linux_amd64_*/obsutil
sudo mv obsutil_linux_amd64_*/obsutil /usr/local/bin/obsutil
- name: Configure and Upload to OBS
run: |
obsutil config -i=${{ env.HUAWEI_CLOUD_AK }} \
-k=${{ env.HUAWEI_CLOUD_SK }} \
-e=${{ env.HUAWEI_CLOUD_ENDPOINT }}
# Upload to versioned path
obsutil cp ./packages/playground/dist \
obs://${{ env.HUAWEI_CLOUD_BUCKET }}/${{ needs.build.outputs.obs-path }} \
-r -f -flat
# If is_latest_release is true, also upload to latest path
if [ "${{ github.event.inputs.is_latest_release }}" = "true" ]; then
# use cdn-base-latest replace cdn-base in all ./packages/playground/dist files
find ./packages/playground/dist -type f \( -name "*.html" -o -name "*.js" -o -name "*.mjs" -o -name "*.css" \) \
-exec sed -i "s|${{ needs.build.outputs.cdn-base }}|${{ needs.build.outputs.cdn-base-latest }}|g" {} +
obsutil cp ./packages/playground/dist \
obs://${{ env.HUAWEI_CLOUD_BUCKET }}/${{ needs.build.outputs.obs-path-latest }} \
-r -f -flat
fi
echo "Uploaded to: obs://${{ env.HUAWEI_CLOUD_BUCKET }}/${{ needs.build.outputs.obs-path }}"
echo "CDN URL: https://res-static.opentiny.design/${{ needs.build.outputs.obs-path }}"