Hugo 13 Github Action

Hugo + Github Actions 实现自动化部署

Github Action

GitHub Actions help you automate tasks within your software development life cycle. GitHub Actions are event-driven, meaning that you can run a series of commands after a specified event has occurred.

大家知道,持续集成由很多操作组成,比如抓取代码、运行测试、登录远程服务器,发布到第三方服务等等。GitHub 把这些操作就称为 actions。

很多操作在不同项目里面是类似的,完全可以共享。GitHub 注意到了这一点,想出了一个很妙的点子,允许开发者把每个操作写成独立的脚本文件,存放到代码仓库,使得其他开发者可以引用。

如果你需要某个 action,不必自己写复杂的脚本,直接引用他人写好的 action 即可,整个持续集成过程,就变成了一个 actions 的组合。这就是 GitHub Actions 最特别的地方。

GitHub 做了一个官方市场 ,可以搜索到他人提交的 actions。另外,还有一个 awesome actions 的仓库,也可以找到不少 action。

Conception

GitHub Actions 有一些自己的术语。

(1)workflow (工作流程):持续集成一次运行的过程,就是一个 workflow。The workflow can be used to build, test, package, release, or deploy a project on GitHub.

(2) ### Events

An event is a specific activity that triggers a workflow. For example, activity can originate from GitHub when someone pushes a commit to a repository or when an issue or pull request is created. You can also use the repository dispatch webhook to trigger a workflow when an external event occurs. For a complete list of events that can be used to trigger workflows, see Events that trigger workflows .

(3)job (任务):一个 workflow 由一个或多个 jobs 构成,含义是一次持续集成的运行,可以完成多个任务。

(4)step(步骤):每个 job 由多个 step 构成,一步步完成。

(5)action (动作):每个 step 可以依次执行一个或多个命令(action)。 (6) ### Runners

A runner is a server that has the GitHub Actions runner application installed. You can use a runner hosted by GitHub, or you can host your own. A runner listens for available jobs, runs one job at a time, and reports the progress, logs, and results back to GitHub. GitHub-hosted runners are based on Ubuntu Linux, Microsoft Windows, and macOS, and each job in a workflow runs in a fresh virtual environment. For information on GitHub-hosted runners, see “About GitHub-hosted runners .” If you need a different operating system or require a specific hardware configuration, you can host your own runners. For information on self-hosted runners, see “Hosting your own runners .”

example

 1name: learn-github-actions
 2on: [push]
 3jobs:
 4  check-bats-version:
 5    runs-on: ubuntu-latest
 6    steps:
 7      - uses: actions/checkout@v2
 8      - uses: actions/setup-node@v2
 9        with:
10          node-version: '14'
11      - run: npm install -g bats
12      - run: bats -v

Visualizing the workflow file ](https://raw.githubusercontent.com/parkman-auex/image_host/main/image202109061227577.png )

[Instance]

[准备部署]

我们开发的项目及github pages实际是分开的,一个用于保存项目,相当于源代码,另外一个用于保存最终的网页文件。

  1. 使用git生成ssh key(相当于生成对密钥)
1ssh-keygen -t rsa -b 4096 -C "$(git config user.email)" -f gh-pages -N ""
2#   You will get 2 files:
3#   gh-pages.pub (public key)
4#   gh-pages     (private key)
  1. 打开HUGO_blog仓库的settings,再点击Secrets,然后添加刚刚生成的私钥,name为ACTIONS_DEPLOY_KEY

  2. 同理,打开redisread.github.io,点击Deploy keys,添加公钥,Allow write access一定要勾上,否则会无法提交

创建workflow

~/actions/new Ref actions-hugo 首先在项目仓库点击action,创建workflow 输入一下的配置代码:

 1# This is a basic workflow to help you get started with Actions
 2
 3name: CI_website
 4
 5# Controls when the workflow will run
 6on:
 7  # Triggers the workflow on push or pull request events but only for the main branch
 8  push:
 9    branches: [ main ]
10  # pull_request:
11    # branches: [ main ]
12
13  # Allows you to run this workflow manually from the Actions tab
14  workflow_dispatch:
15
16# A workflow run is made up of one or more jobs that can run sequentially or in parallel
17jobs:
18  # This workflow contains a single job called "deploy"
19  deploy:
20    # The type of runner that the job will run on
21    runs-on: ubuntu-latest # 镜像市场
22    
23    # Steps represent a sequence of tasks that will be executed as part of the job
24    steps:
25      # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
26      - name: checkout
27        uses: actions/checkout@v2
28        with: # parameters
29         submodules: true # fetch-depth: 0  Fetch all history for .GitInfo and .Lastmod 
30      - name: Setup Hugo
31        uses: peaceiris/actions-hugo@v2
32        with:
33          hugo-version: 'latest'
34          extended: true  
35      - name: Build
36        run: hugo --minify # ADD PYTHON waiting
37      - name: Deploy
38        uses: peaceiris/actions-gh-pages@v3
39        with:
40          #github_token: ${{ secrets.GITHUB_TOKEN }}
41          #publish_dir: ./public
42          deploy_key: ${{ secrets.ACTIONS_DEPLOY_KEY }}
43          external_repository: parkman-auex/parkman-auex.github.io
44          keep_files: false # remove existing files
45          PUBLISH_BRANCH: main
46          PUBLISH_DIR: "./public"
47          commit_message: ${{ github.event.head_commit.message }}

TEST

Push 前 push 后 (Push后 )

python

  • 未引入 自动加密脚本 未引入 自动加密脚本
  • 查看网页源代码 可以解析 未引入 自动加密脚本 网页源码
  • 引入 自动加密脚本
  • 可以看到网页,有 cookies 记住过密码 引入 自动加密脚本 同时源码中这段内容也已经被加密 引入 自动加密脚本 网页源码

500 Words|This article has been read times