远飞闲记

阅读、思考、技术

使用Github Actions自动发布Hugo Blog配置

🗓️发布日期:2020-06-08 · 🗺️总字数: 552 字 · ⏱️阅读时间: 2 Minute

Github Actions绝对是提高生产力的工具,其实本质跟Jenkins && Gitlab CI/CD一致都是用于对“解放双手”而开发的工具,在使用Github Actions发布Blog Post之前一直都是使用在根目录下建了一个 deploy.sh 的文件每次在写完文章后都会 sh ./deplay.sh 一下同步上传,现在使用Github Actions的配置完全只需要写好Post后直接提交到Github的仓库中就👌了,剩下的就完全交给配置好的Hugo Publish Actions去执行并上传;

GitHub Actions是什么和为什么要使用及其配置文件参数说明,参见Getting started with GitHub Actions ,在为项目Git仓库配置好Actions会在项目的目录下生成一个 .github/workflows/main.yml 的文件初始化的时候也可以手动去创建目录和对应的 main.yml 文件,有了这个文件后就可以 Github Marketplace Actions 中搜索需要的自动化Actions进行设置了;

Hugo Action


# This is a basic workflow to help you get started with Actions
name: Hugo Publish
# Controls when the action will run. Triggers the workflow on push or pull request
# events but only for the master branch
on:
  push:
    branches: [ master ]

      pull_request:
	branches: [ master ]
	  jobs:
	    # This workflow contains a single job called "build"
	    build:
	      # The type of runner that the job will run on
	      runs-on: ubuntu-latest
	      # Steps represent a sequence of tasks that will be executed as part of the job
	      steps:
		# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
		- uses: actions/checkout@v2
		    with:
		      submodules: true  # Fetch Hugo themes (true OR recursive)
		      fetch-depth: 0    # Fetch all history for .GitInfo and .Lastmod
		      # build hugo
		      - name: Hugo Actions
			uses: srt32/hugo-action@v0.0.3

SSH Deploy 配置

1
2
3
4
5
6
7
8
9
- name: Deploy to Staging server
    uses: easingthemes/ssh-deploy@v2.0.7
      env:
	SSH_PRIVATE_KEY: ${{ secrets.SERVER_SSH_KEY }}
	ARGS: "-avz --delete"
	SOURCE: "public/"
	REMOTE_HOST: ${{ secrets.REMOTE_HOST }}
	REMOTE_USER: ${{ secrets.REMOTE_USER }}
	TARGET: ${{ secrets.REMOTE_TARGET }}

配置其中的 secrets 变量参数,请到对应项目仓库 Settings->Secrets 中设置,如下图:

🏷️ 开发流程 git