深夜提醒

现在是深夜,建议您注意休息,不要熬夜哦~

🏮 🏮 🏮

新年快乐

祝君万事如意心想事成!

share-image
中
ESC

GitHub Actions in Practice: Automated Hexo Blog Deployment and Scheduled Tasks

English 阅读中文版

While maintaining a Hexo blog, manually running hexo clean && hexo g && hexo d is not just tedious but also prone to deployment failures caused by local environment differences (like inconsistent Node.js versions).

This article shows how to use GitHub Actions for automated deployment and how to add scheduled tasks to update the blog’s data automatically.

What is GitHub Actions?

GitHub Actions is GitHub’s continuous integration and continuous deployment (CI/CD) platform. It lets you automate build, test and deploy workflows directly in a GitHub repository.

The core concepts include:

  • Workflow: an automated process made up of one or more jobs, defined in a .yml file.
  • Event: the rule that triggers a workflow, such as push (a code commit) or schedule (a timed trigger).
  • Job: a group of steps that run on the same runner.
  • Step: a specific operation within a job, either running a script or using a ready-made Action.

Hands-On Configuration

In the project we created the file .github/workflows/blog_automation.yml, which implements two core features:

  1. Auto-deploy on commit: when code is pushed to the main branch, the blog is automatically built and deployed.
  2. Scheduled data scraping: run the scraper script once an hour and automatically commit the updated data back to the repository.

1. Workflow Trigger Rules

First, define the trigger conditions:

name: Blog Automation

on:
push:
branches:
- main
- master
schedule:
- cron: '0 * * * *' # Run once every hour

2. The Deploy Job

This job runs only when the push event fires. It sets up a Node.js environment (version 24), installs dependencies, and runs the deploy script.

jobs:
deploy:
if: github.event_name == 'push'
runs-on: ubuntu-latest
permissions:
contents: read
steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: '24' # Keep it aligned with the local dev environment
cache: 'npm' # Enable caching to speed up installation

- name: Install Dependencies
run: npm ci

- name: Install Playwright Browsers
run: npx playwright install --with-deps chromium

- name: Run Deploy Script
run: bash deploy.sh

Key points:

  • npm ci: compared with npm install, it’s better suited to CI, installing dependencies strictly per package-lock.json.
  • deploy.sh: we wrap the specific Hexo commands in a script and use npx to ensure we invoke the locally installed Hexo:
#!/bin/bash
set -e
echo "=== Cleaning the Hexo cache ==="
npx hexo clean
echo "=== Generating Hexo static files ==="
npx hexo g
echo "=== Deploying to the server ==="
npx hexo d

3. Scheduled Scraping with Auto-Commit (Scrape Job)

This job shows off GitHub Actions’ powerful write-back capability. It runs the scraper on a schedule and, if there is updated data, automatically commits and pushes back to the repository.

scrape:
if: github.event_name == 'schedule'
runs-on: ubuntu-latest
permissions:
contents: write # Needs write permission to commit code
steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: '24'
cache: 'npm'

# ... dependency-install steps same as above ...

- name: Run Scraper
run: node rq_scraper.js

- name: Commit and Push Changes
run: |
git config --global user.name 'github-actions[bot]'
git config --global user.email 'github-actions[bot]@users.noreply.github.com'
git add source/_data/rq_data.json
# Commit only if there are changes
if ! git diff --staged --quiet; then
git commit -m "chore: update rq data [skip ci]"
git push
echo "Changes pushed to repository."
else
echo "No changes to commit."
fi

Tips:

  • permissions: contents: write: this is required, otherwise the Action cannot push changes to the repository.
  • [skip ci]: adding this marker to the commit message prevents that commit from re-triggering the push event and causing a build loop.

Summary

With GitHub Actions we not only free our hands but also keep the deployment environment consistent (Node.js 24). Whether it’s a simple static-blog deployment or complex scheduled data processing, GitHub Actions offers an elegant solution.

文章作者:阿文
文章链接: https://www.awen.me/post/976a6ae8.html
版权声明:本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 阿文的博客!
本文于 2026-01-30 10:00 发布,已超过半年(260天),请注意甄别内容是否已过期。

评论

0 条评论
😀😃😄 😁😅😂 🤣😊😇 🙂🙃😉 😌😍🥰 😘😗😙 😚😋😛 😝😜🤪 🤨🧐🤓 😎🥸🤩 🥳😏😒 😞😔😟 😕🙁☹️ 😣😖😫 😩🥺😢 😭😤😠 😡🤬🤯 😳🥵🥶 😱😨😰 😥😓🤗 🤔🤭🤫 🤥😶😐 😑😬🙄 😯😦😧 😮😲🥱 😴🤤😪 😵🤐🥴 🤢🤮🤧 😷🤒🤕 🤑🤠😈 👿👹👺 🤡💩👻 💀☠️👽 👾🤖🎃 😺😸😹 😻😼😽 🙀😿😾 👍👎👏 🙌👐🤲 🤝🤜🤛 ✌️🤞🤟 🤘👌🤏 👈👉👆 👇☝️✋ 🤚🖐️🖖 👋🤙💪 🦾🖕✍️ 🙏💅🤳 💯💢💥 💫💦💨 🕳️💣💬 👁️‍🗨️🗨️🗯️ 💭💤❤️ 🧡💛💚 💙💜🖤 🤍🤎💔 ❣️💕💞 💓💗💖 💘💝💟 ☮️✝️☪️ 🕉️☸️✡️ 🔯🕎☯️ ☦️🛐⛎ ♈♉♊ ♋♌♍ ♎♏♐ ♑♒♓ 🆔⚛️🉑 ☢️☣️📴 📳🈶🈚 🈸🈺🈷️ ✴️🆚💮 🉐㊙️㊗️ 🈴🈵🈹 🈲🅰️🅱️ 🆎🆑🅾️ 🆘❌⭕ 🛑⛔📛 🚫💯💢 ♨️🚷🚯 🚳🚱🔞 📵🚭❗ ❕❓❔ ‼️⁉️🔅 🔆〽️⚠️ 🚸🔱⚜️ 🔰♻️✅ 🈯💹❇️ ✳️❎🌐 💠Ⓜ️🌀 💤🏧🚾 ♿🅿️🈳 🈂🛂🛃 🛄🛅🛗 🚀🛸🚁 🚉🚆🚅 ✈️🛫🛬 🛩️💺🛰️
加载中...

留言反馈

😀😃😄 😁😅😂 🤣😊😇 🙂🙃😉 😌😍🥰 😘😗😙 😚😋😛 😝😜🤪 🤨🧐🤓 😎🥸🤩 🥳😏😒 😞😔😟 😕🙁☹️ 😣😖😫 😩🥺😢 😭😤😠 😡🤬🤯 😳🥵🥶 😱😨😰 😥😓🤗 🤔🤭🤫 🤥😶😐 😑😬🙄 😯😦😧 😮😲🥱 😴🤤😪 😵🤐🥴 🤢🤮🤧 😷🤒🤕 🤑🤠😈 👿👹👺 🤡💩👻 💀☠️👽 👾🤖🎃 😺😸😹 😻😼😽 🙀😿😾 👍👎👏 🙌👐🤲 🤝🤜🤛 ✌️🤞🤟 🤘👌🤏 👈👉👆 👇☝️✋ 🤚🖐️🖖 👋🤙💪 🦾🖕✍️ 🙏💅🤳 💯💢💥 💫💦💨 🕳️💣💬 👁️‍🗨️🗨️🗯️ 💭💤❤️ 🧡💛💚 💙💜🖤 🤍🤎💔 ❣️💕💞 💓💗💖 💘💝💟 ☮️✝️☪️ 🕉️☸️✡️ 🔯🕎☯️ ☦️🛐⛎ ♈♉♊ ♋♌♍ ♎♏♐ ♑♒♓ 🆔⚛️🉑 ☢️☣️📴 📳🈶🈚 🈸🈺🈷️ ✴️🆚💮 🉐㊙️㊗️ 🈴🈵🈹 🈲🅰️🅱️ 🆎🆑🅾️ 🆘❌⭕ 🛑⛔📛 🚫💯💢 ♨️🚷🚯 🚳🚱🔞 📵🚭❗ ❕❓❔ ‼️⁉️🔅 🔆〽️⚠️ 🚸🔱⚜️ 🔰♻️✅ 🈯💹❇️ ✳️❎🌐 💠Ⓜ️🌀 💤🏧🚾 ♿🅿️🈳 🈂🛂🛃 🛄🛅🛗 🚀🛸🚁 🚉🚆🚅 ✈️🛫🛬 🛩️💺🛰️