#!/usr/bin/env bash # # deploy — copy template and push repos # # Author: zenobit # License: MIT # DEPLOY_VERSION='0.0.3' # Source shared functions SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" # shellcheck source=lib.sh source "${SCRIPT_DIR}/lib.sh" if [[ "${1:-}" == --help || "${1:-}" == -h ]]; then printf '%s\n' \ 'Usage: web-deploy [--build | --surfer | --build-and-deploy | --help]' \ '--build: build this repository with web-create (no publishing).' \ '--surfer: replace a Surfer site with the already-built OUT_DIR (default: website/).' \ '--build-and-deploy: build this repository, then deploy to Surfer.' \ 'SURFER_SITE_NAME optionally selects a named site; omit for the default site.' \ 'SURFER_SERVER, SURFER_USER and SURFER_TOKEN override saved surfer config.' \ 'Without arguments: show the single-repository or LIST-based menu.' \ 'LIST is optional; define it in .env only to manage multiple repositories.' exit 0 fi case "${1:-}" in ''|--build|--surfer|--build-and-deploy) ;; *) _re "Unknown argument: ${1}. Run web-deploy --help." ;; esac # Load .env if [ -f ".env" ]; then source ".env" _g 'Using .env' elif [ -f "${SCRIPT_DIR}/.env" ]; then source "${SCRIPT_DIR}/.env" _g "Using ${SCRIPT_DIR}/.env" elif [ -f "${SCRIPT_DIR}/website-source/.env" ]; then source "${SCRIPT_DIR}/website-source/.env" export GITHUB_TOKEN echo "Using ${SCRIPT_DIR}/website-source/.env" else _re 'No .env found. Configure .env or website-source/.env before building.' exit 1 fi # --------------------------------------------------------------------------- # Helpers # --------------------------------------------------------------------------- # Get field from LIST entry (pipe-separated) # Usage: _parse_list_entry "$entry" _parse_list_entry() { echo "$1" | cut -d'|' -f"$2" } # Return local value if set, otherwise fall back to shared value _get_repo_val() { echo "${1:-$2}" } # Build a human-readable list of repo names from LIST _list_names() { for entry in "${LIST[@]}"; do local d d=$(_parse_list_entry "$entry" 1) basename "$d" done } # --------------------------------------------------------------------------- # Remove deprecated files from repos using template # --------------------------------------------------------------------------- _remove_deprecated() { local d d="$1" for f in $d/create $d/create-web $d/docs/goatcounter.html $d/src/goatcounter.html $d/src/_config.yml $d/docs/_config.yml; do [ rm "$f" 2>/dev/null ] && _b "Deprecated $f removed!" done } # --------------------------------------------------------------------------- # Actions # --------------------------------------------------------------------------- _copy() { for entry in "${LIST[@]}"; do local d d=$(_parse_list_entry "$entry" 1) [ -d "$d" ] || { _y "Skipping (not found): $d"; continue; } _g "Copying template to: $d" mkdir -p "$d/src" # Copy src/* but never overwrite src/.env for f in src/*; do [ "$f" = "src/.env" ] && continue cp -v "$f" "$d/src/" done cp -v lib.sh web-create web-deploy "$d/" cp -rv .github "$d/" _remove_deprecated "$d" done } _create_env() { for entry in "${LIST[@]}"; do local d title giscus_repo giscus_repo_id giscus_category giscus_category_id d=$( _parse_list_entry "$entry" 1) title=$( _parse_list_entry "$entry" 2) giscus_repo=$( _parse_list_entry "$entry" 3) giscus_repo_id=$( _parse_list_entry "$entry" 4) giscus_category=$( _parse_list_entry "$entry" 5) giscus_category_id=$(_parse_list_entry "$entry" 6) [ -d "$d" ] || { _y "Skipping (not found): $d"; continue; } # Fall back to shared values or auto-detect title=$( _get_repo_val "$title" "$(basename "$d" | sed 's/_/ /g')") giscus_repo=$( _get_repo_val "$giscus_repo" "$(git -C "$d" remote get-url origin 2>/dev/null \ | sed 's|.*github\.com[:/]\(.*\)\.git|\1|; s|.*github\.com[:/]\(.*\)|\1|')") giscus_repo_id=$( _get_repo_val "$giscus_repo_id" "${GISCUS_REPO_ID:-}") giscus_category=$( _get_repo_val "$giscus_category" "${GISCUS_CATEGORY:-}") giscus_category_id=$( _get_repo_val "$giscus_category_id" "${GISCUS_CATEGORY_ID:-}") # Ask before overwriting existing src/.env if [ -f "$d/src/.env" ]; then _y "src/.env already exists in $d" _confirm "Overwrite?" || { _y "Skipping $d"; continue; } fi mkdir -p "$d/src" echo "Creating src/.env for: $d" cat > "$d/src/.env" <> "$d/src/.env" done } _build_current() { (cd "${SCRIPT_DIR}" && ./web-create -b) } _deploy_surfer() { local site_dir="${OUT_DIR:-website}" [[ "$site_dir" == /* ]] || site_dir="${SCRIPT_DIR}/${site_dir}" [ -s "${site_dir}/index.html" ] || _re "No built index.html in ${site_dir}. Run web-deploy --build first." command -v surfer >/dev/null 2>&1 || _re 'Surfer CLI is required. Install @cloudron/surfer (with the deploy command).' if [[ -n "${SURFER_USER:-}" || -n "${SURFER_TOKEN:-}" ]]; then [[ -n "${SURFER_USER:-}" && -n "${SURFER_TOKEN:-}" ]] || _re 'Set both SURFER_USER and SURFER_TOKEN, or use saved surfer credentials.' fi # Never accidentally package repository metadata or local environment secrets. local private_file private_file=$(find "$site_dir" \( -name .git -o -name .env -o -name '.env.*' \) -print -quit) || return 1 [ -z "$private_file" ] || _re "Refusing to publish private configuration: ${private_file}" local args=(deploy "$site_dir") [ -z "${SURFER_SITE_NAME:-}" ] || args+=(--site-name "$SURFER_SITE_NAME") [ -z "${SURFER_SERVER:-}" ] || args+=(--server "$SURFER_SERVER") if [ -n "${SURFER_USER:-}" ]; then args+=(--username "$SURFER_USER" --password "$SURFER_TOKEN") fi [ -z "${SURFER_MESSAGE:-}" ] || args+=(--message "$SURFER_MESSAGE") # Do not print this command: its arguments may contain an app password. surfer "${args[@]}" } _build_repos() { for entry in "${LIST[@]}"; do local d d=$(_parse_list_entry "$entry" 1) [ -f "$d/web-create" ] || { _y "Skipping (no web-create): $d"; continue; } _g "Building: $d" (cd "$d" && GITHUB_TOKEN="$GITHUB_TOKEN" ./web-create -b) || _y "Build failed: $d" done } _push_template() { _g "Pushing template..." git push origin } _push_repos() { local msg msg=$(_input "Commit message...") [ -z "$msg" ] && _g "No message, skipping push" && return for entry in "${LIST[@]}"; do local d d=$(_parse_list_entry "$entry" 1) [ -d "$d/.git" ] || continue _g "Pushing: $d" git -C "$d" add -A git -C "$d" commit -m "$msg" || _y "Nothing to commit in $d" git -C "$d" push origin || _y "Push failed: $d" done } _release_template() { if ! command -v gh &>/dev/null; then _re "gh CLI not found — install it first" fi local version version=$(_input "Release version (e.g. v1.0.0)...") [ -z "$version" ] && _g "No version, skipping release" && return local notes notes=$(_write "Release notes (optional)...") if gh release create "$version" --title "$version" --notes "$notes"; then _g "Release $version created" else _re "Release failed" fi } # --------------------------------------------------------------------------- # Menu # --------------------------------------------------------------------------- case "${1:-}" in --build) _build_current; exit $? ;; --surfer) _deploy_surfer; exit $? ;; --build-and-deploy) _build_current && _deploy_surfer; exit $? ;; esac if [ ${#LIST[@]} -eq 0 ]; then _b "Website Deploy System — this repository" _y 'Single-repository mode. Surfer deployment uses SURFER_* settings and/or saved surfer credentials.' ACTION=$(_choose "Build this website" "Deploy built website to Surfer" "Build and deploy to Surfer" "Exit") case "$ACTION" in "Build this website") _build_current; exit $? ;; "Deploy built website to Surfer") _confirm 'Replace the configured Surfer site with the built website?' && _deploy_surfer exit $? ;; "Build and deploy to Surfer") _confirm 'Build and replace the configured Surfer site?' && _build_current && _deploy_surfer exit $? ;; *) exit 0 ;; esac fi _b "Website Deploy System v$DEPLOY_VERSION by oSoWoSo" ACTION=$(_choose \ "Copy template to repos" \ "Create src/.env in repos" \ "Build all repos" \ "Push template (origin)" \ "Push repos from LIST" \ "Create release (template)" \ "All (copy + create .env + build + push template + push repos)") msgCopy="Copy template to: $(_list_names | sed 's/^/ /') Continue?" msgEnv="Create src/.env in: $(_list_names | sed 's/^/ /') Continue?" msgBuild="Build all repos in: $(_list_names | sed 's/^/ /') Continue?" case "$ACTION" in "Copy template to repos") _confirm "$msgCopy" && _copy ;; "Create src/.env in repos") _confirm "$msgEnv" && _create_env ;; "Build all repos") _confirm "$msgBuild" && _build_repos ;; "Push template (origin)") _confirm "Push template to origin?" && _push_template ;; "Push repos from LIST") _confirm "Commit and push all repos in LIST?" && _push_repos ;; "Create release (template)") _release_template ;; "All (copy + create .env + build + push template + push repos)") _confirm "Run all steps?" && { _copy _create_env _build_repos _push_template _push_repos } ;; esac