-
Notifications
You must be signed in to change notification settings - Fork 1
100 lines (82 loc) · 2.75 KB
/
build.yml
File metadata and controls
100 lines (82 loc) · 2.75 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
name: Sync Posts To Repo Blog
on:
workflow_dispatch:
push:
branches:
- master
jobs:
build:
name: Generate Target Posts
runs-on: ubuntu-22.04 # rsync --mkpath 在 ubuntu-22.04 才有这个参数,故选择这个稳定版本
steps:
# 检出 Git 仓库
- name: Checkout repository
uses: actions/checkout@v3
# 安装 jq 工具
- name: Install jq
run: sudo apt-get install jq
# 构建,根据 map.json 拷贝文件,路径都是都对项目根目录的路径
- name: Copy files to directory public
run: |
# 启用错误处理
set -e
# 处理 rsync 错误
function handle_error
{
echo "Rsync failed with exit code $1."
exit 1
}
# 执行 rsync 命令
cat .github/workflows/file-mapping.json | jq -r 'to_entries[] | "rsync --mkpath \(.key) \(.value)"' | while read -r command; do
echo "Executing: $command"
eval "$command"
if [ $? -ne 0 ]; then
handle_error $?
fi
done
echo "Files copied successfully."
# 上传构建产物
- name: Upload artifacts
# 链接 https://github.com/actions/upload-artifact/
uses: actions/upload-artifact@v4
with:
name: _posts
path: public
include-hidden-files: true
if-no-files-found: error
# 设置为两个串行作业,个人认为这两个作业没必要完全绑成一个作业,为了不相互干扰和影响
sync:
name: Sync Directory Posts
runs-on: ubuntu-latest
needs: build # 表示依赖于 build 作业完成
steps:
# 下载构建产物
- name: Download artifact
# 链接 https://github.com/actions/download-artifact
uses: actions/download-artifact@v4
with:
name: _posts
path: public
# 创建 repo blog 的目录
- name: Create directory for repo blog
run: |
mkdir -p blog
# 签出 blog 的 master 分支
- name: Checkout repo blog
uses: actions/checkout@v4.1.1
with:
repository: DavidingPlus/blog
ref: master
path: blog
token: ${{ secrets.PERSONAL_ACCESS_TOKEN }}
# 进行同步
- name: Sync posts to source/_posts
run: |
rm -rf blog/source/_posts/*
cp -r public/* blog/source/_posts/
cd blog/
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
git add source/_posts/
git commit -m "Sync posts from repo https://github.com/DavidingPlus/study-notes"
git push -u origin master