personal-site/cicd/scripts/deploy.sh
badbl0cks 1fbcbf772a
All checks were successful
Build And Deploy / build-and-deploy (push) Successful in 1m22s
Enable WireGuard service, change HTTPFetchClient to use wireguard proxy, and add required env vars
Add WireGuard-related env variables to .env.example (addresses,
keys, endpoint, DNS)
Resolve WIREGUARD_ENDPOINT_HOST to WIREGUARD_ENDPOINT_IP in
cicd/scripts/deploy.sh and write it to .env, failing if unresolved
Un-comment and enable the wireguard service in docker-compose.yml
Remove an obsolete commented workflow snippet
2026-02-07 17:44:58 -08:00

47 lines
1.5 KiB
Bash
Executable file

#!/bin/bash
set -eu
#######################
# VARIABLES #
#######################
ROOT_DIR=$(dirname $(dirname $(dirname $(realpath $0))))
### NO EDITS BELOW THIS LINE ###
cd ${ROOT_DIR}
source .env
mkdir -p ${HOME}/.ssh
chmod 700 ${HOME}/.ssh
echo "${SSH_KEY}" > ${HOME}/.ssh/id_ed25519-${SSH_HOST//./_}
echo "${SSH_KNOWN_HOST}" > ${HOME}/.ssh/known_hosts-${SSH_HOST//./_}
chmod -R 600 ${HOME}/.ssh/
chmod 700 ${HOME}/.ssh
grep -q "Host ${SSH_HOST}" ${HOME}/.ssh/config 2>&1 1>/dev/null || cat >> ${HOME}/.ssh/config <<EOF
Host ${SSH_HOST}
HostName ${SSH_HOST}
User ${SSH_USER}
Port ${SSH_PORT}
IdentityFile ${HOME}/.ssh/id_ed25519-${SSH_HOST//./_}
UserKnownHostsFile ${HOME}/.ssh/known_hosts-${SSH_HOST//./_}
StrictHostKeyChecking yes
ControlMaster auto
ControlPath ~/.ssh/control-%C
ControlPersist yes
ConnectionAttempts 3
ConnectTimeout 10
ServerAliveInterval 10
EOF
WIREGUARD_ENDPOINT_IP=$(dig +short $WIREGUARD_ENDPOINT_HOST | tail -n1)
[[ -z ${WIREGUARD_ENDPOINT_IP} ]] && echo "Failed to resolve IP address for WIREGUARD_ENDPOINT_HOST" >&2 && exit 1
echo "WIREGUARD_ENDPOINT_IP=${WIREGUARD_ENDPOINT_IP}" >> .env
DOCKER_HOST=ssh://${SSH_HOST} docker load -i ${IMAGE_FILENAME}
ssh ${SSH_HOST} "mkdir -p /srv/${IMAGE_NAME#*/}/"
ssh ${SSH_HOST} "cd /srv/${IMAGE_NAME#*/}/ && docker compose down"
scp .env ${SSH_HOST}:/srv/${IMAGE_NAME#*/}/.env
cd deploy
scp -r . ${SSH_HOST}:/srv/${IMAGE_NAME#*/}/
ssh ${SSH_HOST} "cd /srv/${IMAGE_NAME#*/}/ && docker compose up -d"