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:
2026-09-08 18:20:49 -03:00
parent ceebfb33f8
commit 3b0e6b4a3f
2 changed files with 27 additions and 8 deletions
+4
View File
@@ -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 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. 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 ## Running the application in dev mode
You can run your application in dev mode that enables live coding using: You can run your application in dev mode that enables live coding using:
+15
View File
@@ -15,12 +15,14 @@ readonly DEFAULT_PLATFORM="linux/amd64"
readonly UNREGISTRY_PLUGIN="https://raw.githubusercontent.com/psviderski/unregistry/main/docker-pussh" readonly UNREGISTRY_PLUGIN="https://raw.githubusercontent.com/psviderski/unregistry/main/docker-pussh"
readonly PLUGIN_DIR="${HOME}/.docker/cli-plugins" readonly PLUGIN_DIR="${HOME}/.docker/cli-plugins"
readonly PLUGIN_PATH="${PLUGIN_DIR}/docker-pussh" readonly PLUGIN_PATH="${PLUGIN_DIR}/docker-pussh"
readonly SCRIPT_DIR="$(cd "$(dirname "$(readlink -f "$0")")" && pwd)"
REMOTE="" REMOTE=""
IMAGE_NAME="" IMAGE_NAME=""
IMAGE_TAG="" IMAGE_TAG=""
SSH_KEY="" SSH_KEY=""
SSH_CONFIG="" SSH_CONFIG=""
PROJECT_DIR="${PROJECT_DIR:-${SCRIPT_DIR}/..}"
SKIP_BUILD=0 SKIP_BUILD=0
SKIP_VERIFY=0 SKIP_VERIFY=0
@@ -63,6 +65,10 @@ Image identity (defaults read from pom.xml):
--name NAME Image repository name --name NAME Image repository name
--tag TAG Image tag --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: Build options:
--skip-build Skip docker build, only pussh an existing image --skip-build Skip docker build, only pussh an existing image
(platform is fixed to ${DEFAULT_PLATFORM}) (platform is fixed to ${DEFAULT_PLATFORM})
@@ -81,6 +87,8 @@ Examples:
$(basename "$0") --remote deploy@prod.example.com $(basename "$0") --remote deploy@prod.example.com
$(basename "$0") --remote ubuntu@1.2.3.4 --ssh-key ~/.ssh/deploy_ed25519 $(basename "$0") --remote ubuntu@1.2.3.4 --ssh-key ~/.ssh/deploy_ed25519
$(basename "$0") --remote deploy@prod --skip-build --tag 2.0.0 $(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 EOF
} }
@@ -109,6 +117,7 @@ while [[ $# -gt 0 ]]; do
--tag) IMAGE_TAG="$2"; shift 2 ;; --tag) IMAGE_TAG="$2"; shift 2 ;;
--ssh-key) SSH_KEY="$2"; shift 2 ;; --ssh-key) SSH_KEY="$2"; shift 2 ;;
--ssh-config) SSH_CONFIG="$2"; shift 2 ;; --ssh-config) SSH_CONFIG="$2"; shift 2 ;;
--project-dir) PROJECT_DIR="$2"; shift 2 ;;
--skip-build) SKIP_BUILD=1; shift ;; --skip-build) SKIP_BUILD=1; shift ;;
--no-verify) SKIP_VERIFY=1; shift ;; --no-verify) SKIP_VERIFY=1; shift ;;
-h|--help) usage; exit 0 ;; -h|--help) usage; exit 0 ;;
@@ -116,6 +125,12 @@ while [[ $# -gt 0 ]]; do
esac esac
done done
# ---------- resolve project root ----------
if ! cd "${PROJECT_DIR}" 2>/dev/null; then
die "Invalid --project-dir: ${PROJECT_DIR}"
fi
PROJECT_DIR="$(pwd)"
# ---------- defaults ---------- # ---------- defaults ----------
[[ -z "$IMAGE_NAME" ]] && IMAGE_NAME=$(read_pom "/*[local-name()='project']/*[local-name()='artifactId']" "test-quarkus") [[ -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") [[ -z "$IMAGE_TAG" ]] && IMAGE_TAG=$(read_pom "/*[local-name()='project']/*[local-name()='version']" "1.0.0-SNAPSHOT")