Skip to content

Commit 86d48a4

Browse files
cleanup and simple
Signed-off-by: saurabhraghuvanshii <saurabhsraghuvanshi@gmail.com>
1 parent f31f601 commit 86d48a4

File tree

2 files changed

+17
-110
lines changed

2 files changed

+17
-110
lines changed

.github/workflows/build-and-deploy-site.yml

Lines changed: 6 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
1-
name: Build and Deploy Site
1+
name: Deploy Gatsby site to Pages
22

33
on:
44
push:
55
branches: [master]
6-
paths-ignore:
7-
- .github/*
8-
96
workflow_dispatch:
10-
workflow_call:
117

128
permissions:
139
contents: write
@@ -16,72 +12,27 @@ concurrency:
1612
group: "pages-deployment"
1713
cancel-in-progress: true
1814

19-
defaults:
20-
run:
21-
shell: bash
22-
2315
jobs:
2416
deploy:
2517
runs-on: ubuntu-24.04
18+
2619
steps:
2720
- name: Checkout
28-
uses: actions/checkout@v6
29-
with:
30-
fetch-depth: 0
21+
uses: actions/checkout@v4
3122

3223
- name: Setup Node
3324
uses: actions/setup-node@v4
3425
with:
3526
node-version: "20"
3627

3728
- name: Install dependencies
38-
run: "[[ -f package-lock.json || -f npm-shrinkwrap.json ]] && npm ci || true"
29+
run: npm ci
3930

40-
# GitHub project pages are served from https://<owner>.github.io/<repo>/.
41-
# Gatsby must use pathPrefix + `gatsby build --prefix-paths` or scripts/CSS load from / and break.
42-
# User/organization sites use a repo named <owner>.github.io and are served from the domain root.
43-
- name: Build site
44-
env:
45-
PUBLIC_SITE_URL: ${{ vars.PUBLIC_SITE_URL }}
31+
- name: Build Gatsby site
4632
run: |
47-
owner="${GITHUB_REPOSITORY%%/*}"
48-
repo_name="${GITHUB_REPOSITORY#*/}"
49-
50-
# Simple rule:
51-
# - If PUBLIC_SITE_URL is set (custom domain), build at root (no repo prefix).
52-
# - Otherwise use GitHub Pages URL; project repos need pathPrefix=<repo>.
53-
if [[ -n "${PUBLIC_SITE_URL:-}" ]]; then
54-
base="${PUBLIC_SITE_URL%/}"
55-
site_origin="$(printf '%s' "$base" | sed -E 's|(https?://[^/]+).*|\1|')"
56-
site_path="$(printf '%s' "$base" | sed -E 's|https?://[^/]+||' | sed 's|^/||;s|/$||')"
57-
site_host="$(printf '%s' "$site_origin" | sed -E 's|https?://||')"
58-
59-
export GATSBY_SITE_URL="$site_origin"
60-
if [[ -n "$site_path" ]]; then
61-
export PATH_PREFIX="$site_path"
62-
npm run build:gh-pages
63-
else
64-
unset PATH_PREFIX
65-
npm run build
66-
fi
67-
else
68-
if [[ "$repo_name" == "${owner}.github.io" ]]; then
69-
export GATSBY_SITE_URL="https://${owner}.github.io"
70-
unset PATH_PREFIX
71-
npm run build
72-
else
73-
export GATSBY_SITE_URL="https://${owner}.github.io"
74-
export PATH_PREFIX="$repo_name"
75-
npm run build:gh-pages
76-
fi
77-
fi
33+
npm run build
7834
touch public/.nojekyll
7935
80-
# Preserve custom domain in GitHub Pages settings by committing CNAME.
81-
if [[ -n "${site_host:-}" ]]; then
82-
printf '%s\n' "$site_host" > public/CNAME
83-
fi
84-
8536
- name: Deploy to GitHub Pages
8637
uses: peaceiris/actions-gh-pages@v4
8738
with:

.github/workflows/build-and-preview-site.yml

Lines changed: 11 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: Build and Preview Site
1+
name: Build and Preview Gatsby Site
22

33
on:
44
pull_request_target:
@@ -15,21 +15,20 @@ concurrency:
1515

1616
jobs:
1717
build-and-deploy-preview:
18-
runs-on: ubuntu-24.04
18+
runs-on: ubuntu-latest
19+
1920
steps:
2021
- name: Checkout PR
2122
if: github.event.action != 'closed'
22-
uses: actions/checkout@v6
23+
uses: actions/checkout@v4
2324
with:
2425
ref: ${{ github.event.pull_request.head.sha }}
25-
fetch-depth: 0
2626

27-
- name: Checkout base branch for cleanup
27+
- name: Checkout for cleanup
2828
if: github.event.action == 'closed'
29-
uses: actions/checkout@v6
29+
uses: actions/checkout@v4
3030
with:
31-
ref: ${{ github.event.pull_request.base.ref }}
32-
fetch-depth: 1
31+
ref: gh-pages
3332

3433
- name: Setup Node
3534
if: github.event.action != 'closed'
@@ -39,61 +38,18 @@ jobs:
3938

4039
- name: Install dependencies
4140
if: github.event.action != 'closed'
42-
run: "[[ -f package-lock.json || -f npm-shrinkwrap.json ]] && npm ci || true"
43-
44-
- name: Resolve preview URLs
45-
if: github.event.action != 'closed'
46-
id: preview-vars
47-
env:
48-
PREVIEW_SITE_URL: ${{ vars.PREVIEW_SITE_URL }}
49-
PUBLIC_SITE_URL: ${{ vars.PUBLIC_SITE_URL }}
50-
run: |
51-
owner="${GITHUB_REPOSITORY%%/*}"
52-
repo_name="${GITHUB_REPOSITORY#*/}"
53-
pr="pr-${{ github.event.pull_request.number }}"
54-
base="${PREVIEW_SITE_URL:-${PUBLIC_SITE_URL:-}}"
55-
56-
if [[ -n "$base" ]]; then
57-
base="${base%/}"
58-
pages_origin="$(printf '%s' "$base" | sed -E 's|(https?://[^/]+).*|\1|')"
59-
base_path="$(printf '%s' "$base" | sed -E 's|https?://[^/]+||' | sed 's|^/||;s|/$||')"
60-
if [[ -n "$base_path" ]]; then
61-
path_prefix="${base_path}/pr-preview/${pr}"
62-
preview_url="${pages_origin}/${base_path}/pr-preview/${pr}/"
63-
else
64-
path_prefix="pr-preview/${pr}"
65-
preview_url="${pages_origin}/pr-preview/${pr}/"
66-
fi
67-
else
68-
pages_origin="https://${owner}.github.io"
69-
if [[ "$repo_name" == "${owner}.github.io" ]]; then
70-
path_prefix="pr-preview/${pr}"
71-
preview_url="${pages_origin}/pr-preview/${pr}/"
72-
else
73-
path_prefix="${repo_name}/pr-preview/${pr}"
74-
preview_url="${pages_origin}/${repo_name}/pr-preview/${pr}/"
75-
fi
76-
fi
77-
78-
echo "pages_origin=${pages_origin}" >> "$GITHUB_OUTPUT"
79-
echo "path_prefix=${path_prefix}" >> "$GITHUB_OUTPUT"
80-
echo "preview_url=${preview_url}" >> "$GITHUB_OUTPUT"
41+
run: npm ci
8142

8243
- name: Build PR preview
8344
if: github.event.action != 'closed'
84-
env:
85-
GATSBY_PREVIEW: "true"
86-
GATSBY_SITE_URL: ${{ steps.preview-vars.outputs.pages_origin }}
87-
PATH_PREFIX: ${{ steps.preview-vars.outputs.path_prefix }}
8845
run: |
89-
npm run build:preview
46+
npm run build
9047
91-
# Block all crawlers from indexing PR previews.
48+
# Prevent indexing
9249
cat > public/robots.txt <<'EOF'
9350
User-agent: *
9451
Disallow: /
9552
EOF
96-
touch public/.nojekyll
9753
9854
- name: Deploy PR preview
9955
if: github.event.action != 'closed'
@@ -114,7 +70,7 @@ jobs:
11470
message: |
11571
🚀 **Preview deployment for PR #${{ github.event.pull_request.number }}**
11672
117-
🌐 **Preview URL**: ${{ steps.preview-vars.outputs.preview_url }}
73+
🌐 **Preview URL**: https://${{ github.repository_owner }}.github.io/${{ github.event.repository.name }}/pr-preview/pr-${{ github.event.pull_request.number }}/
11874
11975
_This preview will be updated automatically when you push new commits to this PR._
12076

0 commit comments

Comments
 (0)