部署到 Github Pages

2021-01-01

使用 github workflows 自动部署

配置


  1. 新建 SSH
  • 生成密钥

    ssh-keygen -t rsa -m pem -b 4096 -C " your git email name " -N " "
    
  • 获取私钥

    cat .ssh/id_rsa
    
  • 获取公钥

    cat .ssh/id_rsa.pub
    
  1. 配置私钥
  • open https://{your-github-page-resource}/settings/secrets/actions/new

    • Set Name

      input ACTIONS_DEPLOY_KEY

    • Set Value

      input your private key

  1. 配置公钥
  • open https://{your-github-page-path}/settings/keys/new

    • Set Name

      input ACTIONS_DEPLOY_KEY

    • Set Value

      input your public key

    • Check Allow write access

编写 workflows


在项目根目录增加.github/workflows/deploy.yaml文件,然后配置如下信息

  • 使用 docker

    因依赖较少,故速度更快

     1# 名称,可自定义
     2name: Fungo Deploy
     3
     4# 依赖
     5on:
     6  # 当命令为 push
     7  push:
     8    # 且分支为 master 时,触发
     9    branches:
    10      - master
    11
    12  # 当命令为 pull
    13  pull_request:
    14    # 且分支为 master 时,触发
    15    branches:
    16      - master
    17  # 手动触发
    18  workflow_dispatch:
    19
    20# 任务
    21jobs:
    22  # 任务名称,可自定义
    23  deploy:
    24    # 启动依赖,可设置为 ubuntu,windows,macos
    25    runs-on: ubuntu-latest
    26
    27    # 使用docker 镜像
    28    container:
    29      image: fundipper/fungo:latest
    30
    31    # 步骤
    32    steps:
    33      # 检出代码
    34      - name: checkout
    35        uses: actions/[email protected]
    36
    37      # 配置ssh
    38      - name: config
    39        run: |
    40          set -e
    41          echo "Host *
    42            StrictHostKeyChecking no
    43            UserKnownHostsFile=/dev/null
    44          " > /etc/ssh/ssh_config          
    45
    46      # 编译项目
    47      - name: build
    48        run: fungo build
    49
    50      # 部署项目
    51      - name: deploy
    52        uses: peaceiris/[email protected]
    53        with:
    54          commit_message: ${{ github.event.head_commit.message }}
    55          # 部署密钥
    56          deploy_key: ${{ secrets.ACTIONS_DEPLOY_KEY }}
    57          # 发布地址
    58          external_repository: "your github page path as 'fundipper/fundipper.github.io'"
    59          # 使用分支
    60          publish_branch: "your branch as 'master'"
    61          # 使用目录
    62          publish_dir: ./public
    63          # 自定义域名
    64          cname: { your-customize-domain }
    
  • 使用 go install

    因依赖较多,故部署略慢

     1# 名称,可自定义
     2name: Fungo Deploy
     3
     4# 依赖
     5on:
     6  # 当命令为 push
     7  push:
     8    # 且分支为 master 时,触发
     9    branches:
    10      - master
    11
    12  # 当命令为 pull
    13  pull_request:
    14    # 且分支为 master 时,触发
    15    branches:
    16      - master
    17  # 手动触发
    18  workflow_dispatch:
    19
    20# 任务
    21jobs:
    22  # 任务名称,可自定义
    23  deploy:
    24    # 启动依赖,可设置为 ubuntu,windows,macos
    25    runs-on: ubuntu-latest
    26
    27    # 步骤
    28    steps:
    29      # 检出代码
    30      - name: checkout
    31        uses: actions/[email protected]
    32
    33      # 安装golang
    34      - name: setup
    35        uses: actions/[email protected]
    36        with:
    37          go-version: 1.18
    38
    39      # 安装fungo
    40      - name: install
    41        run: go install github.com/fundipper/[email protected]
    42
    43      # 编译项目
    44      - name: build
    45        run: fungo build
    46
    47      # 部署项目
    48      - name: deploy
    49        uses: peaceiris/[email protected]
    50        with:
    51          commit_message: ${{ github.event.head_commit.message }}
    52          # 部署密钥
    53          deploy_key: ${{ secrets.ACTIONS_DEPLOY_KEY }}
    54          # 发布地址
    55          external_repository: "your github page path as 'fundipper/fundipper.github.io'"
    56          # 使用分支
    57          publish_branch: "your branch as 'master'"
    58          # 使用目录
    59          publish_dir: ./public
    60          # 自定义域名
    61          cname: { your-customize-domain }
    

查看 workflows


  • 使用 git push 提交代码到 github

  • open https://{your-github-page-source}/actions

    查看 github workflows 是否执行成功

自定义域名


  1. 解析域域名
  • 将需要绑定的域名,使用 CNAME 解析至{your-github-page-path}
  1. 绑定域名
  • 待解析生效后,访问https://{your-github-page-path}/settings/pages

  • 输入自定义域名,点击保存