使用GitHub Actions持续集成Hugo静态网站到服务器

GitHub Actions 是由GitHub在2018年推出的一款持续集成的服务方案。

我的个人博客是使用Hugo基于markdown生成的静态网站,markdown源文件host在GitHub中,网站host在Centos虚拟机中。

因此,准备使用GitHub Actions来自动构建、部署网站。

准备

  1. 了解GitHub Actions如何工作和.yml的结构。
  2. 了解GitHub Actions插件

具体的细节搜一搜就能找到了。

开始

.yml文件主要有两部分:Hugo打包 和 上传部署到远程服务器。

本站主要用了两个GitHub Actions组件:

  1. peaceiris/actions-hugo
  2. easingthemes/ssh-deploy

下面是本站的.yml:

name: My Blog

on:
  push:
    branches:
      - master  # Set a branch to deploy

jobs:
  build_and_deploy:
    runs-on: ubuntu-18.04
    steps:
      - uses: actions/checkout@v2
        with:
          submodules: true  # Fetch Hugo themes (true OR recursive)
          fetch-depth: 0    # Fetch all history for .GitInfo and .Lastmod

      - name: Setup Hugo
        uses: peaceiris/actions-hugo@v2
        with:
          hugo-version: '0.79.1'
          # extended: true

      - name: Build
        run: hugo --minify

      - name: SSH Deploy
        uses: easingthemes/ssh-deploy@v2.1.5
        env:
          SSH_PRIVATE_KEY: ${{ secrets.SERVER_SSH_PRIVATE_KEY }}
          ARGS: "-rltgoDzvO"
          SOURCE: "public/"
          REMOTE_HOST: ${{ secrets.REMOTE_HOST }}
          REMOTE_USER: ${{ secrets.REMOTE_USER }}
          TARGET: ${{ secrets.REMOTE_TARGET }}

配置

easingthemes/ssh-deploy组件

在你的GitHub repository -> Settings -> Secrets配置以下环境变量:

  1. SERVER_SSH_PRIVATE_KEY
  2. REMOTE_HOST
  3. REMOTE_USER
  4. REMOTE_TARGET

字段具体含义和private key的生成参考其官网:https://GitHub.com/easingthemes/ssh-deploy

这里需要注意下面这句话:

Private key part of an SSH key pair. 
The public key part should be added to the authorized_keys file on the server that receives the deployment.

其他遇到的问题

  1. 'Load key "/home/runner/.ssh/deploy_key": invalid format'

原因: 没有用ssh-deploy官方提到的方式:

The keys should be generated using the PEM format. You can use this command

ssh-keygen -m PEM -t rsa -b 4096
  1. 'bash: rsync: command not found'

原因:需要在网站host server上安装 rsync

我使用的是CentOS,用yum安装一下即可

yum install rsync

参考资料

  1. GitHub Actions 入门教程

使用GitHub Actions持续集成Hugo静态网站到服务器

微信公众号:编码者频道

扫描二维码(或者长按识别二维码)关注公众号获取最新信息。

本文版权归作者和博客园共有,欢迎转载,

转载请保留原文链接,谢谢!

原文地址:https://www.cnblogs.com/codesee/p/14531516.html