Skip to content

Commit 91fce6e

Browse files
committed
Add PR previews
1 parent 4731946 commit 91fce6e

File tree

7 files changed

+110
-4
lines changed

7 files changed

+110
-4
lines changed

.dockerignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/node_modules/
2+
/public/

.github/workflows/deploy.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ jobs:
99
build-and-deploy:
1010
runs-on: ubuntu-latest
1111
steps:
12-
- name: Checkout
12+
- name: Checkout code
1313
uses: actions/checkout@v4
1414
with:
1515
submodules: recursive

.github/workflows/preview.yml

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
name: Deploy PR Preview to VPS
2+
3+
on:
4+
pull_request:
5+
branches: [ "main" ]
6+
7+
jobs:
8+
build-and-deploy-preview:
9+
runs-on: ubuntu-latest
10+
steps:
11+
- name: Checkout code
12+
uses: actions/checkout@v4
13+
with:
14+
submodules: recursive
15+
16+
- name: Set Padded PR Number
17+
id: padded_number
18+
run: echo "PADDED_NUMBER=$(printf "%03d" ${{ github.event.number }})" >> $GITHUB_ENV
19+
20+
- name: Build Docker image for PR
21+
run: |
22+
docker build --build-arg HUGO_BASEURL="https://frc.mahoney.best/pr/${{ env.PADDED_NUMBER }}" -t tvhsfrc-preview:${{ env.PADDED_NUMBER }} .
23+
24+
- name: Save Docker image to file
25+
run: |
26+
docker save tvhsfrc-preview:${{ env.PADDED_NUMBER }} -o image-preview.tar
27+
chmod 644 image-preview.tar
28+
29+
- name: Upload Docker image to VPS
30+
uses: appleboy/[email protected]
31+
with:
32+
host: ${{ secrets.VPS_HOST }}
33+
username: ${{ secrets.VPS_USERNAME }}
34+
key: ${{ secrets.VPS_SSH_KEY }}
35+
port: ${{ secrets.VPS_PORT }}
36+
source: ./image-preview.tar
37+
target: ~/tvhsfrc-preview-${{ env.PADDED_NUMBER }}
38+
39+
- name: Deploy Docker container on VPS
40+
uses: appleboy/[email protected]
41+
with:
42+
host: ${{ secrets.VPS_HOST }}
43+
username: ${{ secrets.VPS_USERNAME }}
44+
key: ${{ secrets.VPS_SSH_KEY }}
45+
port: ${{ secrets.VPS_PORT }}
46+
script: |
47+
docker load -i ~/tvhsfrc-preview-${{ env.PADDED_NUMBER }}/image-preview.tar
48+
docker stop tvhsfrc-preview-${{ env.PADDED_NUMBER }} || true
49+
docker rm tvhsfrc-preview-${{ env.PADDED_NUMBER }} || true
50+
docker run -d --name tvhsfrc-preview-${{ env.PADDED_NUMBER }} -p $((9000 + ${{ github.event.number }})):80 tvhsfrc-preview:${{ env.PADDED_NUMBER }}
51+
52+
- name: Comment on PR with Preview URL
53+
uses: actions/github-script@v6
54+
with:
55+
script: |
56+
const prNumber = context.payload.pull_request.number;
57+
const paddedNumber = process.env.PADDED_NUMBER;
58+
const previewUrl = `https://frc.mahoney.best/pr/${paddedNumber}/`;
59+
github.rest.issues.createComment({
60+
issue_number: prNumber,
61+
owner: context.repo.owner,
62+
repo: context.repo.repo,
63+
body: `🚀 Your PR preview has been deployed! [View it here!](${previewUrl})`
64+
});

.github/workflows/remove-preview.yml

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
name: Remove PR Preview from VPS
2+
3+
on:
4+
pull_request:
5+
types: [closed]
6+
7+
jobs:
8+
remove-preview:
9+
runs-on: ubuntu-latest
10+
11+
steps:
12+
- name: Set Padded PR Number
13+
id: padded_number
14+
run: echo "PADDED_NUMBER=$(printf "%03d" ${{ github.event.number }})" >> $GITHUB_ENV
15+
16+
- name: Remove Docker container for PR
17+
uses: appleboy/[email protected]
18+
with:
19+
host: ${{ secrets.VPS_HOST }}
20+
username: ${{ secrets.VPS_USERNAME }}
21+
key: ${{ secrets.VPS_SSH_KEY }}
22+
port: ${{ secrets.VPS_PORT }}
23+
script: |
24+
docker stop tvhsfrc-preview-${{ env.PADDED_NUMBER }} || true
25+
docker system prune -f

Dockerfile

+16-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,22 @@ RUN npm install -g postcss postcss-cli autoprefixer
77
WORKDIR /app
88
COPY . .
99

10-
RUN hugo --environment production
10+
# Hugo base url
11+
ARG HUGO_BASEURL="https://tvhsfrc.org"
12+
13+
RUN HUGO_BASEURL=$HUGO_BASEURL hugo --environment production
14+
15+
RUN find public -type f -name "*.html" -exec sed -i "s|src=.*/img/dragons.png|src=\"/img/dragons.png|g" {} \;
16+
RUN find public -type f -name "*.html" -exec sed -i "s|src=.*/img/logo.png|src=\"/img/logo.png|g" {} \;
17+
RUN find public -type f -name "*.html" -exec sed -i "s|class=\"logo-link\" href=\".*\"|class=\"logo-link\" href=\"$HUGO_BASEURL\"|g" {} \;
18+
19+
RUN find public -type f -name "*.html" -exec sed -i "s|src=\"/img|src=\"$HUGO_BASEURL/img|g" {} \;
20+
RUN find public -type f -name "*.html" -exec sed -i "s|href=\"/css|href=\"$HUGO_BASEURL/css|g" {} \;
21+
RUN find public -type f -name "*.html" -exec sed -i "s|src=\"/js|src=\"$HUGO_BASEURL/js|g" {} \;
22+
RUN find public -type f -name "*.html" -exec sed -i "s|href=\"/fonts|href=\"$HUGO_BASEURL/fonts|g" {} \;
23+
24+
RUN find public -type f -name "*.css" -exec sed -i "s|url(/img|url($HUGO_BASEURL/img|g" {} \;
25+
RUN find public -type f -name "*.css" -exec sed -i "s|url(/fonts|url($HUGO_BASEURL/fonts|g" {} \;
1126

1227
FROM nginx:alpine
1328

config/development/hugo.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
######################## development configuration ####################
22
# The base URL of your site (required). This will be prepended to all relative URLs.
3-
baseURL: http://localhost:1313/
3+
baseURL:
44

hugo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
baseURL = 'https://frc.mahoney.best/'
1+
baseURL = 'https://tvhsfrc.org/'
22
languageCode = 'en-us'
33
title = 'TVHS Dragons : FRC 5881'
44
theme = 'dot-org-hugo-theme'

0 commit comments

Comments
 (0)