-
Notifications
You must be signed in to change notification settings - Fork 0
140 lines (121 loc) · 4.48 KB
/
auto-add-id.yml
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
name: "Auto Add Post ID"
on:
workflow_dispatch:
push:
branches:
- main
paths:
- "content/posts/**.md"
pull_request:
branches:
- main
paths:
- "content/posts/**.md"
env:
repo: mildronize/blog-v8
working_directory: "tmp-site"
snippets_directory: "snippets"
# Version of Zola available in GitHub releases.
# Ref: https://www.getzola.org/documentation/deployment/gitlab-pages/
ZOLA_VERSION: "0.19.2"
jobs:
add-post-id:
name: Add Post ID
runs-on: ubuntu-latest
steps:
- name: Determine working branch name
run: |
if [[ "${{ github.event_name }}" == "pull_request" ]]; then
echo "BRANCH_NAME=${{ github.event.pull_request.head.ref }}" >> $GITHUB_ENV
elif [[ "${{ github.event_name }}" == "push" ]]; then
echo "BRANCH_NAME=${{ github.ref_name }}" >> $GITHUB_ENV
else
echo "Only push and pull_request events are supported"
exit 1
fi
- name: Setup branch name
run: |
echo "new_branch_name=$BRANCH_NAME.auto-add-post-id-$(date +%s)" >> $GITHUB_ENV
shell: bash
- name: Clone Project
run: |
git clone https://${{ secrets.GH_TOKEN }}@github.com/${{ env.repo }} ${{ env.working_directory }}
cd ${{ env.working_directory }} && git submodule update --init --recursive
- name: Checkout Branch
run: git checkout ${{ env.BRANCH_NAME }}
working-directory: ${{ env.working_directory }}
- uses: oven-sh/setup-bun@v2
- name: Install Zola
run: |
zola_url="https://github.com/getzola/zola/releases/download/v$ZOLA_VERSION/zola-v$ZOLA_VERSION-x86_64-unknown-linux-gnu.tar.gz"
if ! wget --quiet --spider $zola_url; then
echo "A Zola release with the specified version could not be found.";
exit 1;
fi
wget $zola_url
tar -xzf *.tar.gz
- name: Install Scripts Dependencies
run: bun install
working-directory: ${{ env.working_directory }}/${{ env.snippets_directory }}
- name: Add Post ID
id: add-post-id
run: |
bun run add-id
working-directory: ${{ env.working_directory }}/${{ env.snippets_directory }}
- name: Setup Git Message & Pull Request Title
run: |
echo "git_message=content: ${{ steps.add-post-id.outputs.added-id-message }}" >> $GITHUB_ENV
shell: bash
- name: Zola Build
run: |
../zola build
working-directory: ${{ env.working_directory }}
- name: Setup Git
run: |
git config --global user.email "${{ env.git_email }}"
git config --global user.name "${{ env.git_name }}"
git checkout -b ${{ env.new_branch_name }}
working-directory: ${{ env.working_directory }}
env:
git_email: [email protected]
git_name: Add Post ID
- name: Commit Change & Push
if: steps.add-post-id.outputs.added-id-is-updated == 'true'
run: |
git add --all content
git restore --staged snippets/bun.lockb
git diff --quiet && git diff --staged --quiet || git commit -am "${{ env.git_message }}"
git push --set-upstream origin ${{ env.new_branch_name }}
working-directory: ${{ env.working_directory }}
- name: Create Pull Request & Merge if possible
if: steps.add-post-id.outputs.added-id-is-updated == 'true'
run: |
gh pr create \
--title "${{ env.git_message }}" \
--body "${{ steps.add-post-id.outputs.added-id-details }}" \
--base ${{ env.BRANCH_NAME }} \
--head ${{ env.new_branch_name }} \
--repo "${{ env.repo }}"
retry=1
while [ $retry -lt 10 ]
do
pr_number=`gh pr view ${{ env.new_branch_name }} --repo "${{ env.repo }}" --json number | jq '.number'`
if [ "$pr_number" != "" ]; then
break
fi
echo "Retry to get PR number... ($retry)"
sleep 3
retry=$(( $retry + 1 ))
done
if [ $retry -eq 10 ]
then
echo "Exceed limit retry"
exit 1
fi
echo "PR Number: $pr_number"
gh pr merge $pr_number \
--merge \
--delete-branch \
--repo ${{ env.repo }}
env:
GITHUB_TOKEN: ${{ secrets.GH_TOKEN }}