偶然发现github actions可以执行一些命令,这下可以把我的crontab停掉了,也不用占用一台机器专门用来发布博客了,并且完全免费!!
首先我的GitHub pages和博客原始文件是分为两个仓库的,在GitHub Pages仓库里添加了一个Actions用来钉钉发通知:
GitHub仓库配置Secret
需要先将环境变量配置在 Settings –> Secrets and Variables –> Actions 里面

配置后,可以在actions里面通过 ${{ secrets.dingtalk_secret }}
调用到对应的数据
针对Hexo博客的构建
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| name: Deploy GitHub Pages
on: push: branches: - master
jobs: deploy_github_pages: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 with: submodules: recursive fetch-depth: 0 - name: Use Node.js 16.x uses: actions/setup-node@v1 with: node-version: 16.x
- name: build_hexo run: | git ls-files -z | while read -d '' path; do touch -d "$(git log -1 --format="@%ct" "$path")" "$path"; done npm install npx hexo clean npx hexo g - name: Deploy 🚀 uses: JamesIves/github-pages-deploy-action@4.1.5 with: BRANCH: main FOLDER: public ssh-key: ${{ secrets.DEPLOY_SSH_KEY }} repository-name: iuxt/iuxt.github.io
|
针对于Hugo博客的构建
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
| name: Deploy GitHub Pages
on: push: branches: - master schedule: - cron: '0 0 * * *'
jobs: deploy_github_pages: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 with: submodules: recursive
- name: Setup Hugo uses: peaceiris/actions-hugo@v2 with: hugo-version: '0.80.0' extended: true
- name: Build run: | git config core.quotePath false hugo
- name: Deploy 🚀 uses: JamesIves/github-pages-deploy-action@4.1.4 with: BRANCH: main FOLDER: public ssh-key: ${{ secrets.DEPLOY_SSH_KEY }} repository-name: iuxt/iuxt.github.io
- name: Use Node.js 10.x uses: actions/setup-node@v1 with: node-version: 10.x - name: algolia upload run: | npm install npm run algolia # 使用algolia的,可以同时推送到algolia env: ALGOLIA_APP_ID: ${{ secrets.ALGOLIA_APP_ID }} ALGOLIA_INDEX_NAME: blog ALGOLIA_INDEX_FILE: public/index.json ALGOLIA_ADMIN_KEY: ${{ secrets.ALGOLIA_ADMIN_KEY }}
|
推送到百度,加快收录
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
| name: Baidu push
on: push: branches: - master schedule: - cron: '0 0 * * *'
jobs: baidu-push: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 with: submodules: recursive
- name: Setup Hugo uses: peaceiris/actions-hugo@v2 with: hugo-version: '0.80.0' extended: true
- name: Build run: | git config core.quotePath false hugo # 使用hugo命令生成静态文件
- name: Install requests run: pip install requests
- name: Push run: | python <<END import requests import re with open('public/sitemap.xml', 'r') as sitemap: pattern = re.compile(r'(?<=<loc>).+?(?=</loc>)') result = pattern.findall(sitemap.read()) req = requests.post('http://data.zz.baidu.com/urls?site=https://zahui.fan&token=${{ secrets.BAIDU_PUSH_TOKEN }}', '\n'.join(result)) print(req.text) END
|
发送钉钉通知
这个我配置在了静态文件所在的仓库,就是hexo构建完成推送到的仓库,每当仓库发生了更新, 就会推送一条通知到钉钉
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
| name: dingtalk_notify
on: push: branches: - main
jobs: dingtalk_notify: runs-on: ubuntu-latest steps: - name: dingtalk_notify run: | python <<END import time import hmac import hashlib import base64 import urllib.parse import requests
now = str(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
secret = "${{ secrets.dingtalk_secret }}" webhook_url = "${{ secrets.dingtalk_url }}"
timestamp = str(round(time.time() * 1000)) secret_enc = secret.encode('utf-8') string_to_sign = '{}\n{}'.format(timestamp, secret) string_to_sign_enc = string_to_sign.encode('utf-8') hmac_code = hmac.new(secret_enc, string_to_sign_enc, digestmod=hashlib.sha256).digest() sign = urllib.parse.quote_plus(base64.b64encode(hmac_code))
data = """ { "msgtype":"text", "text": { "content": "%s %s" } } """ %(now, "博客自动发布成功")
params={ 'timestamp': timestamp, 'sign': sign }
header = {'Content-Type': 'application/json'}
r = requests.post(webhook_url, headers=header, params=params, data=data.encode('utf-8')) print(r.text) END
|