#!/usr/bin/env bash set -Eeuo pipefail repo_url="${DEPLOY_REPO_URL:?DEPLOY_REPO_URL is required}" branch="${DEPLOY_BRANCH:-main}" workdir="${DEPLOY_WORKDIR:-/workspace/egfh-website}" deploy_command="${DEPLOY_COMMAND:-npm run deploy:worker:test}" worker_env_file="${WORKER_ENV_FILE:-.env.worker.test}" worker_env_example="${WORKER_ENV_EXAMPLE:-${worker_env_file}.example}" mkdir -p "$(dirname "$workdir")" if [ ! -d "$workdir/.git" ]; then rm -rf "$workdir" git clone --branch "$branch" "$repo_url" "$workdir" else git -C "$workdir" remote set-url origin "$repo_url" git -C "$workdir" fetch --prune origin "$branch" git -C "$workdir" checkout "$branch" git -C "$workdir" reset --hard "origin/$branch" fi cd "$workdir" if [ -f "$worker_env_example" ]; then : > "$worker_env_file" while IFS= read -r line || [ -n "$line" ]; do key="" default_value="" if [[ "$line" =~ ^[[:space:]]*#?[[:space:]]*([A-Za-z_][A-Za-z0-9_]*)=(.*)$ ]]; then key="${BASH_REMATCH[1]}" default_value="${BASH_REMATCH[2]}" else continue fi if [ -n "${!key+x}" ]; then printf "%s=%s\n" "$key" "${!key}" >> "$worker_env_file" elif [[ "$line" != \#* && "$default_value" != replace-with-* ]]; then printf "%s=%s\n" "$key" "$default_value" >> "$worker_env_file" fi done < "$worker_env_example" echo "Generated $worker_env_file from container environment" fi if [ -f package-lock.json ]; then npm ci else npm install fi exec bash -lc "$deploy_command"