- 发表于
如何使用 GitHub Actions 创建博客文章提醒
- 作者
- 姓名
- Imamuzzaki Abu Salam
- https://x.com/ImBIOS_Dev
作为一名作家,我理解在发布新内容时保持一致性的重要性。但是,有时生活中会发生一些事情,这使得记住撰写新的博客文章变得很困难。为了帮助我保持分享计划的进度,我使用 GitHub Actions 创建了一个简单的提醒。在这篇文章中,我将分享我是如何创建这个工作流的。
什么是 GitHub Action?
GitHub Actions 是一款强大的工具,它可以让你自动化你的工作流。你可以使用它来构建、测试和部署你的代码。你也可以使用它来执行各种其他任务,例如发送通知或安排提醒。
我如何创建了一个撰写博客文章的提醒
为了创建撰写博客文章的提醒,我使用了 GitHub 的特殊仓库 的 README.md 并添加了一个名为 .github/workflows/blog-posts.yml
的文件。在这个文件中,我定义了 GitHub Actions 将执行的工作流。以下是该文件的初始内容:
name: Blog Posts
on:
schedule:
- cron: '0 0 * * 0' # 每周日 00:00 运行
workflow_dispatch:
jobs:
update-posts:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: 更新文章列表
run: |
sleep 1m
curl -LO https://blog.imam.dev/feed.xml
node src/list-posts.js
rm feed.xml
- name: 提交更改
run: |
git config --local user.email "github-actions[bot]@users.noreply.github.com"
git config --local user.name "github-actions[bot]"
git add -A
git diff-index --quiet HEAD || git commit -m "更新博客文章"
- name: 拉取更改
run: git pull -r
- name: 推送更改
uses: ad-m/github-push-action@0fafdd62b84042d49ec0cb92d9cac7f7ce4ec79e
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
此工作流在每周日的 00:00 触发。然后它运行一个脚本,该脚本更新博客文章列表。该脚本是用 JavaScript 编写的,它解析我博客的 RSS 订阅源。然后它生成一个博客文章列表,并更新 README.md 文件。最后,它提交更改并将其推送到 GitHub。我使用 ouuan 的仓库作为此工作流的参考。
提醒来自哪里?它实际上是在 list-posts.js
文件中。我在博客文章列表中添加了一个提醒。以下是该文件的内容:
const { readFileSync, writeFileSync } = require('fs')
/**
* 将 XML 字符串转换为 JSON
* @param {string} xmlString
* @returns {object} json
*/
const xmlToJson = (xmlString) => {
const regex = /<(\w+)([^>]*)>([\s\S]*?)<\/\1>/gm
const matches = xmlString.matchAll(regex)
const json = {}
for (const match of matches) {
const [, key, attributes, value] = match
const subMatches = value.matchAll(regex)
const subJson = {}
for (const subMatch of subMatches) {
const [, subKey, subAttributes, subValue] = subMatch
if (subValue.match(regex)) {
if (Array.isArray(subJson[subKey])) {
subJson[subKey].push(
xmlToJson(`<${subKey}${subAttributes}>${subValue}</${subKey}>`)[subKey]
)
} else if (subJson[subKey]) {
subJson[subKey] = [
subJson[subKey],
xmlToJson(`<${subKey}${subAttributes}>${subValue}</${subKey}>`)[subKey],
]
} else {
subJson[subKey] = xmlToJson(`<${subKey}${subAttributes}>${subValue}</${subKey}>`)[subKey]
}
} else if (Array.isArray(subJson[subKey])) {
subJson[subKey].push(subValue)
} else if (subJson[subKey]) {
subJson[subKey] = [subJson[subKey], subValue]
} else {
subJson[subKey] = subValue
}
}
if (json[key]) {
if (Array.isArray(json[key])) {
json[key].push(subJson)
} else {
json[key] = [json[key], subJson]
}
} else {
json[key] = subJson
}
}
return json
}
/**
* 按 pubDate 对 JSON 进行排序
* @param {object} json
* @returns {object} sortedJson
*/
const sortJson = (json) => {
json.sort((a, b) => new Date(b.pubDate) - new Date(a.pubDate))
return json
}
// 读取 XML 文件并将其转换为 JSON
const xmlString = readFileSync('feed.xml', 'utf8')
const feeds = sortJson(xmlToJson(xmlString).rss.channel.item)
// 创建文章的 Markdown 列表
const posts = feeds
.slice(0, 5)
.map(
(item) =>
`- ${new Date(item.pubDate).toISOString().split('T')[0]} [${item.title}](${
item.link
}?utm_source=GitHubProfile)`
)
// 如果文章发生了变化,更新 README.md,
// 否则抛出一个错误,提醒我撰写博客文章
const readme = readFileSync('README.md', 'utf8')
if (readme.includes(posts.join('\n'))) {
throw new Error('没有新的博客文章')
} else {
const updatedReadme = readFileSync('README.md', 'utf8').replace(
/(?<=<!--START_SECTION:blog-posts-->\n)[\s\S]*(?=\n<!--END_SECTION:blog-posts-->)/,
posts.join('\n')
)
writeFileSync('README.md', updatedReadme)
console.log('已更新 README.md')
}
该脚本读取我博客的 RSS 订阅源,并生成一个博客文章列表。然后它使用博客文章列表更新 README.md 文件。如果没有新的博客文章,它会抛出一个错误来提醒我撰写博客文章。
这只是一个在文章仍然相同的情况下,脚本执行时会抛出的错误,它不是一个发送到我的电子邮件或其他更明显地方的提醒。因此,我决定启用任何工作流运行失败的通知。以下是操作方法:
点击页面右上角,选择 设置。
在左侧边栏中选择 通知。
点击 Actions。
选择 仅针对失败的工作流发送通知。
现在,当脚本执行且没有新的博客文章时,我将收到一个通知。我也可以在 GitHub 网站上看到通知。
我探索的另一种方法
之前我告诉你的工作流是一个修改后的版本,这样我的 README.md 始终是最新的。我还探索了另一种创建撰写博客文章提醒的方法。但它只是一个纯粹的提醒,没有任何 README.md 更新机制,只是一个提醒。
为了创建撰写博客文章的提醒,我创建了一个新的 GitHub 仓库,并添加了一个名为 .github/workflows/remind.yml
的文件。在这个文件中,我定义了 GitHub Actions 将执行的工作流。以下是该文件的内容:
name: Reminder to write a blog post
on:
schedule:
- cron: '0 10 * * 1-5'
jobs:
remind:
runs-on: ubuntu-latest
steps:
- name: 发送提醒
uses: dawidd6/action-send-mail@v3.1.0
with:
server_address: smtp.gmail.com
server_port: 465
username: ${{ secrets.EMAIL_USERNAME }}
password: ${{ secrets.EMAIL_PASSWORD }}
subject: 'Reminder to write a new blog post'
body: "Don't forget to write a new blog post today!"
to: my-email@example.com
此工作流在工作日的上午 10:00 向我发送电子邮件提醒,提醒我撰写新的博客文章。我使用了一个第三方 action,dawidd6/action-send-mail
来发送电子邮件。我将我的电子邮件凭据作为 GitHub 机密提供,因此它们在工作流文件中不可见。
总结
我探索了两种创建撰写博客文章提醒的方法。第一种方法是更新我的 GitHub 个人资料的 README.md 文件。第二种方法是发送电子邮件提醒。我目前正在使用第一种方法,因为它比第二种方法更明显。每次访问我的 GitHub 个人资料时,我都能看到提醒。
使用 GitHub Actions 创建撰写博客文章的提醒是一种简单有效的方法,可以让你保持博客计划的进度。有了此工作流,你将永远不会忘记撰写新的文章。如果你有兴趣创建自己的提醒工作流,请务必查看 GitHub Actions 文档以了解更多信息。祝你博客愉快!