build: make build-and-push.sh runnable from any directory
- Resolve PROJECT_DIR from the script's own location by default (works with absolute, relative and symlinked invocations) - Allow override via --project-dir flag or PROJECT_DIR env var - Improve error message for invalid --project-dir paths - Update usage and README accordingly
This commit is contained in:
@@ -15,12 +15,14 @@ readonly DEFAULT_PLATFORM="linux/amd64"
|
||||
readonly UNREGISTRY_PLUGIN="https://raw.githubusercontent.com/psviderski/unregistry/main/docker-pussh"
|
||||
readonly PLUGIN_DIR="${HOME}/.docker/cli-plugins"
|
||||
readonly PLUGIN_PATH="${PLUGIN_DIR}/docker-pussh"
|
||||
readonly SCRIPT_DIR="$(cd "$(dirname "$(readlink -f "$0")")" && pwd)"
|
||||
|
||||
REMOTE=""
|
||||
IMAGE_NAME=""
|
||||
IMAGE_TAG=""
|
||||
SSH_KEY=""
|
||||
SSH_CONFIG=""
|
||||
PROJECT_DIR="${PROJECT_DIR:-${SCRIPT_DIR}/..}"
|
||||
SKIP_BUILD=0
|
||||
SKIP_VERIFY=0
|
||||
|
||||
@@ -63,6 +65,10 @@ Image identity (defaults read from pom.xml):
|
||||
--name NAME Image repository name
|
||||
--tag TAG Image tag
|
||||
|
||||
Working directory (defaults to the parent of the script's directory):
|
||||
--project-dir PATH Project root containing pom.xml
|
||||
(override with env PROJECT_DIR)
|
||||
|
||||
Build options:
|
||||
--skip-build Skip docker build, only pussh an existing image
|
||||
(platform is fixed to ${DEFAULT_PLATFORM})
|
||||
@@ -81,6 +87,8 @@ Examples:
|
||||
$(basename "$0") --remote deploy@prod.example.com
|
||||
$(basename "$0") --remote ubuntu@1.2.3.4 --ssh-key ~/.ssh/deploy_ed25519
|
||||
$(basename "$0") --remote deploy@prod --skip-build --tag 2.0.0
|
||||
/absolute/path/to/test-quarkus/scripts/$(basename "$0") --remote deploy@prod
|
||||
PROJECT_DIR=/srv/proj $(basename "$0") --remote deploy@prod --skip-build
|
||||
EOF
|
||||
}
|
||||
|
||||
@@ -104,18 +112,25 @@ require() {
|
||||
# ---------- arg parsing ----------
|
||||
while [[ $# -gt 0 ]]; do
|
||||
case "$1" in
|
||||
--remote) REMOTE="$2"; shift 2 ;;
|
||||
--name) IMAGE_NAME="$2"; shift 2 ;;
|
||||
--tag) IMAGE_TAG="$2"; shift 2 ;;
|
||||
--ssh-key) SSH_KEY="$2"; shift 2 ;;
|
||||
--ssh-config) SSH_CONFIG="$2"; shift 2 ;;
|
||||
--skip-build) SKIP_BUILD=1; shift ;;
|
||||
--no-verify) SKIP_VERIFY=1; shift ;;
|
||||
-h|--help) usage; exit 0 ;;
|
||||
--remote) REMOTE="$2"; shift 2 ;;
|
||||
--name) IMAGE_NAME="$2"; shift 2 ;;
|
||||
--tag) IMAGE_TAG="$2"; shift 2 ;;
|
||||
--ssh-key) SSH_KEY="$2"; shift 2 ;;
|
||||
--ssh-config) SSH_CONFIG="$2"; shift 2 ;;
|
||||
--project-dir) PROJECT_DIR="$2"; shift 2 ;;
|
||||
--skip-build) SKIP_BUILD=1; shift ;;
|
||||
--no-verify) SKIP_VERIFY=1; shift ;;
|
||||
-h|--help) usage; exit 0 ;;
|
||||
*) die "Unknown argument: $1 (use --help)" ;;
|
||||
esac
|
||||
done
|
||||
|
||||
# ---------- resolve project root ----------
|
||||
if ! cd "${PROJECT_DIR}" 2>/dev/null; then
|
||||
die "Invalid --project-dir: ${PROJECT_DIR}"
|
||||
fi
|
||||
PROJECT_DIR="$(pwd)"
|
||||
|
||||
# ---------- defaults ----------
|
||||
[[ -z "$IMAGE_NAME" ]] && IMAGE_NAME=$(read_pom "/*[local-name()='project']/*[local-name()='artifactId']" "test-quarkus")
|
||||
[[ -z "$IMAGE_TAG" ]] && IMAGE_TAG=$(read_pom "/*[local-name()='project']/*[local-name()='version']" "1.0.0-SNAPSHOT")
|
||||
|
||||
Reference in New Issue
Block a user