feat: add git:from-image support

The new command superscedes the previous tags plugin, and integrates docker image deployment with the general build process.

While `docker image load` is not supported, this otherwise completely handles all previous workflows supported by the `tags:deploy` command, while doing so in a much easier to use interface.

Closes #4296
This commit is contained in:
Jose Diaz-Gonzalez
2021-03-01 02:03:11 -05:00
parent 5804cd11bc
commit 0a31f6fe3a
8 changed files with 255 additions and 7 deletions

57
plugins/git/git-from-directory Executable file
View File

@@ -0,0 +1,57 @@
#!/usr/bin/env bash
source "$PLUGIN_CORE_AVAILABLE_PATH/common/property-functions"
source "$PLUGIN_AVAILABLE_PATH/git/functions"
source "$PLUGIN_AVAILABLE_PATH/git/internal-functions"
set -eo pipefail
[[ $DOKKU_TRACE ]] && set -x
trigger-git-git-from-directory() {
declare desc="updates a repository from a directory"
declare trigger="git-from-directory"
declare APP="$1" SOURCECODE_WORK_DIR="$2" USER_NAME="${3:-Dokku}" USER_EMAIL="${4:-automated@dokku.sh}"
local APP_ROOT="$DOKKU_ROOT/$APP"
local DOKKU_DEPLOY_BRANCH="$(fn-git-deploy-branch "$APP")"
local REV
local TMP_WORK_DIR=$(mktemp -d "/tmp/dokku-${DOKKU_PID}-${FUNCNAME[0]}.XXXXXX")
local TMP_WORK_DIR_2=$(mktemp -d "/tmp/dokku-${DOKKU_PID}-${FUNCNAME[0]}.XXXXXX")
# shellcheck disable=SC2086
trap "rm -rf '$TMP_WORK_DIR' '$TMP_WORK_DIR_2' >/dev/null" RETURN INT TERM EXIT
dokku_log_info1 "Updating git repository with specified build context"
if [[ "$(fn-git-cmd "$APP_ROOT" count-objects)" == "0 objects, 0 kilobytes" ]]; then
# setup new repo
cp -rT "$SOURCECODE_WORK_DIR" "$TMP_WORK_DIR"
suppress_output fn-git-cmd "$TMP_WORK_DIR" init
suppress_output fn-git-cmd "$TMP_WORK_DIR" config user.name "$USER_NAME"
suppress_output fn-git-cmd "$TMP_WORK_DIR" config user.email "$USER_EMAIL"
suppress_output fn-git-cmd "$TMP_WORK_DIR" add --all
suppress_output fn-git-cmd "$TMP_WORK_DIR" commit -m "Initial commit"
REV="$(fn-git-cmd "$TMP_WORK_DIR" rev-parse HEAD)"
rsync -a "$TMP_WORK_DIR/.git/" "$APP_ROOT"
fn-git-create-hook "$APP"
else
# update existing repo
GIT_TERMINAL_PROMPT=0 suppress_output git clone "$APP_ROOT" "$TMP_WORK_DIR_2"
cp -rT "$SOURCECODE_WORK_DIR" "$TMP_WORK_DIR"
mv "$TMP_WORK_DIR_2/.git" "$TMP_WORK_DIR/.git"
suppress_output fn-git-cmd "$TMP_WORK_DIR" config user.name "$USER_NAME"
suppress_output fn-git-cmd "$TMP_WORK_DIR" config user.email "$USER_EMAIL"
suppress_output fn-git-cmd "$TMP_WORK_DIR" add --all
suppress_output fn-git-cmd "$TMP_WORK_DIR" update-index --refresh
if suppress_output fn-git-cmd "$TMP_WORK_DIR" diff-index --quiet HEAD --; then
dokku_log_warn "No changes detected, aborting git update"
return
fi
suppress_output fn-git-cmd "$TMP_WORK_DIR" commit -m "Automated commit @ $(date +%s)"
REV="$(fn-git-cmd "$TMP_WORK_DIR" rev-parse HEAD)"
fn-git-fetch "$APP" "$TMP_WORK_DIR" "$REV"
fi
git_receive_app "$APP" "$REV"
}
trigger-git-git-from-directory "$@"

View File

@@ -29,6 +29,7 @@ fn-help-content() {
cat <<help_content
git:allow-host <host>, Adds a host to known_hosts
git:auth <host> [<username> <password>], Configures netrc authentication for a given git server
git:from-image <app> <docker-image> [<git-username> <git-email>], Updates a app's git repository with a given docker image
git:sync [--build] <app> <repository> [<git-ref>], Clone or fetch an app from remote git repo
git:initialize <app>, Initialize a git repository for an app
git:public-key, Outputs the dokku public deploy key

View File

@@ -36,6 +36,58 @@ cmd-git-auth() {
netrc set "$HOST" "$USERNAME" "$PASSWORD"
}
cmd-git-from-image() {
declare desc="updates a app's git repository with a given docker image"
local cmd="git:from-image"
[[ "$1" == "$cmd" ]] && shift 1
declare APP DOCKER_IMAGE USER_NAME USER_EMAIL
local BUILD_DIR
ARGS=()
skip=false
for arg in "$@"; do
if [[ "$arg" == "--build-dir" ]]; then
skip=true
continue
fi
if [[ "$skip" == "true" ]]; then
BUILD_DIR="$arg"
skip=false
continue
fi
ARGS+=("$arg")
done
APP="${ARGS[0]}"
DOCKER_IMAGE="${ARGS[1]}"
USER_NAME="${ARGS[2]}"
USER_EMAIL="${ARGS[3]}"
verify_app_name "$APP"
[[ -z "$DOCKER_IMAGE" ]] && dokku_log_fail "Please specify a docker image"
local TMP_WORK_DIR=$(mktemp -d "/tmp/dokku-${DOKKU_PID}-${FUNCNAME[0]}.XXXXXX")
trap "rm -rf '$TMP_WORK_DIR' '$TMP_WORK_DIR_2' >/dev/null" RETURN INT TERM EXIT
dokku_log_info1 "Generating build context"
if [[ -n "$BUILD_DIR" ]]; then
if [[ ! -d "$BUILD_DIR" ]]; then
dokku_log_fail "Invalid BUILD_DIR specified for docker build context"
fi
dokku_log_verbose "Syncing build directory context"
rsync -a "$BUILD_DIR/" "$TMP_WORK_DIR"
fi
dokku_log_verbose "Setting Dockerfile"
touch "$TMP_WORK_DIR/Dockerfile"
echo "FROM $DOCKER_IMAGE" >"$TMP_WORK_DIR/Dockerfile"
plugn trigger git-from-directory "$APP" "$TMP_WORK_DIR" "$USER_NAME" "$USER_EMAIL"
}
cmd-git-sync() {
declare desc="clone or fetch an app from remote git repo"
local cmd="git:sync"

View File

@@ -0,0 +1,6 @@
#!/usr/bin/env bash
source "$PLUGIN_AVAILABLE_PATH/git/internal-functions"
set -eo pipefail
[[ $DOKKU_TRACE ]] && set -x
cmd-git-from-image "$@"