From 3b0e6b4a3f765f42abf97264bd61e6240afa241c Mon Sep 17 00:00:00 2001 From: Daniel Arroyo Date: Tue, 8 Sep 2026 18:20:49 -0300 Subject: [PATCH] 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 --- README.md | 4 ++++ scripts/build-and-push.sh | 31 +++++++++++++++++++++++-------- 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 3fd431e..bceea96 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,10 @@ The script reads `artifactId` and `version` from `pom.xml` for the image name and tag, builds for `linux/amd64`, and verifies the image is present on the remote after pushing. See `./scripts/build-and-push.sh --help` for all flags. +It can be invoked from any directory: by default it resolves the project root +from the script's own location. Override with `--project-dir PATH` or the +`PROJECT_DIR` environment variable when needed. + ## Running the application in dev mode You can run your application in dev mode that enables live coding using: diff --git a/scripts/build-and-push.sh b/scripts/build-and-push.sh index 2f300b7..af01115 100755 --- a/scripts/build-and-push.sh +++ b/scripts/build-and-push.sh @@ -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")