Skip to content

Commit 00c6fb7

Browse files
authored
fix: PR deploy preview (#2)
Manual deployments to Netlify using github actions + Added the pull request preview
1 parent 4e29f0e commit 00c6fb7

File tree

6 files changed

+66
-5
lines changed

6 files changed

+66
-5
lines changed

.github/workflows/main.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ jobs:
3333
3434
- name: Build and Deploy to Netlify
3535
run: |
36-
npm run build
3736
netlify deploy --prod --build
3837
3938
env:

.github/workflows/preview.yml

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
name: Branch Preview Comment
2+
3+
on:
4+
pull_request:
5+
types:
6+
- opened
7+
push:
8+
branches:
9+
- '*'
10+
11+
jobs:
12+
build:
13+
runs-on: ubuntu-latest
14+
permissions:
15+
pull-requests: write
16+
steps:
17+
- name: Set up Node.js
18+
uses: actions/setup-node@v3
19+
with:
20+
node-version: 18.18.0
21+
- uses: actions/checkout@v4
22+
- name: Cache node modules
23+
uses: actions/cache@v2
24+
with:
25+
path: |
26+
**/node_modules
27+
key: ${{ runner.os }}-modules-${{ hashFiles('**/package-lock.json') }}
28+
restore-keys: |
29+
**/package-lock.json
30+
- name: Install dependencies
31+
run: npm install
32+
- name: Build & Deploy to Netlify
33+
id: netlify_deploy
34+
run: |
35+
NETLIFY_AUTH_TOKEN="${{ secrets.NETLIFY_AUTH_TOKEN }}"
36+
NETLIFY_SITE_ID="${{ secrets.NETLIFY_PREVIEW_SITE_ID }}"
37+
PR_NUMBER="${{ github.event.number }}"
38+
39+
# Run the Netlify deploy command
40+
DEPLOY_OUTPUT=$(netlify deploy --build --context deploy-preview --alias "pr-review-${PR_NUMBER}" --json)
41+
42+
# Extract the URLs from the JSON output using jq
43+
DEPLOY_URL=$(echo "$DEPLOY_OUTPUT" | jq -r '.deploy_url')
44+
LOGS_URL=$(echo "$DEPLOY_OUTPUT" | jq -r '.logs')
45+
46+
# Set outputs for later use
47+
echo "::set-output name=deploy_url::${DEPLOY_URL}"
48+
echo "::set-output name=logs_url::${LOGS_URL}"
49+
env:
50+
NETLIFY_AUTH_TOKEN: ${{ secrets.NETLIFY_AUTH_TOKEN }}
51+
NETLIFY_SITE_ID: ${{ secrets.NETLIFY_PREVIEW_SITE_ID }}
52+
- name: Comment on PR
53+
uses: mshick/add-pr-comment@v2
54+
with:
55+
message: |
56+
This pull request is being automatically deployed to Netlify.
57+
58+
✅ Preview: ${{ steps.netlify_deploy.outputs.deploy_url }}
59+
🔖 Deployed version: ${{ github.sha }}
60+
env:
61+
GITHUB_TOKEN: ${{ secrets.GH_TOKEN }}

netlify.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[build]
22
command = "npm run build"
3-
publish = ".next"
3+
publish = "build"
44

55
[[plugins]]
66
package = "@netlify/plugin-nextjs"

next.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const nextConfig = {
33
experimental: {
44
appDir: true,
55
},
6+
distDir: 'build',
67
};
78

89
module.exports = nextConfig;

src/app/page.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
export default function Home() {
3-
return <div>Hello World!</div>;
2+
return <div>Hello World! Lets build something great!</div>;
43
}

tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@
3333
"next-env.d.ts",
3434
"**/*.ts",
3535
"**/*.tsx",
36-
".next/types/**/*.ts"
36+
".next/types/**/*.ts",
37+
"build/types/**/*.ts"
3738
],
3839
"exclude": [
3940
"node_modules"

0 commit comments

Comments
 (0)