Skip to content

自动部署

脚本

sh
#!/usr/bin/env sh

set -e

npm run docs:build

cd docs/.vitepress/dist

git init

git add -A

git commit -m '🎉deploy gh-pages🎉'

git push -f git@github.com:fxzer/zerdocs.git master:gh-pages

cd ..

rm -rf  dist

cd -

Action

主要流程

检查代码(Checkout) ===> 启动node环境(Setup Node) ===> 构建静态文件(Build) ===> 把静态文件push到gh-pages,触发gh-pages自带的workflow部署(Push To GitHub Pages)===> 把仓库同步到Gitee(Sync to Gitee) ===> 触发Gitee Pages服务部署(Build Gitee Pages)

yml
name: Deploy GitHub Pages And Sync to Gitee

# 触发条件:在 push 到 master 分支后
on:
  push:
    branches:
      - main
    paths-ignore: # 下列文件的变更不触发部署
      - LICENSE
# 任务
jobs:
  build-and-deploy:
    runs-on: ubuntu-latest # 部署运行环境搭建
    steps:
      - name: Checkout
        uses: actions/checkout@main

      - name: Set USERNAME and REPO_NAME
        run: |
          echo "USERNAME=${{ github.actor }}" >> $GITHUB_ENV
          echo "REPO_NAME=${{ github.repository }}" >> $GITHUB_ENV

      - name: Setup Node
        uses: actions/setup-node@main
        with:
          node-version: 'lts/*'

      - name: Install pnpm
        uses: pnpm/action-setup@v2
        with:
          version: 8
          run_install: true

      - name: Build 🔧
        run: |
          pnpm build

      - name: Push To GitHub Pages
        uses: ftnext/action-push-ghpages@v1.0.0
        with:
          build_dir: dist # 构建后的静态文件目录
          github_token: ${{ secrets.DEPLOY_TOKEN }}

      - name: Sync to Gitee
        uses: wearerequired/git-mirror-action@master
        env:
          # 注意在 Settings->Secrets 配置 GITEE_RSA_PRIVATE_KEY(复制本机生成的 ~/.ssh/gitee )
          SSH_PRIVATE_KEY: ${{ secrets.GITEE_RSA_PRIVATE_KEY }}
        with:
          # GitHub 源仓库地址
          source-repo: git@github.com:${{ env.REPO_NAME }}.git
          #  Gitee 目标仓库地址
          destination-repo: git@gitee.com:${{ env.REPO_NAME }}.git

      # 首次必须手动 Gitee 部署,否则会出错
      - name: Build Gitee Pages
        uses: yanglbme/gitee-pages-action@main
        with:
          # Gitee 用户名
          gitee-username: ${{ env.USERNAME }}
          # 在 Settings->Secrets 配置 GITEE_PSWD
          gitee-password: ${{ secrets.GITEE_PSWD }}
          # Gitee 仓库,仓库名严格区分大小写,请准确填写,否则会出错
          gitee-repo: ${{ env.REPO_NAME }}
          # 要部署的分支,默认是 master,若是其他分支,则需要指定(指定的分支必须存在)
          branch: gh-pages