Skip to content

GitHub Action 笔记

记录 GitHub Actions 使用笔记

在Github 工作流中访问远程服务器

1. 生成ssh密钥

ssh-keygen -t rsa -b 4096 -C "github-deploy-key" -N "" -f deploy-key

生成密钥文件名为 deploy_key (私钥) 和 deploy_key.pub (公钥)。

2. 将公钥添加到远程服务器的 authorized_keys 文件

  1. 将公钥 (deploy_key.pub 文件内容) 复制到你的剪贴板。

  2. 登录你的远程服务器。

  3. 将公钥添加到 ~/.ssh/authorized_keys 文件。 如果 .ssh 目录不存在,先创建它: mkdir -p ~/.ssh && chmod 700 ~/.ssh。 如果 authorized_keys 文件不存在,创建它: touch ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys

    bash
    echo "<your public key>" >> ~/.ssh/authorized_keys

3. 在 Github 仓库中添加私钥变量

  1. 将私钥 (deploy_key 文件内容) 复制到你的剪贴板。
  2. 在你的 Github 仓库中,进入 "Settings" -> "Security" -> "Secrets and variables" -> "Actions"。
  3. 点击 "New repository secret",创建一个新的 secret。
  4. Name: 设置一个有意义的名字,例如 DEPLOY_KEY在你的 workflow 文件中,将使用这个名字来引用私钥。
  5. Secret: 粘贴你复制的私钥。

4. 在 Workflow 中使用 SSH

在你的 .github/workflows 目录下的 YAML 文件中,使用 SSH 连接到远程服务器。这里是一个示例:

yaml
name: Deploy to Server
on:
  push:
    branches:
      - main
jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v3
      - name: Install SSH key
        uses: webfactory/[email protected]
        with:
          ssh-private-key: ${{ secrets.DEPLOY_KEY }}
      - name: Add known host
        run: |
          mkdir -p ~/.ssh
          ssh-keyscan -H ${{ secrets.SERVER_HOST }} >> ~/.ssh/known_hosts
      - name: Deploy
        run: |
          ssh ${{ secrets.SERVER_USERNAME }}@${{ secrets.SERVER_HOST }} "
            cd /app
            # 其他部署命令,例如重启服务
          "

Github Actions 示例: 自动部署 GitHub Pages 和 Gitee Pages示例

yaml
# .github/workflows/deploy-pages.yml 
name: Deploy Pages

on:
  # 推送时执行
  push:
    branches: [main, master]
  # pr 时执行
  pull_request:
    branches: [main, master]
  # 定时执行,字段含义:分钟 小时 日 月 星期
  # 注意:
  # 1.采用的是 UTC 时间
  #   即,你配置的 0 0 * * *(UTC)00:00 实际上是(GMT+0800)08:00
  #   只有你配置为 00 16 * * *(UTC)16:00 实际上才是 GMT+0800)00:00
  # 2.官方声明:schedule 事件在 GitHub Actions 工作流运行期间负载过高时可能会延迟。 高负载时间包括每小时的开始时间。 为了降低延迟的可能性,建议将您的工作流程安排在不同时间运行。
  #   例如:笔者之前设定的定时规则为 0 0 * * *(UTC),实际(GMT+0800)执行时间通常在 9:10 ~ 10:00,甚至会出现在 10:00 之后执行的情况
  # 3.建议不只是定时执行的时候注意时间,自己 push 时也注意时间,早点休息
  #   例如:笔者多次亲身经历,凌晨时间 pages build and deployment 任务大概能持续 8 分钟的 Current status: deployment_queued 状态,
  #        即使能进入 Current status: deployment_in_progress 和 Current status: syncing_files 状态,如果文件多点,没一会儿就 Error: Timeout reached, aborting! 超时失败了
  schedule:
    - cron: 30 17 * * *
  # 可手动执行
  workflow_dispatch:

jobs:
  # 任务1: 部署 GitHub Pages
  deploy-github-pages:
    runs-on: ubuntu-latest
    steps:
      # 1、检出源码
      - name: Checkout
        uses: actions/checkout@v3
        with:
          # 默认只拉取分支最近一次的 commit,可能会导致一些文章的 GitInfo 变量无法获取,设为 0 代表拉取所有分支所有提交
          fetch-depth: 0
      # 2、配置 Git
      # 主要是 quotePath,默认情况下,文件名包含中文时,git 会使用引号把文件名括起来,这会导致 action 中无法读取 :GitInfo 变量
      - name: Git Configuration
        run: |
          git config --global core.quotePath false
          git config --global core.autocrlf false
          git config --global core.safecrlf true
          git config --global core.ignorecase false  
      # 3、安装 PNPM
      - name: Setup PNPM
        uses: pnpm/action-setup@v2
        with:
          version: latest
      # 4、安装 Node 环境
      - name: Setup Node
        uses: actions/setup-node@v3
        with:
          node-version: 18
          registry-url: https://registry.npmjs.org
          cache: pnpm
      # 5、安装依赖
      - name: Install dependencies
        run: pnpm i --frozen-lockfile
      # 6、打包
      - name: Build
        run: pnpm build
      # 7、部署 GitHub Pages
      - name: Deploy GitHub Pages
        uses: JamesIves/github-pages-deploy-action@v4
        with:
          BRANCH: pages
          FOLDER: docs/.vitepress/dist

  # 任务2: 部署 Gitee Pages
  deploy-gitee-pages:
    runs-on: ubuntu-latest
    steps:
      # 1、同步内容到 Gitee
      - name: Sync to Gitee
        uses: wearerequired/git-mirror-action@master #使用action库
        env:
          SSH_PRIVATE_KEY: ${{ secrets.GITEE_RSA_PRIVATE_KEY }} #Gitee私钥
        with:
          source-repo: [email protected]:Charles7c/charles7c.github.io.git #GitHub 源仓库地址
          destination-repo: [email protected]:Charles7c/charles7c.git #Gitee 目标仓库地址
      # 2、部署 Gitee Pages
      - name: Deploy Gitee Pages
        # 手动执行时只同步内容, 不进行部署
        if: github.event_name != 'workflow_dispatch'
        uses: yanglbme/gitee-pages-action@main
        with:
          gitee-username: ${{ secrets.GITEE_USERNAME }} #Gitee 用户名
          gitee-password: ${{ secrets.GITEE_PASSWORD }} #Gitee 密码
          gitee-repo: Charles7c/charles7c #Gitee 仓库
          branch: pages #要部署的分支,默认是 master,若是其他分支,则需要指定(指定的分支必须存在)

总访问量
总访问人数 人次