captaincore/app/site/copy-to-production
2021-12-15 13:33:27 -05:00

83 lines
No EOL
2.6 KiB
Bash

#!/usr/bin/env bash
#
# Copy staging to production (Kinsta only)
#
# `captaincore copy-staging-to-production <site> --email=<email>`
#
while read config; do
if [[ "$config" == "Error:"* ]]; then
continue
fi
declare "$config"
done <<< "$(php ${CAPTAINCORE_PATH}/lib/local-scripts/configs.php fetch)"
if [ ${#@} -ne 1 ]; then
echo -e "${COLOR_RED}Error:${COLOR_NORMAL} Please specify a site."
exit
fi
site=$1
run_command() {
# Load site configs
while read site_configs; do declare "$site_configs"; done <<< "$(captaincore site get $site --bash --captain-id=$CAPTAIN_ID)"
# Vars found, proceed
if [ -z "$domain" ]; then
echo "Domain not found"
return
fi
# Define Rclone config file
rclone_config_file="$path/${site}_${site_id}/rclone.conf"
if [ ! -f "$rclone_config_file" ]; then
captaincore site key-generate $site --captain-id=$CAPTAIN_ID
fi
if [ "$home_directory" != "" ]; then
home_directory="${home_directory}/"
fi
# Unique timestamp
timestamp=$(date +%s)
# Grab core version
core_version=$( captaincore ssh $site-staging --command="wp core version --skip-plugins --skip-themes" --captain-id=$CAPTAIN_ID )
captaincore ssh $site-staging --command="wp db export wp-content/mysql.sql --skip-plugins --skip-themes --add-drop-table; zip --test -q -r wp-content_$timestamp.zip . --exclude=wp-content/wp-includes/\* --exclude=wp-content/wp-admin/\* --exclude=wp-content/uploads/\* --exclude=wp-content/updraft/\*" --captain-id=$CAPTAIN_ID
staging_home_url=$( captaincore ssh $site-staging --command="wp option get home --skip-plugins --skip-themes" --captain-id=$CAPTAIN_ID )
# Download staging snapshot to production site and import database
captaincore ssh $site --script=migrate -- --url="${staging_home_url}/wp-content_$timestamp.zip" --update-urls --captain-id=$CAPTAIN_ID
# Remove from production_to_staging
captaincore ssh $site-staging --command="rm -f wp-content_$timestamp.zip" --captain-id=$CAPTAIN_ID
# Install matching core version
captaincore ssh $site --command="wp core update --version=$core_version --skip-plugins --skip-themes" --captain-id=$CAPTAIN_ID
# Sync uploads folders
rclone sync staging:${home_directory}wp-content/uploads/ production:${home_directory}wp-content/uploads/ --config="$rclone_config_file"
# Post completion to CaptainCore API
if [[ $captaincore_dev == true ]]; then
curl_argument="-k"
fi
curl ${curl_argument} --request POST "$captaincore_api" --header "Content-Type: application/json" --data @- << EOF
{
"command":"staging-to-production",
"site_id":"$site_id",
"email":"$email",
"token":"$token"
}
EOF
}
run_command