From 68c0d7e96403512b2db7f51282b38aa6946793b3 Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Sat, 2 Apr 2022 04:22:44 -0400 Subject: [PATCH 1/5] feat: warn and exit non-zero when attempting to rebuild from a non-existent branch Closes #4898 --- plugins/git/functions | 18 ++++++++++-------- plugins/git/receive-app | 10 +++++++++- tests/unit/git_2.bats | 23 +++++++++++++++++++++++ 3 files changed, 42 insertions(+), 9 deletions(-) diff --git a/plugins/git/functions b/plugins/git/functions index 5603aeb96..35181407e 100755 --- a/plugins/git/functions +++ b/plugins/git/functions @@ -96,8 +96,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 +115,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 +129,16 @@ 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" } git_receive_app() { @@ -148,10 +149,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 } diff --git a/plugins/git/receive-app b/plugins/git/receive-app index 1c09982b4..ef7f59af1 100755 --- a/plugins/git/receive-app +++ b/plugins/git/receive-app @@ -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 "$@" diff --git a/tests/unit/git_2.bats b/tests/unit/git_2.bats index 8a65f2ab6..9b13c1632 100644 --- a/tests/unit/git_2.bats +++ b/tests/unit/git_2.bats @@ -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" From 1b6861cdbb819cf4206f48d65c676334cb2917de Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Sun, 3 Apr 2022 13:20:50 -0400 Subject: [PATCH 2/5] feat: add plugin trigger for checking if an app has code This will allow us to short-circuit build processes that would otherwise be triggered due to various commands in Dokku. --- docs/development/plugin-triggers.md | 14 ++++++++++++++ plugins/git/git-has-code | 18 ++++++++++++++++++ plugins/git/internal-functions | 16 ++++++++++++++++ 3 files changed, 48 insertions(+) create mode 100755 plugins/git/git-has-code diff --git a/docs/development/plugin-triggers.md b/docs/development/plugin-triggers.md index 9014822f7..4326d8adb 100644 --- a/docs/development/plugin-triggers.md +++ b/docs/development/plugin-triggers.md @@ -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: diff --git a/plugins/git/git-has-code b/plugins/git/git-has-code new file mode 100755 index 000000000..083120278 --- /dev/null +++ b/plugins/git/git-has-code @@ -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 "$@" diff --git a/plugins/git/internal-functions b/plugins/git/internal-functions index 845a64370..43f545d23 100755 --- a/plugins/git/internal-functions +++ b/plugins/git/internal-functions @@ -405,6 +405,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" From eec49878a5cf0d686a90e8655b2e297463c05c25 Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Sun, 3 Apr 2022 15:05:43 -0400 Subject: [PATCH 3/5] fix: skip rebuild during clone/rename if there is no code --- plugins/apps/subcommands.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/plugins/apps/subcommands.go b/plugins/apps/subcommands.go index bac59eb94..d8eef9ba2 100644 --- a/plugins/apps/subcommands.go +++ b/plugins/apps/subcommands.go @@ -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 } From ba7c685e1191d175ecf87ef2d1064487864f9e88 Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Sat, 16 Apr 2022 02:25:48 -0400 Subject: [PATCH 4/5] fix: pass failed exit codes around as necessary --- plugins/builder-dockerfile/core-post-extract | 2 +- plugins/git/functions | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/plugins/builder-dockerfile/core-post-extract b/plugins/builder-dockerfile/core-post-extract index c121c86bb..5334e8d3a 100755 --- a/plugins/builder-dockerfile/core-post-extract +++ b/plugins/builder-dockerfile/core-post-extract @@ -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 diff --git a/plugins/git/functions b/plugins/git/functions index 35181407e..c10bc72d0 100755 --- a/plugins/git/functions +++ b/plugins/git/functions @@ -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)" @@ -139,6 +142,7 @@ git_build() { fi git_build_app_repo "$APP" "$REF" + return $? } git_receive_app() { From 49d08a8fb66f6f2555451bf08321cfd618e8278a Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Sat, 16 Apr 2022 02:44:51 -0400 Subject: [PATCH 5/5] fix: pass along failed build status when building from image --- plugins/common/functions | 6 ++++-- plugins/git/internal-functions | 4 +++- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/plugins/common/functions b/plugins/common/functions index ce1460496..1bda3930e 100755 --- a/plugins/common/functions +++ b/plugins/common/functions @@ -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" } diff --git a/plugins/git/internal-functions b/plugins/git/internal-functions index 43f545d23..d7f1dfe82 100755 --- a/plugins/git/internal-functions +++ b/plugins/git/internal-functions @@ -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" }