89 linhas
3,8 KiB
Shell
Ficheiro executável
89 linhas
3,8 KiB
Shell
Ficheiro executável
#!/usr/bin/env bash
|
|
# Publishes a folder on a Interweb Angola VPS: asks the API for the server and its IP (vps.py),
|
|
# then copies the files with rsync over SSH, with the private key kept in a GitHub secret.
|
|
# Inputs come from INPUT_* environment variables (see action.yml).
|
|
set -euo pipefail
|
|
|
|
err() { echo "::error::$*"; exit 1; }
|
|
|
|
for bin in rsync ssh ssh-keyscan; do
|
|
command -v "$bin" >/dev/null 2>&1 || err "$bin is needed on the runner (Linux and macOS runners have it)."
|
|
done
|
|
PY="$(command -v python3 || command -v python || true)"
|
|
[ -n "$PY" ] || err "Python 3 is needed on the runner."
|
|
|
|
[ -n "${INPUT_SSH_PRIVATE_KEY:-}" ] || err "ssh-private-key is empty. Save the private key as a repository secret."
|
|
[ -n "${INPUT_TARGET:-}" ] || err "target is empty. Say the folder on the server, for example /var/www/html."
|
|
case "$INPUT_TARGET" in /*) ;; *) err "target must be an absolute path, such as /var/www/html." ;; esac
|
|
case "$INPUT_TARGET" in /|/root|/etc|/usr|/bin|/boot|/var|/home) [ "${INPUT_DELETE:-false}" = "true" ] && err "Refusing delete: true on $INPUT_TARGET." ;; esac
|
|
SOURCE="${INPUT_SOURCE:-.}"
|
|
[ -d "$SOURCE" ] || err "The source folder $SOURCE does not exist in the workspace."
|
|
PORT="${INPUT_SSH_PORT:-22}"
|
|
USER_NAME="${INPUT_SSH_USER:-root}"
|
|
case "$PORT" in ''|*[!0-9]*) err "ssh-port must be a number." ;; esac
|
|
|
|
WORK="$(mktemp -d)"
|
|
trap 'rm -rf "$WORK"' EXIT
|
|
chmod 700 "$WORK"
|
|
|
|
# 1. The server, from the API.
|
|
"$PY" "$GITHUB_ACTION_PATH/vps.py" "$WORK/server"
|
|
while IFS='=' read -r k v; do
|
|
case "$k" in
|
|
SERVER_ID) SERVER_ID="$v" ;; SERVER_NAME) SERVER_NAME="$v" ;;
|
|
SERVER_IP) SERVER_IP="$v" ;; POWER_STATE) POWER_STATE="$v" ;;
|
|
esac
|
|
done < "$WORK/server"
|
|
HOST="${INPUT_HOST:-$SERVER_IP}"
|
|
{
|
|
echo "server-id=$SERVER_ID"
|
|
echo "server-name=$SERVER_NAME"
|
|
echo "ip=$SERVER_IP"
|
|
echo "power-state=$POWER_STATE"
|
|
} >> "${GITHUB_OUTPUT:-/dev/null}"
|
|
|
|
# 2. SSH key and host key.
|
|
printf '%s\n' "$INPUT_SSH_PRIVATE_KEY" | tr -d '\r' > "$WORK/key"
|
|
chmod 600 "$WORK/key"
|
|
if [ -n "${INPUT_KNOWN_HOSTS:-}" ]; then
|
|
printf '%s\n' "$INPUT_KNOWN_HOSTS" > "$WORK/known_hosts"
|
|
else
|
|
echo "::warning::known-hosts is empty: trusting the host key the server shows now. Set known-hosts to be safe from spoofing."
|
|
ssh-keyscan -p "$PORT" -T 15 "$HOST" > "$WORK/known_hosts" 2>/dev/null || true
|
|
[ -s "$WORK/known_hosts" ] || err "Could not read the SSH host key of $HOST on port $PORT. Is SSH open on the server?"
|
|
fi
|
|
SSH=(ssh -i "$WORK/key" -p "$PORT" -o BatchMode=yes -o IdentitiesOnly=yes -o ConnectTimeout=20
|
|
-o StrictHostKeyChecking=yes -o UserKnownHostsFile="$WORK/known_hosts")
|
|
REMOTE="$USER_NAME@$HOST"
|
|
TARGET_Q="$(printf '%q' "${INPUT_TARGET%/}")"
|
|
|
|
remote() {
|
|
echo "Running on the server: $1"
|
|
"${SSH[@]}" "$REMOTE" "cd $TARGET_Q && $1"
|
|
}
|
|
|
|
# 3. Deploy.
|
|
echo "Deploying $SOURCE to $REMOTE:$INPUT_TARGET"
|
|
"${SSH[@]}" "$REMOTE" "mkdir -p $TARGET_Q"
|
|
if [ -n "${INPUT_PRE_COMMAND:-}" ] && [ "${INPUT_DRY_RUN:-false}" != "true" ]; then remote "$INPUT_PRE_COMMAND"; fi
|
|
|
|
ARGS=(-az --human-readable --stats)
|
|
if [ "${INPUT_DELETE:-false}" = "true" ]; then ARGS+=(--delete); fi
|
|
if [ "${INPUT_DRY_RUN:-false}" = "true" ]; then ARGS+=(--dry-run --itemize-changes); fi
|
|
while IFS= read -r line; do
|
|
line="$(printf '%s' "$line" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//')"
|
|
if [ -n "$line" ]; then ARGS+=("--exclude=$line"); fi
|
|
done <<< "${INPUT_EXCLUDE:-}"
|
|
ARGS+=(--exclude=.git/ --exclude=.github/)
|
|
if [ -n "${INPUT_RSYNC_OPTIONS:-}" ]; then
|
|
read -r -a EXTRA <<< "$INPUT_RSYNC_OPTIONS"
|
|
ARGS+=("${EXTRA[@]}")
|
|
fi
|
|
rsync "${ARGS[@]}" -e "${SSH[*]}" "${SOURCE%/}/" "$REMOTE:${INPUT_TARGET%/}/"
|
|
|
|
if [ "${INPUT_DRY_RUN:-false}" = "true" ]; then
|
|
echo "Dry run: nothing was copied, and pre-command and post-command did not run."
|
|
exit 0
|
|
fi
|
|
if [ -n "${INPUT_POST_COMMAND:-}" ]; then remote "$INPUT_POST_COMMAND"; fi
|
|
echo "Deploy done on $SERVER_NAME ($HOST)."
|