-
Notifications
You must be signed in to change notification settings - Fork 12
123 lines (114 loc) · 3.87 KB
/
pr-preview.yml
File metadata and controls
123 lines (114 loc) · 3.87 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# Workflow for building and deploying a preview of the Hugo site for a pull request
name: Deploy Preview
on:
# Runs on pull requests targeting the main branch
pull_request_target:
branches:
- main
# Sets permissions of the GitHub TOKEN to allow deployment to GitHub Pages
permissions:
contents: read
pages: write
id-token: write
# Allow one concurrent deployment per pull request, and cancel in-progress runs.
concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
cancel-in-progress: true
# Default to bash
defaults:
run:
shell: bash
jobs:
# Build job
build:
runs-on: ubuntu-latest
outputs:
preview_url: ${{ steps.base_url.outputs.base_url }}
env:
HUGO_VERSION: 0.148.2
steps:
- name: Install Hugo CLI
run: |
wget -O ${{ runner.temp }}/hugo.deb https://github.com/gohugoio/hugo/releases/download/v${HUGO_VERSION}/hugo_extended_${HUGO_VERSION}_linux-amd64.deb \
&& sudo dpkg -i ${{ runner.temp }}/hugo.deb
- name: Install Dart Sass
env:
DART_SASS_VERSION: 1.83.4
run: |
wget -O /tmp/dart-sass.tar.gz \
"https://github.com/sass/dart-sass/releases/download/${DART_SASS_VERSION}/dart-sass-${DART_SASS_VERSION}-linux-x64.tar.gz"
tar -xf /tmp/dart-sass.tar.gz -C /tmp
echo "/tmp/dart-sass" >> $GITHUB_PATH
- name: Setup Pages
id: pages
uses: actions/configure-pages@v5
# Build production site from base branch
- name: Checkout base branch
uses: actions/checkout@v4
with:
ref: ${{ github.base_ref }}
path: base
submodules: recursive
- name: Install Node.js dependencies for base
working-directory: base
run: "[[ -f package-lock.json || -f npm-shrinkwrap.json ]] && npm ci || true"
- name: Build production site
env:
HUGO_ENVIRONMENT: production
HUGO_ENV: production
run: |
hugo \
--source base \
--minify \
--baseURL "${{ steps.pages.outputs.base_url }}/" \
--destination "${{ github.workspace }}/public"
# Build PR preview
- name: Checkout PR branch
uses: actions/checkout@v4
with:
ref: ${{ github.event.pull_request.head.sha }}
path: pr
submodules: recursive
- name: Install Node.js dependencies for PR
working-directory: pr
run: "[[ -f package-lock.json || -f npm-shrinkwrap.json ]] && npm ci || true"
- name: Set preview baseURL
id: base_url
run: echo "base_url=${{ steps.pages.outputs.base_url }}/pr-${{ github.event.number }}" >> $GITHUB_OUTPUT
- name: Build PR preview
env:
HUGO_ENVIRONMENT: production
HUGO_ENV: production
run: |
hugo \
--source pr \
--minify \
--baseURL "${{ steps.base_url.outputs.base_url }}/" \
--destination "${{ github.workspace }}/public/pr-${{ github.event.number }}"
- name: Upload artifact
uses: actions/upload-pages-artifact@v3
with:
path: ./public
# Deployment job
deploy:
environment:
name: github-pages
url: ${{ needs.build.outputs.preview_url }}
runs-on: ubuntu-latest
needs: build
steps:
- name: Deploy to GitHub Pages
id: deployment
uses: actions/deploy-pages@v4
add-comment:
runs-on: ubuntu-latest
needs: [build, deploy]
permissions:
pull-requests: write
steps:
- name: Add preview URL to PR
uses: peter-evans/create-or-update-comment@v4
with:
issue-number: ${{ github.event.pull_request.number }}
body: | # This is a multiline string, so newlines are preserved correctly.
Preview deployed at: ${{ needs.build.outputs.preview_url }}