Skip to content

Commit f853ed1

Browse files
authored
feat: add separate workflows to build and deploy preview pr (#359)
1 parent b0821d3 commit f853ed1

File tree

2 files changed

+60
-42
lines changed

2 files changed

+60
-42
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
name: Build 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: Install Hugo CLI
16+
env:
17+
HUGO_VERSION: 0.135.0
18+
run: |
19+
wget -O ${{ runner.temp }}/hugo.deb https://github.com/gohugoio/hugo/releases/download/v${HUGO_VERSION}/hugo_extended_${HUGO_VERSION}_linux-amd64.deb \
20+
&& sudo dpkg -i ${{ runner.temp }}/hugo.deb
21+
22+
- name: Check out repo
23+
uses: actions/checkout@v4
24+
with:
25+
submodules: recursive
26+
27+
- name: Build website with Hugo
28+
env:
29+
# For maximum backward compatibility with Hugo modules
30+
HUGO_ENVIRONMENT: preview
31+
HUGO_ENV: preview
32+
run: |
33+
hugo \
34+
--gc \
35+
--minify
36+
37+
- name: Upload artifacts
38+
uses: actions/upload-artifact@v3
39+
with:
40+
name: public-folder
41+
path: ./public

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

Lines changed: 19 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,36 @@
11
name: Deploy preview for PR
22

33
on:
4-
pull_request:
5-
types: [opened, synchronize, reopened]
6-
# Allow manual runs from the Actions tab
7-
workflow_dispatch:
4+
workflow_run:
5+
workflows: ["Build preview for PR"]
6+
types:
7+
- completed
88

9-
# Set permissions of the GITHUB_TOKEN
109
permissions:
1110
contents: read
1211
id-token: write
1312
pull-requests: write
1413

15-
# Allow only one concurrent deployment
1614
concurrency:
17-
group: ${{ github.workflow }}-${{ github.ref }}
15+
group: ${{ github.workflow }}-${{ github.event.workflow_run.head_branch }}
1816
cancel-in-progress: false
1917

20-
# Define environment variables
2118
env:
22-
HUGO_BASEURL: "https://preview-developer.espressif.com/pr${{ github.event.pull_request.number }}/"
23-
24-
# Default to bash
25-
defaults:
26-
run:
27-
shell: bash
19+
HUGO_BASEURL: "https://preview-developer.espressif.com/pr${{ github.event.workflow_run.pull_requests[0].number }}/"
2820

2921
jobs:
30-
build-and-deploy-preview:
22+
deploy-preview:
3123
runs-on: ubuntu-latest
24+
if: >
25+
github.event.workflow_run.event == 'pull_request' &&
26+
github.event.workflow_run.conclusion == 'success'
3227
3328
steps:
34-
35-
- name: Install Hugo CLI
36-
env:
37-
HUGO_VERSION: 0.135.0
38-
run: |
39-
wget -O ${{ runner.temp }}/hugo.deb https://github.com/gohugoio/hugo/releases/download/v${HUGO_VERSION}/hugo_extended_${HUGO_VERSION}_linux-amd64.deb \
40-
&& sudo dpkg -i ${{ runner.temp }}/hugo.deb
41-
42-
- name: Check out repo
43-
uses: actions/checkout@v4
29+
- name: Download artifacts
30+
uses: actions/download-artifact@v3
4431
with:
45-
submodules: recursive
46-
47-
- name: Build website with Hugo
48-
env:
49-
# For maximum backward compatibility with Hugo modules
50-
HUGO_ENVIRONMENT: preview
51-
HUGO_ENV: preview
52-
run: |
53-
hugo \
54-
--gc \
55-
--minify
32+
name: public-folder
33+
path: ./public
5634

5735
- name: Deploy to AWS S3 PR-specific subdirectory
5836
uses: jakejarvis/s3-sync-action@master
@@ -61,28 +39,27 @@ jobs:
6139
env:
6240
AWS_S3_BUCKET: ${{ secrets.PREVIEW_AWS_BUCKET_NAME }}
6341
SOURCE_DIR: './public'
64-
# Subdirectory for the PR
65-
DEST_DIR: "pr${{ github.event.pull_request.number }}"
42+
DEST_DIR: "pr${{ github.event.workflow_run.pull_requests[0].number }}"
6643
AWS_REGION: ${{ secrets.AWS_REGION }}
6744
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
6845
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
6946

7047
- name: Post Preview Link to PR
7148
uses: actions/github-script@v7
7249
with:
73-
github-token: ${{secrets.GITHUB_TOKEN}}
50+
github-token: ${{ secrets.GITHUB_TOKEN }}
7451
script: |
7552
github.rest.issues.createComment({
76-
issue_number: context.issue.number,
53+
issue_number: context.event.workflow_run.pull_requests[0].number,
7754
owner: context.repo.owner,
7855
repo: context.repo.repo,
79-
body: `🎉 A preview of this PR is available at: ${{ env.HUGO_BASEURL }} `
56+
body: `🎉 A preview of this PR is available at: ${{ env.HUGO_BASEURL }}`
8057
})
8158
8259
- name: Invalidate CloudFront cache for PR
8360
uses: chetan/invalidate-cloudfront-action@v2
8461
env:
85-
PATHS: "/pr-${{ github.event.pull_request.number }}/*"
62+
PATHS: "/pr-${{ github.event.workflow_run.pull_requests[0].number }}/*"
8663
DISTRIBUTION: ${{ secrets.PREVIEW_CLOUDFRONT_DISTRIBUTION }}
8764
AWS_REGION: ${{ secrets.AWS_REGION }}
8865
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}

0 commit comments

Comments
 (0)