-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy-pages.sh
More file actions
executable file
·140 lines (109 loc) · 4.08 KB
/
Copy pathdeploy-pages.sh
File metadata and controls
executable file
·140 lines (109 loc) · 4.08 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#!/usr/bin/env bash
set -euo pipefail
SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)"
REPO_ROOT="$SCRIPT_DIR"
PROJECT_NAME="${CF_PAGES_PROJECT_NAME:-microfilm-releases}"
PRODUCTION_BRANCH="${CF_PAGES_PRODUCTION_BRANCH:-main}"
PUBLISH_DIR="${CF_PAGES_DIR:-docs}"
DEPLOY_BRANCH="${CF_PAGES_BRANCH:-}"
CUSTOM_DOMAIN="${CF_PAGES_CUSTOM_DOMAIN:-microfilm.browserbox.io}"
WRANGLER_CHECK_TIMEOUT="${CF_PAGES_WRANGLER_CHECK_TIMEOUT:-45}"
log() {
printf '%s\n' "$*" >&2
}
fail() {
log "Error: $*"
exit 1
}
require_cmd() {
command -v "$1" >/dev/null 2>&1 || fail "$1 is required."
}
resolve_wrangler() {
if command -v wrangler >/dev/null 2>&1; then
WRANGLER=(wrangler)
return
fi
require_cmd npm
log "Wrangler not found on PATH. Installing it globally with npm..."
npm install --global wrangler@latest
hash -r
command -v wrangler >/dev/null 2>&1 || fail "Wrangler was installed, but 'wrangler' is still not on PATH. Add your npm global bin directory to PATH and retry."
WRANGLER=(wrangler)
}
run_wrangler() {
"${WRANGLER[@]}" "$@"
}
run_wrangler_check() {
if command -v timeout >/dev/null 2>&1; then
timeout "$WRANGLER_CHECK_TIMEOUT" "${WRANGLER[@]}" "$@"
return
fi
if command -v gtimeout >/dev/null 2>&1; then
gtimeout "$WRANGLER_CHECK_TIMEOUT" "${WRANGLER[@]}" "$@"
return
fi
"${WRANGLER[@]}" "$@"
}
ensure_project_exists() {
local project_list_output
local project_list_status=0
local create_output
local create_status=0
project_list_output="$(run_wrangler_check pages project list --json 2>&1)" || project_list_status=$?
if [[ "$project_list_status" -eq 124 ]]; then
fail "Wrangler project lookup timed out after ${WRANGLER_CHECK_TIMEOUT}s. Run 'wrangler pages project list --json' directly and verify Cloudflare connectivity before retrying."
fi
if [[ "$project_list_status" -ne 0 ]]; then
printf '%s\n' "$project_list_output" >&2
fail "Unable to list Cloudflare Pages projects."
fi
if printf '%s\n' "$project_list_output" | grep -Eq "\"(name|Project Name)\"[[:space:]]*:[[:space:]]*\"${PROJECT_NAME}\""; then
return 0
fi
log "Creating Cloudflare Pages project '${PROJECT_NAME}'..."
create_output="$(run_wrangler pages project create "$PROJECT_NAME" --production-branch "$PRODUCTION_BRANCH" 2>&1)" || create_status=$?
if [[ "$create_status" -ne 0 ]]; then
if printf '%s\n' "$create_output" | grep -Fq "already exists"; then
log "Cloudflare Pages project '${PROJECT_NAME}' already exists."
return 0
fi
printf '%s\n' "$create_output" >&2
return "$create_status"
fi
printf '%s\n' "$create_output" >&2
}
main() {
cd "$REPO_ROOT"
[[ -d "$PUBLISH_DIR" ]] || fail "Publish directory '$PUBLISH_DIR' does not exist."
require_cmd git
resolve_wrangler
log "Checking Wrangler authentication..."
local auth_status=0
run_wrangler_check whoami || auth_status=$?
if [[ "$auth_status" -eq 124 ]]; then
fail "Wrangler auth check timed out after ${WRANGLER_CHECK_TIMEOUT}s. Run 'wrangler whoami' directly and verify Cloudflare connectivity before retrying."
fi
if [[ "$auth_status" -ne 0 ]]; then
fail "Wrangler is not authenticated. Run 'wrangler login' or export CLOUDFLARE_API_TOKEN and CLOUDFLARE_ACCOUNT_ID."
fi
if [[ -z "$DEPLOY_BRANCH" ]]; then
DEPLOY_BRANCH="$(git rev-parse --abbrev-ref HEAD 2>/dev/null || printf '%s' "$PRODUCTION_BRANCH")"
fi
ensure_project_exists
cp install.* docs/
COMMIT_HASH="$(git rev-parse HEAD)"
COMMIT_MESSAGE="$(git log -1 --pretty=%s)"
log "Deploying '$PUBLISH_DIR' to Cloudflare Pages project '$PROJECT_NAME' on branch '$DEPLOY_BRANCH'..."
run_wrangler pages deploy "$PUBLISH_DIR" \
--project-name "$PROJECT_NAME" \
--branch "$DEPLOY_BRANCH" \
--commit-hash "$COMMIT_HASH" \
--commit-message "$COMMIT_MESSAGE"
if [[ "$DEPLOY_BRANCH" == "$PRODUCTION_BRANCH" ]]; then
log "Production URL: https://${PROJECT_NAME}.pages.dev"
else
log "Preview URL: https://${DEPLOY_BRANCH}.${PROJECT_NAME}.pages.dev"
fi
log "Attach custom domain '$CUSTOM_DOMAIN' in Cloudflare Pages once DNS is ready."
}
main "$@"