Merge pull request #5092 from dokku/4898-git-warnings
Warn and exit non-zero when attempting to rebuild from a non-existent branch
This commit is contained in:
@@ -774,6 +774,20 @@ set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x
|
||||
# TODO
|
||||
```
|
||||
|
||||
### `git-has-code`
|
||||
|
||||
- Description: Checks to see if there is code at the specified branch
|
||||
- Arguments: `$APP $BRANCH_NAME`
|
||||
- Example:
|
||||
|
||||
```shell
|
||||
#!/usr/bin/env bash
|
||||
|
||||
set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x
|
||||
|
||||
# TODO
|
||||
```
|
||||
|
||||
### `git-post-pull`
|
||||
|
||||
- Description:
|
||||
|
||||
@@ -48,6 +48,10 @@ func CommandClone(oldAppName string, newAppName string, skipDeploy bool, ignoreE
|
||||
os.Setenv("SKIP_REBUILD", "true")
|
||||
}
|
||||
|
||||
if err := common.PlugnTrigger("git-has-code", []string{newAppName}...); err != nil {
|
||||
os.Setenv("SKIP_REBUILD", "true")
|
||||
}
|
||||
|
||||
if err := common.PlugnTrigger("post-app-clone", []string{oldAppName, newAppName}...); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -167,6 +171,10 @@ func CommandRename(oldAppName string, newAppName string, skipDeploy bool) error
|
||||
os.Setenv("SKIP_REBUILD", "true")
|
||||
}
|
||||
|
||||
if err := common.PlugnTrigger("git-has-code", []string{newAppName}...); err != nil {
|
||||
os.Setenv("SKIP_REBUILD", "true")
|
||||
}
|
||||
|
||||
if err := common.PlugnTrigger("post-app-rename", []string{oldAppName, newAppName}...); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -22,7 +22,7 @@ trigger-builder-dockerfile-core-post-extract() {
|
||||
|
||||
if [[ ! -f "$NEW_DOCKERFILE" ]]; then
|
||||
dokku_log_fail "Invalid dockerfile-path specified: $NEW_DOCKERFILE"
|
||||
return
|
||||
return 1
|
||||
fi
|
||||
|
||||
mv "$NEW_DOCKERFILE" Dockerfile
|
||||
|
||||
@@ -157,7 +157,7 @@ dokku_log_fail_quiet() {
|
||||
if [[ -z "$DOKKU_QUIET_OUTPUT" ]]; then
|
||||
echo " ! $*" 1>&2
|
||||
fi
|
||||
exit ${DOKKU_FAIL_EXIT_CODE:=1}
|
||||
exit "${DOKKU_FAIL_EXIT_CODE:=1}"
|
||||
}
|
||||
|
||||
dokku_log_fail() {
|
||||
@@ -708,7 +708,9 @@ dokku_receive() {
|
||||
fi
|
||||
|
||||
DOKKU_QUIET_OUTPUT=1 config_set --no-restart "$APP" DOKKU_APP_TYPE="$IMAGE_SOURCE_TYPE"
|
||||
dokku_build "$APP" "$IMAGE_SOURCE_TYPE" "$TMP_WORK_DIR"
|
||||
if ! dokku_build "$APP" "$IMAGE_SOURCE_TYPE" "$TMP_WORK_DIR"; then
|
||||
return 1
|
||||
fi
|
||||
plugn trigger release-and-deploy "$APP"
|
||||
}
|
||||
|
||||
|
||||
@@ -44,6 +44,7 @@ git_build_app_repo() {
|
||||
else
|
||||
git_trigger_build "$APP" "$GIT_BUILD_APP_REPO_TMP_WORK_DIR" "$REV" | sed -u "s/^/"$'\e[1G'"/"
|
||||
fi
|
||||
return $?
|
||||
}
|
||||
|
||||
git_trigger_build() {
|
||||
@@ -51,7 +52,9 @@ git_trigger_build() {
|
||||
declare APP="$1" TMP_WORK_DIR="$2" REV="$3"
|
||||
local BUILDER
|
||||
|
||||
plugn trigger core-post-extract "$APP" "$TMP_WORK_DIR" "$REV"
|
||||
if ! plugn trigger core-post-extract "$APP" "$TMP_WORK_DIR" "$REV"; then
|
||||
return 1
|
||||
fi
|
||||
plugn trigger post-extract "$APP" "$TMP_WORK_DIR" "$REV"
|
||||
|
||||
BUILDER="$(plugn trigger builder-detect "$APP" "$TMP_WORK_DIR" | head -n1 || true)"
|
||||
@@ -96,8 +99,7 @@ cmd-git-hook() {
|
||||
plugn trigger deploy-source-set "$APP" "git-push" "$newrev"
|
||||
else
|
||||
if [[ $(find "$PLUGIN_PATH"/enabled/*/receive-branch 2>/dev/null | wc -l) != 1 ]]; then
|
||||
# shellcheck disable=SC2086
|
||||
plugn trigger receive-branch $APP $newrev $refname
|
||||
plugn trigger receive-branch "$APP" "$newrev" "$refname"
|
||||
plugn trigger deploy-source-set "$APP" "git-push" "$newrev"
|
||||
elif [[ -z "$(fn-git-deploy-branch "$APP" "")" ]]; then
|
||||
echo $'\e[1G\e[K'"-----> Set ${refname/refs\/heads\//} to DOKKU_DEPLOY_BRANCH."
|
||||
@@ -116,7 +118,7 @@ git_build() {
|
||||
declare desc="setup and call git_build_app_repo"
|
||||
local APP="$1" REV="$2"
|
||||
local DOKKU_DEPLOY_BRANCH ENV_VAR_NAME REF
|
||||
if [[ $# -ge 2 ]]; then
|
||||
if [[ -n "$REV" ]]; then
|
||||
ENV_VAR_NAME="$(fn-plugin-property-get "git" "$APP" "rev-env-var")"
|
||||
if [[ -z "$ENV_VAR_NAME" ]] && ! fn-plugin-property-exists "git" "$APP" "rev-env-var"; then
|
||||
ENV_VAR_NAME="GIT_REV"
|
||||
@@ -130,14 +132,17 @@ git_build() {
|
||||
DOKKU_DEPLOY_BRANCH="$(fn-git-deploy-branch "$APP")"
|
||||
if [[ -f "$DOKKU_ROOT/$APP/refs/heads/$DOKKU_DEPLOY_BRANCH" ]]; then
|
||||
REF=$(<"$DOKKU_ROOT/$APP/refs/heads/$DOKKU_DEPLOY_BRANCH")
|
||||
else
|
||||
echo $'\e[1G\e[K'"-----> WARNING: Cannot build from '$DOKKU_DEPLOY_BRANCH' as there are no commits stored at that branch."
|
||||
fi
|
||||
fi
|
||||
|
||||
if [[ -z "$REF" ]]; then
|
||||
return
|
||||
return 1
|
||||
fi
|
||||
# shellcheck disable=SC2086
|
||||
git_build_app_repo $APP $REF
|
||||
|
||||
git_build_app_repo "$APP" "$REF"
|
||||
return $?
|
||||
}
|
||||
|
||||
git_receive_app() {
|
||||
@@ -148,10 +153,11 @@ git_receive_app() {
|
||||
if [[ ! -d "$DOKKU_ROOT/$APP/refs" ]]; then
|
||||
true
|
||||
else
|
||||
local exit_code=0
|
||||
acquire_app_deploy_lock "$APP" "exclusive"
|
||||
# shellcheck disable=SC2086
|
||||
git_build $APP $REV
|
||||
git_build "$APP" "$REV" || exit_code="$?"
|
||||
release_app_deploy_lock "$APP"
|
||||
return "$exit_code"
|
||||
fi
|
||||
}
|
||||
|
||||
|
||||
18
plugins/git/git-has-code
Executable file
18
plugins/git/git-has-code
Executable file
@@ -0,0 +1,18 @@
|
||||
#!/usr/bin/env bash
|
||||
source "$PLUGIN_AVAILABLE_PATH/git/internal-functions"
|
||||
set -eo pipefail
|
||||
[[ $DOKKU_TRACE ]] && set -x
|
||||
|
||||
trigger-git-git-has-code() {
|
||||
declare desc="checks if there is code at a specific branch"
|
||||
declare trigger="git-has-code"
|
||||
declare APP="$1" BRANCH_NAME="$2"
|
||||
|
||||
if fn-git-has-code "$APP" "$BRANCH_NAME"; then
|
||||
return 0
|
||||
fi
|
||||
|
||||
return 1
|
||||
}
|
||||
|
||||
trigger-git-git-has-code "$@"
|
||||
@@ -110,7 +110,9 @@ cmd-git-from-image() {
|
||||
verify_app_name "$APP"
|
||||
[[ -z "$DOCKER_IMAGE" ]] && dokku_log_fail "Please specify a docker image"
|
||||
|
||||
plugn trigger git-from-image "$APP" "$DOCKER_IMAGE" "$BUILD_DIR" "$USER_NAME" "$USER_EMAIL"
|
||||
if ! plugn trigger git-from-image "$APP" "$DOCKER_IMAGE" "$BUILD_DIR" "$USER_NAME" "$USER_EMAIL"; then
|
||||
return 1
|
||||
fi
|
||||
plugn trigger deploy-source-set "$APP" "docker-image" "$DOCKER_IMAGE"
|
||||
}
|
||||
|
||||
@@ -405,6 +407,22 @@ fn-git-fetch() {
|
||||
fn-git-cmd "$APP_ROOT" config --add core.bare true
|
||||
}
|
||||
|
||||
fn-git-has-code() {
|
||||
declare desc="checks if there is code at a specific branch"
|
||||
declare APP="$1" DOKKU_DEPLOY_BRANCH="$2"
|
||||
|
||||
if [[ -z "$DOKKU_DEPLOY_BRANCH" ]]; then
|
||||
DOKKU_DEPLOY_BRANCH="$(fn-git-deploy-branch "$APP")"
|
||||
fi
|
||||
|
||||
local HEAD_FILE="$DOKKU_ROOT/$APP/refs/heads/$DOKKU_DEPLOY_BRANCH"
|
||||
if [[ -f "$HEAD_FILE" ]]; then
|
||||
return 0
|
||||
fi
|
||||
|
||||
return 1
|
||||
}
|
||||
|
||||
fn-git-last-updated-at() {
|
||||
declare desc="retrieve the deploy branch for a given application"
|
||||
declare APP="$1"
|
||||
|
||||
@@ -3,4 +3,12 @@ source "$PLUGIN_AVAILABLE_PATH/git/functions"
|
||||
set -eo pipefail
|
||||
[[ $DOKKU_TRACE ]] && set -x
|
||||
|
||||
git_receive_app "$@"
|
||||
trigger-git-receive-app() {
|
||||
declare desc="builds the app from the local git repository"
|
||||
declare trigger="receive-app"
|
||||
|
||||
git_receive_app "$@"
|
||||
return $?
|
||||
}
|
||||
|
||||
trigger-git-receive-app "$@"
|
||||
|
||||
@@ -12,6 +12,29 @@ teardown() {
|
||||
global_teardown
|
||||
}
|
||||
|
||||
@test "(git) push to non-deploy branch" {
|
||||
run /bin/bash -c "dokku git:set $TEST_APP deploy-branch custom-branch"
|
||||
echo "output: $output"
|
||||
echo "status: $status"
|
||||
assert_success
|
||||
|
||||
GIT_REMOTE_BRANCH=custom-branch run deploy_app
|
||||
echo "output: $output"
|
||||
echo "status: $status"
|
||||
assert_success
|
||||
|
||||
run /bin/bash -c "dokku git:set $TEST_APP deploy-branch non-existing-branch"
|
||||
echo "output: $output"
|
||||
echo "status: $status"
|
||||
assert_success
|
||||
|
||||
run /bin/bash -c "dokku ps:rebuild $TEST_APP"
|
||||
echo "output: $output"
|
||||
echo "status: $status"
|
||||
assert_output_contains "Cannot build from 'non-existing-branch' as there are no commits stored at that branch."
|
||||
assert_failure
|
||||
}
|
||||
|
||||
@test "(git) deploy specific branch" {
|
||||
run /bin/bash -c "dokku git:set --global deploy-branch global-branch"
|
||||
echo "output: $output"
|
||||
|
||||
Reference in New Issue
Block a user