如何把Issue博客自动同步到github page中
这个问题我问了chatgpt:
问题1
生成一段github的workflows配置文档,当有新issue的时候,自动将issue的内容同步到该GitHub项目的blog分支的content/post文件夹中的新Markdown文件中。文件名为"{YYYYMMDD}-{Title}.md",其中{YYYYMMDD}为issue的日期,"{Title}"为issue的标题。文件开头加上
---
title:
data:
lastmod:
draft: false
tags:
---
它给我回复了一个GitHub Workflows 配置文档
name: Sync Issue to Blogon:issues:types: [opened, edited]jobs:sync-issue-to-blog:runs-on: ubuntu-lateststeps:- name: Checkout Codeuses: actions/checkout@v2- name: Setup Hugouses: peaceiris/actions-hugo@v2with:hugo-version: "latest"- name: Create Blog Postid: create_postrun: |issue_title=$(echo "${{ github.event.issue.title }}" | sed 's/[[:space:]]/-/g')issue_date=$(echo "${{ github.event.issue.created_at }}" | cut -c1-10 | sed 's/-//g')post_path="content/post/${issue_date}-${issue_title}.md"post_date=$(echo "${{ github.event.issue.created_at }}" | cut -c1-10)post_title=$(echo "${{ github.event.issue.title }}" | sed 's/"/\\"/g')post_body=$(echo "${{ github.event.issue.body }}" | sed 's/"/\\"/g')echo "---" > "${post_path}"echo "title: \"${post_title}\"" >> "${post_path}"echo "date: \"${post_date}\"" >> "${post_path}"echo "lastmod: \"${post_date}\"" >> "${post_path}"echo "draft: false" >> "${post_path}"echo "tags:" >> "${post_path}"echo "---" >> "${post_path}"echo "${post_body}" >> "${post_path}"- name: Commit and Push Changesrun: |git config --global user.name "${{ github.actor }}"git config --global user.email "${{ github.actor }}@users.noreply.github.com"git add .git commit -m "Sync issue #${{ github.event.issue.number }} to blog"git push origin blogenv:GH_TOKEN: ${{ secrets.GH_TOKEN }}
这个 Workflows 将会在项目中有新的 issue 被创建或编辑时运行。它首先会检出代码库,并安装最新版的 Hugo。接下来,它会创建一个新的 Markdown 文件,其中包含了 issue 的标题和正文,并将其保存到项目的 blog 分支的 content/post 目录下,文件名格式为 {YYYYMMDD}-{Title}.md。最后,它会将更改提交到 blog 分支,并使用 GitHub API 的 token 进行身份验证。注意,为了使 Workflows 正确运行,你需要将 GitHub API 的 token 存储在你的项目的 secrets 中,并将其命名为 GH_TOKEN。
问题2
后面我又完善了gpt给的回答,重新问了new bing。
编写github action配置文件: 当issue的open/edit事件触发时,使用GitHub CLI client获取issue的详细信息。
定义issue的id为ISSUE_ID。遍历content/posts/下的所有markdown文件,读取markdown文件头部的yaml部分,如果存在issue_id=ISSUEID,那么定义变量POSTPATH=该markdown文件路径,否则定义变量POSTPATH="content/post/{ISSUE_ID},那么定义变量POST_PATH=该markdown文件路径,否则定义变量POST_PATH="content/post/ISSUEID,那么定义变量POSTPATH=该markdown文件路径,否则定义变量POSTPATH="content/post/{issue_date}-${issue_title}.md"。
设置环境变量BLOG_TITLE为issue的标题,
设置环境变量BLOG_DATE为issue的创建时间,
格式为2020-09-15T11:30:03+00:00,
设置环境变量BLOG_DESCRIPTION为空白,
设置环境变量BLOG_TAGS为issue的标签列表,格式为[“tag1”,“tag2”],
设置环境变量BLOG_CONTENT为issue的内容,基于markdown格式。
最后,使用envsubst命令,用环境变量更新template/template.md的内容,将更新后的文件保存在${POST_PATH}路径下。
# This is a basic workflow to help you get started with Actionsname: Blog Post Generator# Controls when the action will run.
on:# Triggers the workflow on issue open or edit eventsissues:types: [opened, edited]# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:# This job generates a blog post from an issuegenerate-post:# The type of runner that the job will run onruns-on: ubuntu-latest# Steps represent a sequence of tasks that will be executed as part of the jobsteps:# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it- uses: actions/checkout@v2# Installs GitHub CLI on the runner (you may need to adjust this according to your OS)- name: Install GitHub CLIrun: |sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-key C99B11DEB97541F0sudo apt-add-repository https://cli.github.com/packagessudo apt updatesudo apt install gh# Authenticates to GitHub using a personal access token (you need to store it as a secret in your repository settings)- name: Authenticate to GitHubenv:GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}run: |gh auth login --with-token < $GITHUB_TOKEN# Runs a bash script to set environment variables and update template file using GitHub CLI and envsubst commands- name: Run bash scriptenv:ISSUE_ID: ${{ github.event.issue.number }}run: |# Get the issue data from GitHub CLI using gh api command (you may need to specify owner and repo flags)issue_data=$(gh api issues/${ISSUE_ID})# Set BLOG_TITLE to the issue's titleBLOG_TITLE=$(echo "$issue_data" | jq -r '.title')# Set BLOG_DATE to the issue's creation time, formatted as 2020-09-15T11:30:03+00:00BLOG_DATE=$(echo "$issue_data" | jq -r '.created_at')# Set BLOG_DESCRIPTION to blankBLOG_DESCRIPTION=""# Set BLOG_TAGS to the issue's label list, formatted as ["tag1","tag2"]BLOG_TAGS=$(echo "$issue_data" | jq -r '[.labels[].name]')# Set BLOG_CONTENT to the issue's content, based on markdown formatBLOG_CONTENT=$(echo "$issue_data" | jq -r '.body')# Export these variables so they can be used by envsubst command laterexport BLOG_TITLE BLOG_DATE BLOG_DESCRIPTION BLOG_TAGS BLOG_CONTENT# Loop through all markdown files under content/posts/for file in content/posts/*.md; do# Read the yaml part at the top of each file using yq command (you may need to install yq first)yaml_part=$(yq e '.' $file)# Check if there is an issue_id field that matches ISSUE_ID if [[ $(echo "$yaml_part" | yq e '.issue_id' -) == "$ISSUE_ID" ]]; then# If yes, set POST_PATH to that file path and break out of loop POST_PATH=$file break else# If no, set POST_PATH using issue date and title (you may need to adjust this according to your desired format)POST_PATH="content/post/${BLOG_DATE}-${BLOG_TITLE}.md"fidone# Use these variables to update template/template.md's content using envsubst command (you may need to adjust this according to your template format)envsubst < template/template.md > ${POST_PATH}# Commits and pushes the updated file to your repository (you may need a personal access token for this step)- name: Commit and push changes run: |git config --global user.name 'GitHub Action'git config --global user.email 'action@github.com'git add ${POST_PATH}
git commit -m "Update blog post from issue $ISSUE_ID"
git push origin main
微调
github cli客户端是默认安装在github workflow的container中的,所以不需要额外安装,只需要调用的时候传入环境变量${GITHUB_TOKEN}就好了。
所以删除了Install GitHub CLI和Authenticate to GitHub的步骤.
触发网站更新操作
在gh-page.yml中的on下添加workflow_run配置。
on:......workflow_run:workflows:- "issue-to-post"types:- completed
测试成功!
参考:https://docs.github.com/en/actions/using-workflows/using-github-cli-in-workflows
欢迎在ISSUE参与本博客讨论: m2kar/m2kar.github.io#14
如何把Issue博客自动同步到github page中相关推荐
- github博客自动同步到gitee(保姆级教程)
github博客自动同步到gitee(保姆级教程) 前言: 由于国内网访问github实在太慢!虽然在公司可以连接外网访问还算可以,但是回学校想看看博客或者分享给别人的时候经常会崩掉!于是我想可不可以 ...
- umi脚手架搭建的项目_15天零成本搭建静态博客,托管于Github Page
博客地址 技术栈概览 前台:Umi(路由) + Antd(视图) + TypeScript(增加项目可维护性以及规范性) 后台:Umi(路由) + Antd(视图) + TypeScript(增加项目 ...
- 使用GitHub Actions实现Hexo博客自动发布
我在使用jenkins实现hexo博客自动发布中介绍了可以使用jenkins实现博客自动发布,其实还有一种更简单的方法,就是GitHub提供的持续集成服务:GitHub Actions.下面就来介绍一 ...
- Atitit qzone qq空间博客自动点赞与评论工具的设计与实现
Atitit qzone qq空间博客自动点赞与评论工具的设计与实现 Qzone发送评论的原理 首先,有个a标签, <a class="c_tx3" href="j ...
- 我的博客即将同步至腾讯云+社区
我的博客即将同步至腾讯云+社区,邀请大家一同入驻:https://cloud.tencent.com/developer/support-plan?invite_code=wwbdyaw5zwxa
- 第一个Python程序——博客自动访问脚本
第一个Python程序--博客自动访问脚本 动机 今天有朋友写信说他认为自己的wordpress博客内显示的访问统计信息不正常,希望我能为他制造一些访问信息,供他对比.朋友提出的请求是在短时间内快速打 ...
- 网页php自描述,一段代码让wordpress博客自动添加关键词(keywords)及网页描述(description)...
让wordpress博客自动添加关键词(keywords)及网页描述(description) 打开你的模板文件wp-content/themes/模板目录文件/header.php 在头部添加 // ...
- 自己做了一款新浪博客自动访客工具
自己做了一款新浪博客自动访客工具. 思路非常简单,从某个博客出发,首先读取,这个博客所有的访客,然后依次访问这些访客的博客,以先进现出的方式,遍历所有的博客.在访问的博客上留下足迹,希望被访问的博客回 ...
- 我的博客即将同步至腾讯云开发者社区,邀请大家一同入驻:https://cloud.tencent.com/developer/support-plan?invite_code=61c74kbnuf9n
我的博客即将同步至腾讯云开发者社区,邀请大家一同入驻:https://cloud.tencent.com/developer/support-plan?invite_code=61c74kbnuf9n
最新文章
- pytorch.range() 和 pytorch.arange() 的区别
- Redhat7 systemctl命令
- OGEngine_粒子效果
- Bad Hair Day(POJ-3250)
- 潜龙号开启水下机器人_国内首个智能绞吸机器人开展水下取土作业
- 21天战拖记——Day6:复习阶段性完成!(2014-05-09)
- IEEE正推进制定量子计算标准
- 系统封装 如何为原生PE集成软件
- 私塾在线《深入浅出学 Hadoop- 初级 部分》
- Linux系统终于可以安装正版的QQ了
- Prisma(一):初识
- 更新Windows11出现错误0x80240022的解决方法
- Could not publish server configuration for Tomcat v8.0 Server at localhost.
- altium designer 10 的AD10Crakc.zip解压后没有patch.exe
- 云医在线服务器不可用,云医在线app
- 微信MAC最新版3.1.0支持发朋友圈了
- html5吴丰,HTML5+CSS3 Web前端设计基础教程
- 数据库的“读现象”浅析
- Android中各种dialog
- 即时通讯音视频开发(十八):详解音频编解码的原理、演进和应用选型