Skip to content

Commit f64cb6b

Browse files
committed
ci(hugo-deploy): use hugo deploy to sync files with s3
1 parent 1d50d1c commit f64cb6b

File tree

4 files changed

+117
-203
lines changed

4 files changed

+117
-203
lines changed

.github/workflows/pr-build-preview.yml

Lines changed: 0 additions & 80 deletions
This file was deleted.
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
name: Trigger preview for PR
2+
3+
on:
4+
pull_request:
5+
types: [opened, synchronize, reopened]
6+
7+
permissions:
8+
contents: read
9+
10+
jobs:
11+
build-preview:
12+
runs-on: ubuntu-latest
13+
14+
steps:
15+
- name: Collect PR metadata
16+
run: |
17+
echo "${{ github.event.pull_request.number }}" > pr_num.txt
18+
echo "${{ github.event.pull_request.html_url }}" > pr_url.txt
19+
echo "${{ github.event.pull_request.head.ref }}" > pr_head_ref.txt
20+
echo "${{ github.event.pull_request.head.sha }}" > pr_head_sha.txt
21+
22+
- name: Debug PR metadata
23+
run: |
24+
set -euo pipefail
25+
26+
echo "=== PR metadata from files (if present) ==="
27+
for file in \
28+
pr_num.txt \
29+
pr_url.txt \
30+
pr_head_ref.txt \
31+
pr_head_sha.txt
32+
do
33+
if [ -f "$file" ]; then
34+
printf "%-18s: %s\n" "$file" "$(cat "$file")"
35+
else
36+
printf "%-18s: (missing)\n" "$file"
37+
fi
38+
done
39+
40+
- name: Upload PR metadata
41+
uses: actions/upload-artifact@v4
42+
with:
43+
name: pr-metadata
44+
path: |
45+
pr_num.txt
46+
pr_url.txt
47+
pr_head_ref.txt
48+
pr_head_sha.txt

.github/workflows/prw-deploy-preview.yml

Lines changed: 63 additions & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name: Deploy preview for PR
33
on:
44
workflow_run:
55
workflows:
6-
- "Build preview for PR"
6+
- "Trigger preview for PR"
77
types:
88
- completed
99

@@ -17,171 +17,108 @@ concurrency:
1717
cancel-in-progress: false
1818

1919
env:
20-
HUGO_BASEURL: "https://preview-developer.espressif.com/"
21-
AWS_S3_BUCKET: ${{ secrets.PREVIEW_AWS_BUCKET_NAME }}
22-
AWS_REGION: ${{ secrets.AWS_REGION }}
23-
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
24-
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
20+
PREVIEW_BASE: https://preview-developer.espressif.com/
2521

2622
jobs:
27-
determine-sync-status:
23+
deploy-preview:
2824
runs-on: ubuntu-latest
29-
outputs:
30-
pr-number: ${{ steps.read-pr-num.outputs.PR_NUMBER }}
31-
sync-status: ${{ steps.check-checksums-s3.outputs.SYNC_STATUS }}
3225
if: >
3326
github.event.workflow_run.event == 'pull_request' &&
3427
github.event.workflow_run.conclusion == 'success'
3528
3629
steps:
37-
- name: Download artifacts (PR number file)
30+
- name: Download PR number artifact
3831
uses: actions/download-artifact@v4
3932
with:
40-
name: pr-num
33+
name: pr-metadata
4134
path: ./
4235
run-id: ${{ github.event.workflow_run.id }}
4336
github-token: ${{ secrets.GITHUB_TOKEN }}
4437

4538
- name: Read PR number from file
46-
id: read-pr-num
4739
run: |
48-
echo "PR_NUMBER=$(cat pr-num.txt)" >> $GITHUB_OUTPUT
49-
50-
- name: Check if checksums-s3.txt is in S3 bucket
51-
id: check-checksums-s3
52-
run: |
53-
if aws s3 ls s3://${{ secrets.PREVIEW_AWS_BUCKET_NAME }}/pr$(cat pr-num.txt)/checksums-s3.txt; then
54-
echo "SYNC_STATUS=update" >> $GITHUB_OUTPUT
55-
else
56-
echo "SYNC_STATUS=replace" >> $GITHUB_OUTPUT
57-
fi
58-
59-
replace-files:
60-
runs-on: ubuntu-latest
61-
needs: determine-sync-status
62-
if: ${{ needs.determine-sync-status.outputs.sync-status == 'replace' }}
63-
64-
steps:
65-
- name: Download artifacts (Public folder)
66-
uses: actions/download-artifact@v4
40+
PR_NUMBER="$(cat pr_num.txt)"
41+
PR_URL="$(cat pr_url.txt)"
42+
PR_REF="$(cat pr_head_ref.txt)"
43+
PR_SHA="$(cat pr_head_sha.txt)"
44+
45+
echo "PR_NUMBER=$PR_NUMBER" >> "$GITHUB_ENV"
46+
echo "HUGO_BASEURL=${PREVIEW_BASE}pr${PR_NUMBER}/" >> "$GITHUB_ENV"
47+
echo "PR_URL=$PR_URL" >> "$GITHUB_ENV"
48+
echo "PR_LINK_TEXT=${PR_URL#https://}" >> "$GITHUB_ENV"
49+
echo "PR_REF=$PR_REF" >> "$GITHUB_ENV"
50+
echo "PR_SHA=$PR_SHA" >> "$GITHUB_ENV"
51+
52+
- name: Check out PR commit with submodules
53+
uses: actions/checkout@v4
6754
with:
68-
name: public-folder
69-
path: ./public
70-
run-id: ${{ github.event.workflow_run.id }}
71-
github-token: ${{ secrets.GITHUB_TOKEN }}
55+
ref: ${{ env.PR_SHA }}
56+
submodules: recursive
57+
fetch-depth: 0
7258

73-
- name: Sync public folder with S3 bucket
74-
run: |
75-
aws s3 sync "$SOURCE_DIR" "$DEST_DIR" --follow-symlinks --delete --cache-control no-cache
59+
- name: Install Hugo
7660
env:
77-
SOURCE_DIR: './public'
78-
DEST_DIR: "s3://${{ secrets.PREVIEW_AWS_BUCKET_NAME }}/pr${{ needs.determine-sync-status.outputs.pr-number }}"
79-
80-
- name: Download checksums-ci.txt from artifacts
81-
uses: actions/download-artifact@v4
82-
with:
83-
name: checksums-ci
84-
path: ./
85-
run-id: ${{ github.event.workflow_run.id }}
86-
github-token: ${{ secrets.GITHUB_TOKEN }}
87-
88-
- name: Copy checksums-ci.txt to s3 bucket
61+
HUGO_VERSION: 0.152.2
8962
run: |
90-
aws s3 cp ./checksums-ci.txt s3://${{ secrets.PREVIEW_AWS_BUCKET_NAME }}/pr${{ needs.determine-sync-status.outputs.pr-number }}/checksums-s3.txt
91-
92-
update-files:
93-
runs-on: ubuntu-latest
94-
needs: determine-sync-status
95-
if: ${{ needs.determine-sync-status.outputs.sync-status == 'update' }}
63+
wget --progress=dot:giga -O ${{ runner.temp }}/hugo.deb https://github.com/gohugoio/hugo/releases/download/v${HUGO_VERSION}/hugo_extended_withdeploy_${HUGO_VERSION}_linux-amd64.deb \
64+
&& sudo dpkg -i ${{ runner.temp }}/hugo.deb
9665
97-
steps:
98-
- name: Download checksums-s3.txt from S3 bucket
66+
- name: Rewrite and verify Hugo deployment URL for PR
9967
run: |
100-
aws s3 cp s3://${{ secrets.PREVIEW_AWS_BUCKET_NAME }}/pr${{ needs.determine-sync-status.outputs.pr-number }}/checksums-s3.txt ./checksums-s3.txt
101-
102-
- name: Download checksums-ci.txt from artifacts
103-
uses: actions/download-artifact@v4
104-
with:
105-
name: checksums-ci
106-
path: ./
107-
run-id: ${{ github.event.workflow_run.id }}
108-
github-token: ${{ secrets.GITHUB_TOKEN }}
68+
# Rewrite the prefix in hugo.toml
69+
sed -i "s|prefix=subdir/|prefix=pr$PR_NUMBER/|g" config/_default/hugo.toml
70+
echo "The updated deployment URL for PR:"
71+
grep '\&prefix=' config/_default/hugo.toml
10972
110-
- name: Download artifacts (Public folder)
111-
uses: actions/download-artifact@v4
112-
with:
113-
name: public-folder
114-
path: ./public
115-
run-id: ${{ github.event.workflow_run.id }}
116-
github-token: ${{ secrets.GITHUB_TOKEN }}
117-
118-
- name: Compare checksums and update public-update folder
119-
run: |
120-
mkdir -p ./public-update
121-
122-
# Find outdated files and remove them (unique checksums in checksums-s3.txt)
123-
comm -23 checksums-s3.txt checksums-ci.txt | awk '{print $1}' > outdated-files.txt
124-
if [ -s outdated-files.txt ]; then
125-
echo "Removing outdated files from S3:"
126-
cat outdated-files.txt
127-
for file in $(cat outdated-files.txt); do
128-
aws s3 rm s3://${{ secrets.PREVIEW_AWS_BUCKET_NAME }}/pr${{ needs.determine-sync-status.outputs.pr-number }}/$file > /dev/null 2>&1
129-
done
130-
fi
131-
132-
# Copy updated files to public-update (unique checksums in checksums-ci.txt)
133-
comm -13 checksums-s3.txt checksums-ci.txt | awk '{print $1}' > updated-files.txt
134-
if [ -s updated-files.txt ]; then
135-
echo "Copying updated files to public-update folder:"
136-
cat updated-files.txt
137-
for file in $(cat updated-files.txt); do
138-
mkdir -p ./public-update/$(dirname "$file")
139-
cp ./public/$file ./public-update/$file
140-
done
141-
fi
142-
143-
- name: Sync public-update with S3 bucket
73+
- name: Add preview banner to website
14474
run: |
145-
aws s3 sync "$SOURCE_DIR" "$DEST_DIR" --follow-symlinks --cache-control no-cache
146-
env:
147-
SOURCE_DIR: './public-update'
148-
DEST_DIR: "s3://${{ secrets.PREVIEW_AWS_BUCKET_NAME }}/pr${{ needs.determine-sync-status.outputs.pr-number }}"
75+
PREVIEW_HTML="<p style='color: rgba(var(--color-primary-600), 1); text-align: center;'>
76+
This site is a preview for <a href='$PR_URL' style='text-decoration: underline;'>$PR_LINK_TEXT</a>
77+
</p>"
78+
echo "$PREVIEW_HTML" >> layouts/partials/header/basic.html
14979
150-
- name: Copy checksums-ci.txt to s3 bucket
80+
- name: Build website with Hugo
81+
env:
82+
# For maximum backward compatibility with Hugo modules
83+
HUGO_ENVIRONMENT: preview
84+
HUGO_ENV: preview
15185
run: |
152-
aws s3 cp ./checksums-ci.txt s3://${{ secrets.PREVIEW_AWS_BUCKET_NAME }}/pr${{ needs.determine-sync-status.outputs.pr-number }}/checksums-s3.txt
86+
hugo \
87+
--baseURL "$HUGO_BASEURL" \
88+
--gc \
89+
--minify
15390
154-
notifications-and-cleanup:
155-
runs-on: ubuntu-latest
156-
needs: [determine-sync-status, replace-files, update-files]
157-
if: |
158-
always()
159-
&& contains(needs.*.result, 'success')
160-
&& !contains(needs.*.result, 'failure')
91+
- name: Deploy preview with Hugo
92+
env:
93+
# AWS_S3_BUCKET: ${{ secrets.PREVIEW_AWS_BUCKET_NAME }}
94+
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
95+
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
96+
AWS_REGION: ${{ secrets.AWS_REGION }}
97+
run: |
98+
hugo deploy \
99+
--target=preview
161100
162-
steps:
163101
- name: Post Preview Link to PR
164102
uses: actions/github-script@v7
165103
with:
166104
github-token: ${{ secrets.GITHUB_TOKEN }}
167105
script: |
168106
try {
169107
const { data: comments } = await github.rest.issues.listComments({
170-
issue_number: ${{ needs.determine-sync-status.outputs.pr-number }},
108+
issue_number: process.env.PR_NUMBER,
171109
owner: context.repo.owner,
172110
repo: context.repo.repo
173111
});
174112
175113
// Define the comment body
176-
const commentBody = `🎉 A preview for this PR is available at: ${{ env.HUGO_BASEURL }}pr${{ needs.determine-sync-status.outputs.pr-number }}/`;
114+
const commentBody = `🎉 A preview for this PR is available at: ${process.env.HUGO_BASEURL}`;
177115
178116
// Look for an existing comment containing the specific text
179117
const existingComment = comments.find(comment =>
180118
comment.body.includes("🎉 A preview for this PR is available at:")
181119
);
182120
183121
if (existingComment) {
184-
// Delete the existing comment
185122
await github.rest.issues.deleteComment({
186123
comment_id: existingComment.id,
187124
owner: context.repo.owner,
@@ -191,7 +128,7 @@ jobs:
191128
192129
// Create a new comment
193130
await github.rest.issues.createComment({
194-
issue_number: ${{ needs.determine-sync-status.outputs.pr-number }},
131+
issue_number: process.env.PR_NUMBER,
195132
owner: context.repo.owner,
196133
repo: context.repo.repo,
197134
body: commentBody
@@ -201,10 +138,13 @@ jobs:
201138
}
202139
203140
- name: Invalidate CloudFront cache for PR
204-
uses: chetan/invalidate-cloudfront-action@v2
205141
env:
206-
PATHS: "/pr${{ needs.determine-sync-status.outputs.pr-number }}/*"
207-
DISTRIBUTION: ${{ secrets.PREVIEW_CLOUDFRONT_DISTRIBUTION }}
142+
DISTRIBUTION_ID: ${{ secrets.PREVIEW_CLOUDFRONT_DISTRIBUTION }}
208143
AWS_REGION: ${{ secrets.AWS_REGION }}
209144
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
210145
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
146+
run: |
147+
echo "Invalidating CloudFront paths for PR $PR_NUMBER..."
148+
aws cloudfront create-invalidation \
149+
--distribution-id "$DISTRIBUTION_ID" \
150+
--paths "/pr$PR_NUMBER/*"

0 commit comments

Comments
 (0)