Skip to content

Instantly share code, notes, and snippets.

@kshpytsya
Last active September 3, 2019 11:12
Show Gist options
  • Save kshpytsya/f5043064a85d37ae579afba0368c88de to your computer and use it in GitHub Desktop.
Save kshpytsya/f5043064a85d37ae579afba0368c88de to your computer and use it in GitHub Desktop.
Build docker image from local git working copy on a vargant box identified by vargant directory
#!/bin/bash
set -euo pipefail
if [ "$#" -lt 3 ]; then
cat << _END_
usage:
$0 <git dir> <vagrant dir> <docker tag> [<extra docker args>...]
note: needs "vcssh", available as https://gist.github.com/kshpytsya/d401ee95268b16f568da6bd6b4cadc1d
On MacOS, this also needs "gnu tar", installable as "brew install gnu-tar".
_END_
exit 1
fi
TMPDIR=$(mktemp -d)
trap 'rm -rf "$TMPDIR"' EXIT SIGINT
GIT_DIR="$1"
VAGRANT_DIR="$2"
DOCKER_TAG="$3"
shift 3
PATH="/usr/local/opt/gnu-tar/libexec/gnubin:$PATH"
git -C "$GIT_DIR" describe --always --dirty --tags > "$TMPDIR/VERSION"
(
cd "$GIT_DIR" &&
git ls-files -z |
tar c --null --files-from /dev/stdin -C "$TMPDIR" VERSION
) |
(
cd "$VAGRANT_DIR" &&
vcssh -- "DIR=\$(mktemp -d) && tar x -C \$DIR && sudo -H DOCKER_BUILDKIT=1 docker build -t $DOCKER_TAG $* \$DIR && rm -rf \$DIR"
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment