-
-
Notifications
You must be signed in to change notification settings - Fork 59
209 lines (186 loc) · 8.4 KB
/
ci-build-site.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
name: build-site-and-compare
on:
push:
branches:
tags:
paths-ignore:
- 'publisher/**'
- 'updater/**'
- '.github/workflows/ci-build-publisher.yml'
- '.github/workflows/ci-build-updater.yml'
- '.github/workflows/ci-frontend-check.yml'
- '.github/workflows/ci-frontend-lint.yml'
- '.github/dependabot.yml'
- '.github/FUNDING.yml'
- 'hugo/content/**' # content likely won't break the build
pull_request:
paths-ignore:
- 'publisher/**'
- 'updater/**'
- '.github/workflows/ci-build-publisher.yml'
- '.github/workflows/ci-build-updater.yml'
- '.github/workflows/ci-frontend-check.yml'
- '.github/workflows/ci-frontend-lint.yml'
- '.github/dependabot.yml'
- '.github/FUNDING.yml'
- 'hugo/content/**' # content likely won't break the build
jobs:
build:
name: build-and-compare
runs-on: ubuntu-latest
steps:
- name: Check out code
uses: actions/checkout@v4
with:
fetch-depth: 0 # Needed to fetch all history for comparison
- name: Set directory names
id: set-dirs
run: |
if [[ "${{ github.event_name }}" == "pull_request" ]]; then
echo "old_dir=master" >> $GITHUB_OUTPUT
echo "new_dir=pr-${{ github.event.pull_request.number }}" >> $GITHUB_OUTPUT
else
echo "old_dir=old-master" >> $GITHUB_OUTPUT
echo "new_dir=new-master" >> $GITHUB_OUTPUT
fi
- name: Get comparison commit
id: get-comparison-commit
run: |
if [[ "${{ github.event_name }}" == "pull_request" ]]; then
echo "compare_sha=$(git merge-base ${{ github.event.pull_request.base.sha }} ${{ github.event.pull_request.head.sha }})" >> $GITHUB_OUTPUT
else
echo "compare_sha=$(git rev-parse HEAD^1)" >> $GITHUB_OUTPUT
fi
- name: Build previous version
run: |
git checkout ${{ steps.get-comparison-commit.outputs.compare_sha }}
docker build -t radio-t/site:previous .
# Create directories for previous build
mkdir -p ${{ steps.set-dirs.outputs.old_dir }}/hugo
cp -r hugo/* ${{ steps.set-dirs.outputs.old_dir }}/hugo/
mkdir -p ${{ steps.set-dirs.outputs.old_dir }}/public
# Run the container with proper volume mounts
docker run --rm \
-v ${{ github.workspace }}/${{ steps.set-dirs.outputs.old_dir }}/hugo:/srv/hugo \
-v ${{ github.workspace }}/${{ steps.set-dirs.outputs.old_dir }}/public:/srv/hugo/public \
radio-t/site:previous
- name: Build current version
run: |
git checkout ${{ github.event.pull_request.head.sha || github.sha }}
docker build -t radio-t/site:current .
# Create directories for current build
mkdir -p ${{ steps.set-dirs.outputs.new_dir }}/hugo
cp -r hugo/* ${{ steps.set-dirs.outputs.new_dir }}/hugo/
mkdir -p ${{ steps.set-dirs.outputs.new_dir }}/public
# Run the container with proper volume mounts
docker run --rm \
-v ${{ github.workspace }}/${{ steps.set-dirs.outputs.new_dir }}/hugo:/srv/hugo \
-v ${{ github.workspace }}/${{ steps.set-dirs.outputs.new_dir }}/public:/srv/hugo/public \
radio-t/site:current
- name: Generate diff
run: |
# Generate initial diff
diff -r -N -u ${{ steps.set-dirs.outputs.old_dir }}/public ${{ steps.set-dirs.outputs.new_dir }}/public > temp.diff || true
# Process diff file to adjust paths
sed -E 's#^(---|\+\+\+) (.+)/public/#\1 \2/#' temp.diff > changes.diff
# Store diff size for later use
diff_size=$(wc -c < changes.diff)
echo "diff_size=$diff_size" >> $GITHUB_ENV
# Generate a summary of changed files
echo "Changed files:" > summary.txt
find ${{ steps.set-dirs.outputs.new_dir }}/public -type f -exec sh -c '
old_dir="${{ steps.set-dirs.outputs.old_dir }}"
new_dir="${{ steps.set-dirs.outputs.new_dir }}"
file_path=${1#$new_dir/public/}
prev_file="$old_dir/public/$file_path"
if [ ! -f "$prev_file" ]; then
echo "New: $file_path"
elif ! cmp -s "$1" "$prev_file"; then
echo "Modified: $file_path"
fi
' sh {} \; >> summary.txt
find ${{ steps.set-dirs.outputs.old_dir }}/public -type f -exec sh -c '
old_dir="${{ steps.set-dirs.outputs.old_dir }}"
new_dir="${{ steps.set-dirs.outputs.new_dir }}"
file_path=${1#$old_dir/public/}
current_file="$new_dir/public/$file_path"
if [ ! -f "$current_file" ]; then
echo "Deleted: $file_path"
fi
' sh {} \; >> summary.txt
# Add size comparison
echo -e "\nSize comparison:" >> summary.txt
du -sh ${{ steps.set-dirs.outputs.old_dir }} >> summary.txt
du -sh ${{ steps.set-dirs.outputs.new_dir }} >> summary.txt
- name: Create Pastebin Paste with the diff
if: github.event_name == 'pull_request' && env.diff_size >= 20000
env:
PASTEBIN_API_KEY: ${{ secrets.PASTEBIN_API_KEY }}
id: create-pastebin
run: |
diff_content=$(cat changes.diff)
# https://pastebin.com/doc_api
paste_output=$(curl --silent "https://pastebin.com/api/api_post.php" \
-d "api_dev_key=$PASTEBIN_API_KEY" \
-d "api_option=paste" \
-d "api_paste_expire_date=1M" \
-d "api_paste_format=diff" \
-d "api_paste_private=0" \
-d "api_paste_code=${diff_content}" \
-d "api_paste_name=radio-t.com Site Changes for PR #${{ github.event.pull_request.number }}, commit ${{ github.sha }}")
# Check if the output is a valid URL, otherwise print error
if [[ "$paste_url" == "https://"* ]]; then
echo "PASTE_URL=$paste_output" >> $GITHUB_ENV
else
echo "Error: Failed to create Pastebin paste: $paste_output" >&2
fi
- name: Upload changes.diff as artifact
uses: actions/upload-artifact@v4
with:
name: changes.diff
path: changes.diff
- name: Generate comment body
id: get-comment-body
run: |
SUMMARY=$(cat summary.txt)
echo "body<<EOF" >> $GITHUB_OUTPUT
echo "### Site Build Comparison" >> $GITHUB_OUTPUT
echo "" >> $GITHUB_OUTPUT
echo "**Build completed at:** $(date -u '+%Y-%m-%d %H:%M:%S UTC')" >> $GITHUB_OUTPUT
echo "" >> $GITHUB_OUTPUT
echo "\`\`\`" >> $GITHUB_OUTPUT
echo "$SUMMARY" >> $GITHUB_OUTPUT
echo "\`\`\`" >> $GITHUB_OUTPUT
echo "" >> $GITHUB_OUTPUT
# If diff is small enough (<20KB), include it directly under spoiler
if [ "$diff_size" -lt 20000 ]; then
echo "**Full changes:**" >> $GITHUB_OUTPUT
echo "" >> $GITHUB_OUTPUT
echo "<details>" >> $GITHUB_OUTPUT
echo "<summary>Click to expand diff</summary>" >> $GITHUB_OUTPUT
echo "" >> $GITHUB_OUTPUT
echo "\`\`\`diff" >> $GITHUB_OUTPUT
cat changes.diff >> $GITHUB_OUTPUT
echo "\`\`\`" >> $GITHUB_OUTPUT
echo "</details>" >> $GITHUB_OUTPUT
fi
echo "[View full changes diff](${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID})" >> $GITHUB_OUTPUT
if [ -n "$PASTE_URL" ]; then
echo "[View diff on Pastebin]($PASTE_URL)" >> $GITHUB_OUTPUT
fi
echo "EOF" >> $GITHUB_OUTPUT
- name: Find existing comment
id: find-comment
if: github.event_name == 'pull_request'
uses: peter-evans/find-comment@v3
with:
issue-number: ${{ github.event.pull_request.number }}
body-includes: "### Site Build Comparison"
- name: Create or update comment
if: github.event_name == 'pull_request'
uses: peter-evans/create-or-update-comment@v4
with:
comment-id: ${{ steps.find-comment.outputs.comment-id }}
issue-number: ${{ github.event.pull_request.number }}
body: ${{ steps.get-comment-body.outputs.body }}
edit-mode: replace