#!/bin/bash
# Deploy Daily Digest Email Scheduler
#
# This script sets up Cloud Scheduler to call the digest cron endpoint
# on the CanadaGPT frontend at scheduled intervals.
#
# Usage: ./scripts/deploy-digest-scheduler.sh
#
# The CRON_SECRET is fetched from Google Cloud Secret Manager.
# Make sure the secret exists: gcloud secrets create cron-secret --data-file=-
set -e
# Color codes for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Configuration
PROJECT_ID="canada-gpt-ca"
REGION="us-central1"
FRONTEND_URL="https://canadagpt-frontend-gkmqy3lq2a-uc.a.run.app"
SECRET_NAME="cron-secret"
echo -e "${BLUE}============================================${NC}"
echo -e "${BLUE} Deploying Daily Digest Email Scheduler${NC}"
echo -e "${BLUE}============================================${NC}"
echo ""
# Check if gcloud is authenticated
echo -e "${YELLOW}→ Checking GCP authentication...${NC}"
if ! gcloud auth list --filter=status:ACTIVE --format="value(account)" | grep -q .; then
echo -e "${RED}✗ Not authenticated with gcloud. Please run:${NC}"
echo -e "${RED} gcloud auth login${NC}"
exit 1
fi
echo -e "${GREEN}✓ Authenticated${NC}"
# Set project
gcloud config set project ${PROJECT_ID} --quiet
# Check if secret exists
echo -e "${YELLOW}→ Checking for ${SECRET_NAME} in Secret Manager...${NC}"
if ! gcloud secrets describe ${SECRET_NAME} --project=${PROJECT_ID} &>/dev/null; then
echo -e "${YELLOW}Secret '${SECRET_NAME}' not found. Creating it now...${NC}"
echo ""
echo -e "${YELLOW}Generating a secure random secret...${NC}"
# Generate a secure random secret
NEW_SECRET=$(openssl rand -base64 32)
# Create the secret
echo -n "$NEW_SECRET" | gcloud secrets create ${SECRET_NAME} \
--project=${PROJECT_ID} \
--data-file=- \
--replication-policy="automatic"
echo -e "${GREEN}✓ Secret '${SECRET_NAME}' created${NC}"
echo ""
echo -e "${YELLOW}IMPORTANT: You need to redeploy the frontend to pick up the new secret:${NC}"
echo -e "${YELLOW} ./scripts/deploy-frontend-cloudrun.sh${NC}"
echo ""
fi
# Fetch the secret value
echo -e "${YELLOW}→ Fetching CRON_SECRET from Secret Manager...${NC}"
CRON_SECRET=$(gcloud secrets versions access latest --secret=${SECRET_NAME} --project=${PROJECT_ID})
if [ -z "$CRON_SECRET" ]; then
echo -e "${RED}✗ Failed to fetch secret. Make sure you have access.${NC}"
exit 1
fi
echo -e "${GREEN}✓ Secret fetched${NC}"
echo ""
echo -e "${BLUE}============================================${NC}"
echo -e "${BLUE} Setting up Cloud Scheduler Jobs${NC}"
echo -e "${BLUE}============================================${NC}"
echo ""
# Daily Digest Job - runs at 8:00 AM EST (13:00 UTC)
DAILY_JOB="digest-daily"
DAILY_SCHEDULE="0 13 * * *"
echo -e "${YELLOW}→ Configuring daily digest job...${NC}"
if gcloud scheduler jobs describe $DAILY_JOB --location=$REGION --project=$PROJECT_ID &>/dev/null; then
echo " Updating existing job..."
gcloud scheduler jobs update http $DAILY_JOB \
--location=$REGION \
--project=$PROJECT_ID \
--schedule="$DAILY_SCHEDULE" \
--time-zone="UTC" \
--uri="$FRONTEND_URL/api/cron/digest" \
--http-method=POST \
--headers="Authorization=Bearer $CRON_SECRET,Content-Type=application/json" \
--message-body='{"digestType":"daily"}' \
--attempt-deadline=600s \
--description="Daily email digest for CanadaGPT users"
else
echo " Creating new job..."
gcloud scheduler jobs create http $DAILY_JOB \
--location=$REGION \
--project=$PROJECT_ID \
--schedule="$DAILY_SCHEDULE" \
--time-zone="UTC" \
--uri="$FRONTEND_URL/api/cron/digest" \
--http-method=POST \
--headers="Authorization=Bearer $CRON_SECRET,Content-Type=application/json" \
--message-body='{"digestType":"daily"}' \
--attempt-deadline=600s \
--description="Daily email digest for CanadaGPT users"
fi
echo -e "${GREEN}✓ Daily digest job configured${NC}"
echo ""
# Weekly Digest Job - runs on Sundays at 9:00 AM EST (14:00 UTC)
WEEKLY_JOB="digest-weekly"
WEEKLY_SCHEDULE="0 14 * * 0"
echo -e "${YELLOW}→ Configuring weekly digest job...${NC}"
if gcloud scheduler jobs describe $WEEKLY_JOB --location=$REGION --project=$PROJECT_ID &>/dev/null; then
echo " Updating existing job..."
gcloud scheduler jobs update http $WEEKLY_JOB \
--location=$REGION \
--project=$PROJECT_ID \
--schedule="$WEEKLY_SCHEDULE" \
--time-zone="UTC" \
--uri="$FRONTEND_URL/api/cron/digest" \
--http-method=POST \
--headers="Authorization=Bearer $CRON_SECRET,Content-Type=application/json" \
--message-body='{"digestType":"weekly"}' \
--attempt-deadline=600s \
--description="Weekly email digest for CanadaGPT users"
else
echo " Creating new job..."
gcloud scheduler jobs create http $WEEKLY_JOB \
--location=$REGION \
--project=$PROJECT_ID \
--schedule="$WEEKLY_SCHEDULE" \
--time-zone="UTC" \
--uri="$FRONTEND_URL/api/cron/digest" \
--http-method=POST \
--headers="Authorization=Bearer $CRON_SECRET,Content-Type=application/json" \
--message-body='{"digestType":"weekly"}' \
--attempt-deadline=600s \
--description="Weekly email digest for CanadaGPT users"
fi
echo -e "${GREEN}✓ Weekly digest job configured${NC}"
echo ""
echo -e "${BLUE}============================================${NC}"
echo -e "${BLUE} Deployment Complete!${NC}"
echo -e "${BLUE}============================================${NC}"
echo ""
echo -e "${GREEN}Scheduler jobs configured:${NC}"
echo " • digest-daily: Daily at 8:00 AM EST (13:00 UTC)"
echo " • digest-weekly: Sundays at 9:00 AM EST (14:00 UTC)"
echo ""
echo -e "${GREEN}Commands:${NC}"
echo " Manual trigger:"
echo " gcloud scheduler jobs run $DAILY_JOB --location=$REGION"
echo " gcloud scheduler jobs run $WEEKLY_JOB --location=$REGION"
echo ""
echo " View job status:"
echo " gcloud scheduler jobs list --location=$REGION"
echo ""
echo " View logs:"
echo " gcloud logging read 'resource.type=cloud_scheduler_job' --limit=20"
echo ""
echo -e "${GREEN}Deployment completed successfully! 🎉${NC}"