-
Notifications
You must be signed in to change notification settings - Fork 0
/
heroku.sh
executable file
·49 lines (39 loc) · 1.97 KB
/
heroku.sh
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
#!/bin/sh -l
# Validate inputs
if [ -z "$INPUT_HEROKU_API_KEY" ] || [ -z "$INPUT_HEROKU_APP_PRODUCTION" ] || [ -z "$INPUT_HEROKU_APP_STAGING" ]; then
printf "\n🚫 Heroku database syncronization failed.\n"
printf " Please provide 'heroku_app_production' and 'heroku_app_staging' to your Github action.\n\n"
exit 1
fi
# Download Heroku CLI
curl -s https://cli-assets.heroku.com/install.sh | sh
printf "\n✅ Heroku CLI installed\n\n"
# Verify authorization
export HEROKU_API_KEY="$INPUT_HEROKU_API_KEY"
heroku auth:whoami > /dev/null 2>&1
if [ $? -ne 0 ]; then
printf "\n🚫 Heroku database syncronization failed.\n"
printf " Unable to authorize using HEROKU_API_KEY.\n\n"
exit 1
fi
LAST_BACKUP_DATE="$(heroku pg:backups:info -a ${INPUT_HEROKU_APP_PRODUCTION} | grep -o -E -m 1 '[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}')"
if $INPUT_HEROKU_CREATE_BACKUP || [ -z "$LAST_BACKUP_DATE" ]; then
printf "✨ Capturing new database backup on ${INPUT_HEROKU_APP_PRODUCTION}...\n\n"
# Create production database backup
export HEROKU_APP=${INPUT_HEROKU_APP_PRODUCTION}
heroku pg:backups:capture
printf "\n✅ Production database backup captured\n\n"
else
printf "⏭ Reusing existing backup on ${INPUT_HEROKU_APP_PRODUCTION} from $LAST_BACKUP_DATE\n\n"
fi
# Restore production database backup to staging
printf "✨ Restoring database backup to ${INPUT_HEROKU_APP_STAGING}...\n\n"
export HEROKU_APP=${INPUT_HEROKU_APP_STAGING}
heroku pg:backups:restore ${INPUT_HEROKU_APP_PRODUCTION}:: DATABASE_URL --confirm ${INPUT_HEROKU_APP_STAGING}
printf "\n✅ Production database backup restored to ${INPUT_HEROKU_APP_STAGING}\n\n"
if $INPUT_RAILS_MIGRATE_BLOBS; then
# Update blobs to use staging bucket
DATABASE_URL="$(heroku pg:credentials:url | grep -o -E -m 1 'postgres://.+')"
psql $DATABASE_URL -c "UPDATE active_storage_blobs SET service_name='staging' WHERE service_name='production';"
printf "\n✅ Postgres: Updated service_name to staging on Active Storage blobs\n"
fi