#!/bin/bash remote_validate_host() { if [[ ! ${1:-} =~ ^([A-Za-z0-9._-]+@)?[A-Za-z0-9][A-Za-z0-9._-]*$ ]]; then printf 'A valid explicit SSH host is required.\n' >&2 return 2 fi } remote_sync() { local host=$1 files remote_dir remote_validate_host "$host" || return files=$(mktemp "${TMPDIR:-/tmp}/parcel-files.XXXXXX") || return # Sending only tracked files keeps local secrets and unrelated work off the remote host. if ! git ls-files -z > "$files.all"; then rm -f "$files" "$files.all" return 1 fi while IFS= read -r -d '' file; do case "$file" in .build/* | build/* | .pi/* | */.build/* | */build/*) continue ;; esac if [[ -e $file || -L $file ]]; then printf '%s\0' "$file" >> "$files"; fi done < "$files.all" if ! remote_dir=$(ssh "$host" 'mkdir -p ~/parcel-ci && cd ~/parcel-ci && mktemp -d run.XXXXXXXX'); then rm -f "$files" "$files.all" return 1 fi if [[ ! $remote_dir =~ ^run\.[A-Za-z0-9]+$ ]]; then printf 'Unexpected remote directory: %s\n' "$remote_dir" >&2 rm -f "$files" "$files.all" return 1 fi if ! rsync -az --from0 --files-from="$files" ./ "$host:parcel-ci/$remote_dir/" >&2; then rm -f "$files" "$files.all" return 1 fi rm -f "$files" "$files.all" printf '%s\n' "$remote_dir" } if [[ ${BASH_SOURCE[0]} == "$0" ]]; then set -euo pipefail cd "$(dirname "$0")/.." [[ $# -eq 1 ]] || { printf 'Usage: %s \n' "$0" >&2; exit 2; } remote_sync "$1" fi