1 #!/bin/bash
2
3 if [[ "$1" == "docs-old" ]]; then
4 echo "🛑 - Build cancelled: Can't build old docs"
5 exit 0;
6 fi
7
8 PROJECT_NAME="$1"
9
10 echo "PROJECT_NAME: $PROJECT_NAME"
11 echo "VERCEL_ENV: $VERCEL_ENV"
12 echo "VERCEL_GIT_COMMIT_REF: $VERCEL_GIT_COMMIT_REF"
13
14 SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
15 echo "SCRIPT_DIR: $SCRIPT_DIR"
16
17 # Check for changes in the www directory (original logic)
18 $(git diff HEAD^ HEAD --quiet ${SCRIPT_DIR})
19 diffResult=$?
20
21 echo "DIFF RESULT (www): $diffResult"
22
23
24 # Check for production build condition before preview checks
25 if [[ "$VERCEL_ENV" == "production" && $diffResult -eq 1 ]] ; then
26 # Proceed with the build for production with changes
27 echo "✅ - Build can proceed (production with changes)"
28 exit 1;
29 fi
30
31 # Exit early if the PR branch doesn't start with 'docs/'
32 if [[ ! "$VERCEL_GIT_COMMIT_REF" =~ ^docs/ ]]; then
33 echo "🛑 - Build cancelled: Branch does not start with 'docs/'"
34 exit 0;
35 fi
36
37 # For preview environments, check project-specific directories
38 if [[ "$VERCEL_ENV" == "preview" && -n "$PROJECT_NAME" ]]; then
39 # Check for changes in the specific project directory
40 PROJECT_DIR="${SCRIPT_DIR}/apps/${PROJECT_NAME}"
41 if [[ -d "$PROJECT_DIR" ]]; then
42 $(git diff HEAD^ HEAD --quiet ${PROJECT_DIR})
43 projectDiffResult=$?
44 echo "DIFF RESULT (project ${PROJECT_NAME}): $projectDiffResult"
45 else
46 projectDiffResult=0
47 echo "Project directory ${PROJECT_DIR} does not exist"
48 fi
49
50 # Check for changes in www/packages directory
51 PACKAGES_DIR="${SCRIPT_DIR}/packages"
52 if [[ -d "$PACKAGES_DIR" ]]; then
53 $(git diff HEAD^ HEAD --quiet ${PACKAGES_DIR})
54 packagesDiffResult=$?
55 echo "DIFF RESULT (packages): $packagesDiffResult"
56 else
57 packagesDiffResult=0
58 echo "Packages directory ${PACKAGES_DIR} does not exist"
59 fi
60
61 # For preview, build if there are changes in project dir OR packages dir
62 previewShouldBuild=$((projectDiffResult + packagesDiffResult))
63 if [[ $previewShouldBuild -gt 0 ]]; then
64 previewShouldBuild=1
65 fi
66 echo "PREVIEW SHOULD BUILD: $previewShouldBuild"
67
68 if [[ $previewShouldBuild -eq 1 ]]; then
69 # Proceed with the build for preview if there are changes in project or packages directory
70 echo "✅ - Build can proceed (preview with project/packages changes)"
71 exit 1;
72 fi
73 fi
74
75 # Don't build
76 echo "🛑 - Build cancelled: Conditions don't match"
77 exit 0;