Skip to content

vitepress 通过 GitHub Actions 部署到远程服务器

touch .github/workflows/main.yaml 创建GitHub actions 文件

需要进入设置,添加action的secrets,添加时不需要带secrets前缀,只有在main.yaml文件中使用时才需要前缀

yaml
# Sample workflow for building and deploying a VitePress site to GitHub Pages
#
name: Deploy VitePress site

on:
  # Runs on pushes targeting the `main` branch. Change this to `master` if you're
  # using the `master` branch as the default branch.
  push:
    branches: [main]

  # Allows you to run this workflow manually from the Actions tab
  workflow_dispatch:

# Allow only one concurrent deployment, skipping runs queued between the run in-progress and latest queued.
# However, do NOT cancel in-progress runs as we want to allow these production deployments to complete.
concurrency:
  group: pages
  cancel-in-progress: false

jobs:
  # Build job
  build:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v3
        with:
          fetch-depth: 0 # Not needed if lastUpdated is not enabled
      # - uses: pnpm/action-setup@v2 # Uncomment this if you're using pnpm
      # - uses: oven-sh/setup-bun@v1 # Uncomment this if you're using Bun
      - name: Setup Node
        uses: actions/setup-node@v3
        with:
          node-version: 18
          cache: npm # or pnpm / yarn
      - name: Install dependencies
        run: npm ci # or pnpm install / yarn install / bun install
      - name: Build with VitePress
        run: |
          npm run build
          touch .vitepress/dist/.nojekyll
      - name: SSH Command
        uses: D3rHase/[email protected]
        with:
          host: ${{secrets.HOST}} # Remote server address / ip - required
          user: ${{secrets.USERNAME}} # Remote server user - required
          private_key: ${{secrets.SSHKEY}} # Private ssh key registered on the remote server - required
          command: rm -rf /target/dir/*
      - name: SCP Uploader
        # You may pin to the exact commit or the version.
        uses: moonpathbg/scp_uploader@latest
        with:
          # remote host
          host: ${{ secrets.HOST }} 
          # remote user
          username: ${{ secrets.USERNAME }}
          # remote port
          # port: # optional, default is 22
          # ssh private key (~/.ssh/id_rsa)
          key: ${{ secrets.SSHKEY }}
          # source files path
          source: .vitepress/dist
          # destination path
          target: /target/dir