自动化部署
简洁说明
警告
请注意,在参考此篇文章时,需要注意用户名以及文件路径是否正确,并且与自己的信息是否匹配哈。
我们使用Github Actions来自动化部署VitePress网站到其他位置(如Github,Netlify etc)。若有需求不公开在线文档源代码,故此设置两个仓库,否则,直接参考官方文档:部署到GitHub Pages,不用拆仓库。
提示
- Github Actions
- 两个仓库: 一个公开 + 一个私有
仓库简短说明
警告
网站源代码上传到www.mardan.wiki.source
,然后通过Github Actions自动化编译后提交到ka1i.github.io
。
- [公开]
ka1i.github.io
Github Pages默认仓库,ka1i为我的Github用户名。 - [私有]
www.mardan.wiki.source
这个一般随便.但是为了方便,我设置为网站域名+.source
后缀。
生成ssh密钥
警告
生成密钥时,请注意不要覆盖到原有密钥了。担心的可以先备份一下。
bash
ssh-keygen -C "github-actions[bot]@users.noreply.github.com"
详细信息
bash
Generating public/private ed25519 key pair.
Enter file in which to save the key (/Users/mardan/.ssh/id_ed25519):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /Users/mardan/.ssh/id_ed25519
Your public key has been saved in /Users/mardan/.ssh/id_ed25519.pub
The key fingerprint is:
SHA256:PYovsyNBExEMW3lW7ZILK3GsWbDPY9ZpMLptHXUADrA github-actions[bot]@users.noreply.github.com
The key's randomart image is:
+--[ED25519 256]--+
| .o==..oo |
| o=.oo o |
| . EB .o . |
| = B o.o . |
| . @ *S=o. |
| * B.*. . |
| B.+.. |
| o *.. |
| o.=. |
+----[SHA256]-----+
部署ssh密钥
提示
- 私钥 id_ed25519
- 公钥 id_ed25519.pub
分别将内容粘贴到指定位置,参考下图:
信息
- 私钥部署
信息
- 公钥部署
Github Actions工作流程
编写自动化部署工作流程: .github/workflows/vitepress-deploy-ci.yaml
vitepress-deploy-ci.yaml
yaml
name: vitepress deploy
on:
push:
branches: [master]
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Install pnpm
uses: pnpm/action-setup@v4
with:
version: 9
run_install: false
- uses: actions/setup-node@v4
with:
node-version: 20
cache: pnpm
- name: install dependence
run: pnpm install
- name: Build with VitePress
run: pnpm build
- name: Upload artifact
uses: actions/upload-artifact@v4
with:
name: github_pages
path: .vitepress/dist
deploy:
needs: build
runs-on: ubuntu-latest
name: Deploy
steps:
- name: Set up workspace
run: |
mkdir github_pages_artifact
mkdir github_pages_source_home
- name: Download artifact
uses: actions/download-artifact@v4
with:
name: github_pages
path: github_pages_artifact
- name: Setup git env
env:
SSH_PRIVATE: ${{ secrets.SSH_PRIVATE }}
GIT_NAME: ka1i
GIT_EMAIL: github-actions[bot]@users.noreply.github.com
run: |
mkdir -p ~/.ssh/
echo "$SSH_PRIVATE" | tr -d '\r' > ~/.ssh/id_rsa
chmod 600 ~/.ssh/id_rsa
ssh-keyscan github.com >> ~/.ssh/known_hosts
git config --global user.name "$GIT_NAME"
git config --global user.email "$GIT_EMAIL"
- name: Clone repository
run: |
git clone --depth="1" git@github.com:ka1i/ka1i.github.io.git github_pages_source_home
echo "Cloned ka1i.github.io repository successfully."
- name: Deploy to GitHub Pages Repository
env:
TZ: Asia/Shanghai
run: |
cp -r github_pages_source_home/.git github_pages_artifact
cd github_pages_artifact
git add .
git commit -m "$(date)"
git status
git push