Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 24 additions & 25 deletions .github/workflows/deploy-static.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ jobs:
runs-on: ubuntu-latest

env:
DEPLOY_ENV: "CDN"
CDN_BASEURL: "https://cdn.jsdelivr.net/gh/scarletborder/bloggerv2@static/"
API_ENDPOINT: "https://blog.scarletborder.cn"

steps:
Expand All @@ -45,12 +43,26 @@ jobs:
run: pnpm install --frozen-lockfile

- name: Build project
run: pnpm run build
run: DEPLOY_ENV='CDN' pnpm run build

- name: Deploy to static branch and get commit hash
id: deploy
uses: peaceiris/actions-gh-pages@v3
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_dir: ./dist
publish_branch: static
force_orphan: true
user_name: "github-actions[bot]"
user_email: "github-actions[bot]@users.noreply.github.com"
commit_message: "Deploy static assets from release branch"

- name: Find built assets and generate report
id: generate-report
run: |
CDN_BASE="${{ env.CDN_BASEURL }}"
COMMIT_HASH="${{ steps.deploy.outputs.commit_hash }}"
CDN_BASE="https://cdn.jsdelivr.net/gh/${{ github.repository }}@${COMMIT_HASH}/"
echo "Generated CDN Base URL: ${CDN_BASE}"
REPORT_FILE="build_report.txt"

# 清空报告文件,以便重新写入
Expand All @@ -74,6 +86,14 @@ jobs:
echo "<script crossorigin='anonymous' src='${JS_CDN_URL}' type='module'></script>" >> "$REPORT_FILE"
done

echo "--- Injecting global __CDN_URL__ variable ---"
# 使用 cat <<EOF 来安全地写入多行脚本
cat <<EOF >> "$REPORT_FILE"
<script>
window.__CDN_URL__ = '${CDN_BASE}';
</script>
EOF

echo "--- Build Report Generated ---"
cat "$REPORT_FILE"

Expand All @@ -98,24 +118,3 @@ jobs:
else
echo "branch_exists=false" >> $GITHUB_OUTPUT
fi

- name: Create static branch if not exists
if: steps.check-branch.outputs.branch_exists == 'false'
run: |
git config --local user.email "github-actions[bot]@users.noreply.github.com"
git config --local user.name "github-actions[bot]"
git checkout --orphan static
git reset --hard
git commit --allow-empty -m "Initial commit for static branch"
git push origin static

- name: Deploy to static branch
uses: peaceiris/actions-gh-pages@v3
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_dir: ./dist
publish_branch: static
force_orphan: true
user_name: "github-actions[bot]"
user_email: "github-actions[bot]@users.noreply.github.com"
commit_message: "Deploy static assets from release branch"
1 change: 1 addition & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import tencentEslintConfig from 'eslint-config-tencent/flat';

const GlobalConstants = {
__API_ENDPOINT__: 'readonly',
__CDN_URL__: 'readonly',
};

export default tseslint.config([
Expand Down
5 changes: 1 addition & 4 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,7 @@
color: #212529 !important;
}
</style>
<script>
window.__static_url =
'https://cdn.jsdelivr.net/gh/scarletborder/bloggerv2@static';
</script>

<script>
(function () {
const { search, hash, pathname } = window.location;
Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions src/components/home/Blogroll.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ export default function Blogroll({ isMobile = false }: BlogrollProps): JSX.Eleme

React.useEffect(() => {
let isMounted = true;
const staticUrl = (window as any).__static_url;
fetch(`${staticUrl}/blogroll.json`)
const staticUrl = __CDN_URL__;
fetch(`${staticUrl}/static/blogroll.json`)
.then(res => res.json())
.then((data) => {
if (!isMounted) return;
Expand Down
3 changes: 3 additions & 0 deletions src/vite-env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@
// API 请求的基础URL
// 一般是博客的根URL,例如 'https://blog.scarletborder.cn'。
declare const __API_ENDPOINT__: string;
// cdn base url
// 使用了hash作为唯一地址
declare const __CDN_URL__: string;
4 changes: 0 additions & 4 deletions template.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,6 @@
/*]]>*/
</style>

<script>
window.__static_url = 'https://cdn.jsdelivr.net/gh/scarletborder/bloggerv2@static';
</script>

<script>
/*<![CDATA[*/
(function () {
Expand Down