Github Actions 功能很强大,网上各种能玩出花的,我有这个想法,是因为折腾某个不可描述的网站的签到,那啥就不说了。

Pelican 是个用 Python 写的一个静态网站生成工具,本站就是用这个生成的。毕竟在自己电脑上生成再提交 github 还是挺麻烦的,要装 好一些 软件呢。

Github Actions 能自动去处理,节省了好多步骤,现在只要负责写写,转转,然后提交到源仓库就好了,其它就交给 Github 吧。

其实也很简单,源仓库建一个 .github/workflows 目录,里面放上 xxx.yml 文件,比如: site_generator.yml。贴上下面的内容:

# This workflow will install Python dependencies, run tests and lint with a single version of Python
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-python-with-github-actions

name: Pelican site generator

on:
  push:
    branches: [ master ]
  pull_request:
    branches: [ master ]

jobs:
  build:

    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v2
    - name: Set up Python 3.9
      uses: actions/setup-python@v2
      with:
        python-version: 3.9
    - name: Install dependencies
      run: |
        python -m pip install --upgrade pip
        pip install flake8 pytest
        if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
    - name: Lint with flake8
      run: |
        # stop the build if there are Python syntax errors or undefined names
        flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
        # exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
        flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
    - name: config git and clone repo
      run: |
        # 设置 git
        git config --global user.email "rovecat@gmail.com"&&git config --global user.name "doobom"
        # 获取 pelican 插件
        git clone --recursive https://github.com/getpelican/pelican-plugins
        # 获取本站的皮
        git clone https://github.com/doobom/elegant.git
    - name: setup SSH keys and known_hosts
      run: |
        # 设置 SSH 环境
        mkdir -p ~/.ssh
        ssh-keyscan github.com >> ~/.ssh/known_hosts
        ssh-agent -a $SSH_AUTH_SOCK > /dev/null
        ssh-add - <<< "${{ secrets.ID_RSA }}"
      env:
        SSH_AUTH_SOCK: /tmp/ssh_agent.sock
    - name: clone doobom.github.io
      run: |
        # 获取网站保存的 repo 到 githubpage 目录,因为我的配置文件里面设置的生成目标目录是 githubpage
        git clone git@github.com:doobom/doobom.github.io.git githubpage
      env:
        SSH_AUTH_SOCK: /tmp/ssh_agent.sock
    - name: pelican make html
      run: |
        # 用 make 命令生成
        make publish
    - name: publish to gitpage
      run: |
        # 提交
        cd githubpage
        git status
        git add .
        git commit -m "auto build and update by github actions."
        git push origin master
      env:
        SSH_AUTH_SOCK: /tmp/ssh_agent.sock

在源仓库 settings > secrets 添加一个 ID_RSA 的环境变量,参考: Github Documents 因为 Pelican 要装挺多东西的,所以放到 requirements.txt 文件里面:

beautifulsoup4>=4.9.3
bs4>=0.0.1
blinker>=1.4
cssmin>=0.2.0
ddt>=1.4.1
docutils>=0.16
feedgenerator>=1.9.1
gitdb2>=4.0.2
GitPython>=3.1.11
invoke>=1.4.1
jinja2>=2.11
livereload>=2.6.3
Markdown>=3.3.3
MarkupSafe>=1.1.1
pelican>=4.5.3
Pygments<2.7.0,>=2.6.1
python-dateutil>=2.8.1
pytz>=2020.4
six>=1.15.0
smartypants>=2.0.1
smmap>=3.0.4
soupsieve>=2.1
tornado>=6.1
typogrify>=2.0.7
Unidecode>=1.1.1
webassets>=2.0

也并不是全部要用的就是了。

另外就是本身 Pelican 的配置文件。

Push 源仓库就可以看到日志了。


参考:

Like this post? Share on:


doobom Avatar doobom is write a bug.
Comments

So what do you think? Did I miss something? Is any part unclear? Leave your comments below.

comments powered by Disqus

Keep Reading


Published

Category

git

Tags

Stay in Touch

Get New Release Alert