Sync and Deploy Workflow #138
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Sync and Deploy Workflow | |
on: | |
schedule: | |
- cron: '0 17 * * *' # 每天晚上1点执行(北京时间) | |
pull_request: | |
types: [ closed ] | |
# 允许你从 Actions 选项卡手动运行此工作流程 | |
workflow_dispatch: | |
permissions: | |
contents: write | |
pages: write | |
id-token: write | |
pull-requests: write # 新增 pull-requests 权限 | |
concurrency: | |
group: sync_and_deploy | |
cancel-in-progress: false | |
jobs: | |
sync-and-deploy: | |
if: github.event.pull_request.base.ref == 'blog/sync' || github.event.pull_request.base.ref == 'master' || github.event_name == 'schedule' | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout | |
uses: actions/checkout@v4 | |
with: | |
fetch-depth: 0 | |
- name: Install jq | |
run: | | |
sudo apt-get update | |
sudo apt-get install -y jq | |
- name: Install GitHub CLI | |
run: | | |
sudo apt-get update | |
curl -fsSL https://cli.github.com/packages/githubcli-archive-keyring.gpg | sudo dd of=/usr/share/keyrings/githubcli-archive-keyring.gpg | |
sudo apt-get install -y apt-transport-https | |
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main" | sudo tee /etc/apt/sources.list.d/github-cli.list > /dev/null | |
sudo apt-get update | |
sudo apt-get install -y gh | |
- name: Setup Node | |
uses: actions/setup-node@v4 | |
- name: Download and unzip repository | |
run: | | |
curl -L https://github.com/monocytess/simple/archive/refs/heads/master.zip > repo.zip | |
unzip repo.zip | |
rm repo.zip | |
- name: Sync files | |
run: | | |
rsync -av --checksum --delete simple-master/src/js30 docs/public/pages/ | |
rm -rf simple-master | |
- name: Modify Html Script files | |
run: | | |
# 查找 docs/public/pages/js30 目录下的所有子目录 | |
find docs/public/pages/js30 -mindepth 1 -type d -print0 | while IFS= read -r -d '' subdir; do | |
# 查找每个子目录中的所有名为 "index.html" 的文件 | |
find "$subdir" -type f -name "index.html" -print0 | while IFS= read -r -d '' file; do | |
sed -i'' -e ' | |
/<head>/,/<\/head>/ { | |
s|src="\.\./\.\./common|src="/assets/common|g; | |
} | |
' "$file" | |
done | |
done | |
- name: Run update script | |
run: | | |
# 安装必要的依赖 | |
npm install -g find | |
# 查找并处理HTML文件 | |
node update_html_files.js | |
- name: Modify Css files | |
run: | | |
find docs/public/pages/js30 -type f -name "*.css" -exec sed -i 's|@import "../../css/js30.css";|@import "/assets/css/js30.base.css";|g' {} \; | |
- name: Add CardLink to docs/index.md | |
run: | | |
# 检查<Flow>标签是否存在 | |
if grep -q '<Flow>' docs/index.md; then | |
# 保存原始的<Flow>标签内容 | |
grep -zoP '(?<=<Flow>).*?(?=</Flow>)' docs/index.md > flow_content.md || echo "" > flow_content.md | |
# 将新的CardLink追加到原始的<Flow>内容之后 | |
echo "" >> flow_content.md | |
for dir in docs/public/pages/js30/*; do | |
if [ -d "$dir" ]; then | |
folder_name=$(basename "$dir") | |
description=$(grep -oP '(?<=<meta name="description" content=").*?(?=">)' "$dir/index.html") | |
echo "<CardLink" >> flow_content.md | |
echo " icon=\"$(shuf -n 1 -e 🧭 🥁 🎸 🎹 🎺 🎻 🎷 🎤 🎧 🎥 🔉)\"" >> flow_content.md | |
echo " title=\"$folder_name\"" >> flow_content.md | |
echo " description=\"$description\"" >> flow_content.md | |
echo " href=\"/pages/js30/$folder_name/index.html\">" >> flow_content.md | |
echo "</CardLink>" >> flow_content.md | |
fi | |
done | |
echo "</Flow>" >> flow_content.md | |
# 去掉 <Flow> 与第一个 <CardLink> 之间的空格 | |
sed -i '/<Flow>/,/<CardLink>/ {/^\s*$/d}' flow_content.md | |
# 使用 `awk` 替换 index.md 中的 <Flow> 内容 | |
awk '{ | |
if ($0 ~ /<Flow>/) { | |
print $0; | |
while (getline line < "flow_content.md") { | |
print line | |
} | |
while ($0 !~ /<\/Flow>/) { | |
getline | |
} | |
} else { | |
print $0 | |
} | |
}' docs/index.md > docs/index_temp.md && mv docs/index_temp.md docs/index.md | |
else | |
echo "<Flow>标签未找到,无法进行修改" >> docs/index.md | |
fi | |
rm flow_content.md | |
- name: Remove empty lines between <Flow> and <CardLink> | |
run: | | |
sed -i '/<Flow>/,/<CardLink>/ {/^\s*$/d}' docs/index.md | |
- 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' | |
# 检查事件是否为Pull Request | |
if [ "$GITHUB_EVENT_NAME" = "pull_request" ]; then | |
# 获取Pull Request编号 | |
pr_number=$(jq -r '.pull_request.number' $GITHUB_EVENT_PATH) | |
# 创建新的分支名称 | |
branch_name="blog/sync-auto${pr_number}" | |
# 切换到新的分支 | |
git checkout -b $branch_name | |
fi | |
git add . | |
git commit -m "Automated changes by GitHub Actions" | |
# 如果是Pull Request,推送更改 | |
if [ "$GITHUB_EVENT_NAME" = "pull_request" ]; then | |
git push -u https://${GITHUB_TOKEN}@github.com/hviwen/hviwen.github.io.git HEAD:$branch_name -f | |
fi | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
- name: Create Pull Request | |
if: ${{ github.event_name == 'pull_request' }} | |
run: | | |
# 检查事件是否为Pull Request | |
if [ "$GITHUB_EVENT_NAME" = "pull_request" ]; then | |
# 获取Pull Request编号 | |
pr_number=$(jq -r '.pull_request.number' $GITHUB_EVENT_PATH) | |
# 创建新的分支名称 | |
branch_name="blog/sync-auto${pr_number}" | |
# 创建Pull Request | |
gh pr create --fill --base master --head $branch_name --title "Automated changes by GitHub Actions" --body "Automated changes by GitHub Actions" | |
fi | |
env: | |
GH_TOKEN: ${{ secrets.GH_PAT }} | |
GITHUB_REPOSITORY: ${{ github.repository }} |