From dd70fb823d27b889247cd7f43ce2d8d0c45c61d2 Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Thu, 8 Jan 2026 02:19:22 -0500 Subject: [PATCH] feat: add the ability to skip setting the deploy-branch when running git:sync Closes #8212 --- docs/deployment/methods/git.md | 8 ++- plugins/git/help-functions | 2 +- plugins/git/internal-functions | 19 ++++-- tests/unit/git_3.bats | 102 +++++++++++++++++++++++++++++++++ 4 files changed, 124 insertions(+), 7 deletions(-) diff --git a/docs/deployment/methods/git.md b/docs/deployment/methods/git.md index eb46b3261..b27371a07 100644 --- a/docs/deployment/methods/git.md +++ b/docs/deployment/methods/git.md @@ -10,7 +10,7 @@ git:from-archive [--archive-type ARCHIVE_TYPE] [ [ ] # Updates an app's git repository with a given docker image git:generate-deploy-key # Generates a deploy ssh key git:load-image [--build-dir DIRECTORY] [ ] # Updates an app's git repository with a docker image loaded from stdin -git:sync [--build|build-if-changes] [] # Clone or fetch an app from remote git repo +git:sync [--build|--build-if-changes] [--skip-deploy-branch] [] # Clone or fetch an app from remote git repo git:initialize # Initialize a git repository for an app git:public-key # Outputs the dokku public deploy key git:report [] [] # Displays a git report for one or more apps @@ -163,6 +163,12 @@ When running `git:sync` without a reference, it may be useful to only build when dokku git:sync --build-if-changes node-js-app https://github.com/heroku/node-js-getting-started.git ``` +By default, when running `git:sync` with a git branch reference, Dokku will automatically set the `deploy-branch` property to the specified branch. To skip setting the deploy branch, specify the `--skip-deploy-branch` flag. + +```shell +dokku git:sync --skip-deploy-branch node-js-app https://github.com/heroku/node-js-getting-started.git main +``` + ### Initializing from private repositories > [!IMPORTANT] diff --git a/plugins/git/help-functions b/plugins/git/help-functions index c0fd67da8..34d116c51 100755 --- a/plugins/git/help-functions +++ b/plugins/git/help-functions @@ -32,7 +32,7 @@ fn-help-content() { git:from-archive [ ], Updates an app's git repository with a given archive file git:from-image [ ], Updates an app's git repository with a given docker image git:load-image [ ], Updates an app's git repository with a docker image loaded from stdin - git:sync [--build] [], Clone or fetch an app from remote git repo + git:sync [--build|--build-if-changes] [--skip-deploy-branch] [], Clone or fetch an app from remote git repo git:initialize , Initialize a git repository for an app git:generate-deploy-key, Generates a deploy ssh key git:public-key, Outputs the dokku public deploy key diff --git a/plugins/git/internal-functions b/plugins/git/internal-functions index 5641cd923..585c899f2 100755 --- a/plugins/git/internal-functions +++ b/plugins/git/internal-functions @@ -179,7 +179,7 @@ cmd-git-sync() { local cmd="git:sync" [[ "$1" == "$cmd" ]] && shift 1 declare APP GIT_REMOTE GIT_REF FLAG - local CURRENT_REF DOKKU_DEPLOY_BRANCH SHOULD_BUILD UPDATED_REF + local CURRENT_REF DOKKU_DEPLOY_BRANCH SHOULD_BUILD SKIP_DEPLOY_BRANCH UPDATED_REF ARGS=() for arg in "$@"; do @@ -193,6 +193,11 @@ cmd-git-sync() { continue fi + if [[ "$arg" == "--skip-deploy-branch" ]]; then + SKIP_DEPLOY_BRANCH="true" + continue + fi + ARGS+=("$arg") done @@ -213,7 +218,7 @@ cmd-git-sync() { if ! fn-git-cmd "$APP_ROOT" rev-parse "$DOKKU_DEPLOY_BRANCH" &>/dev/null; then dokku_log_info1_quiet "Cloning $APP from $GIT_REMOTE#$GIT_REF" - fn-git-clone "$APP" "$GIT_REMOTE" "$GIT_REF" + fn-git-clone "$APP" "$GIT_REMOTE" "$GIT_REF" "$SKIP_DEPLOY_BRANCH" else dokku_log_verbose "Fetching remote code for $APP from $GIT_REMOTE#$GIT_REF" fn-git-fetch "$APP" "$GIT_REMOTE" "$GIT_REF" @@ -373,7 +378,7 @@ EOF fn-git-clone() { declare desc="creates an app from remote git repo" - declare APP="$1" GIT_REMOTE="$2" GIT_REF="$3" + declare APP="$1" GIT_REMOTE="$2" GIT_REF="$3" SKIP_DEPLOY_BRANCH="$4" local APP_ROOT="$DOKKU_ROOT/$APP" [[ -z "$APP" ]] && dokku_log_fail "Please specify an app to run the command on" @@ -404,8 +409,12 @@ fn-git-clone() { else GIT_TERMINAL_PROMPT=0 suppress_output git clone -n --branch "$GIT_REF" "$GIT_REMOTE" "$TMP_CLONE_DIR" if fn-git-cmd "$TMP_CLONE_DIR" show-ref --verify "refs/heads/$GIT_REF" &>/dev/null; then - dokku_log_verbose "Detected branch, setting deploy-branch to $GIT_REF" - fn-plugin-property-write "git" "$APP" "deploy-branch" "$GIT_REF" + if [[ "$SKIP_DEPLOY_BRANCH" != "true" ]]; then + dokku_log_verbose "Detected branch, setting deploy-branch to $GIT_REF" + fn-plugin-property-write "git" "$APP" "deploy-branch" "$GIT_REF" + else + dokku_log_verbose "Detected branch $GIT_REF, skipping deploy-branch setting" + fi fi fi diff --git a/tests/unit/git_3.bats b/tests/unit/git_3.bats index a86b2e09c..8e7119d41 100644 --- a/tests/unit/git_3.bats +++ b/tests/unit/git_3.bats @@ -629,3 +629,105 @@ teardown() { echo "status: $status" assert_success } + +@test "(git:sync) --skip-deploy-branch" { + run /bin/bash -c "dokku git:report $TEST_APP --git-deploy-branch" + echo "output: $output" + echo "status: $status" + assert_success + assert_output "master" + + run /bin/bash -c "dokku git:sync $TEST_APP https://github.com/dokku/smoke-test-app.git another-branch" + echo "output: $output" + echo "status: $status" + assert_success + assert_output_contains "Detected branch, setting deploy-branch to another-branch" + + run /bin/bash -c "dokku git:report $TEST_APP --git-deploy-branch" + echo "output: $output" + echo "status: $status" + assert_success + assert_output "another-branch" + + run destroy_app + echo "output: $output" + echo "status: $status" + assert_success + + run create_app + echo "output: $output" + echo "status: $status" + assert_success + + run /bin/bash -c "dokku git:report $TEST_APP --git-deploy-branch" + echo "output: $output" + echo "status: $status" + assert_success + assert_output "master" + + run /bin/bash -c "dokku git:sync --skip-deploy-branch $TEST_APP https://github.com/dokku/smoke-test-app.git another-branch" + echo "output: $output" + echo "status: $status" + assert_success + assert_output_contains "skipping deploy-branch setting" + + run /bin/bash -c "dokku git:report $TEST_APP --git-deploy-branch" + echo "output: $output" + echo "status: $status" + assert_success + assert_output "master" +} + +@test "(git:sync) --build --skip-deploy-branch" { + run /bin/bash -c "dokku git:report $TEST_APP --git-deploy-branch" + echo "output: $output" + echo "status: $status" + assert_success + assert_output "master" + + run /bin/bash -c "dokku git:sync --build --skip-deploy-branch $TEST_APP https://github.com/dokku/smoke-test-app.git another-branch" + echo "output: $output" + echo "status: $status" + assert_success + assert_output_contains "skipping deploy-branch setting" + assert_output_contains "Application deployed" + + run /bin/bash -c "dokku git:report $TEST_APP --git-deploy-branch" + echo "output: $output" + echo "status: $status" + assert_success + assert_output "master" +} + +@test "(git:sync) --build-if-changes --skip-deploy-branch" { + run /bin/bash -c "dokku git:report $TEST_APP --git-deploy-branch" + echo "output: $output" + echo "status: $status" + assert_success + assert_output "master" + + run /bin/bash -c "dokku git:sync --build-if-changes --skip-deploy-branch $TEST_APP https://github.com/dokku/smoke-test-app.git another-branch" + echo "output: $output" + echo "status: $status" + assert_success + assert_output_contains "skipping deploy-branch setting" + assert_output_contains "Application deployed" + + run /bin/bash -c "dokku git:report $TEST_APP --git-deploy-branch" + echo "output: $output" + echo "status: $status" + assert_success + assert_output "master" + + run /bin/bash -c "dokku git:sync --build-if-changes --skip-deploy-branch $TEST_APP https://github.com/dokku/smoke-test-app.git another-branch" + echo "output: $output" + echo "status: $status" + assert_success + assert_output_contains "Skipping build as no changes were detected" + + run /bin/bash -c "dokku git:report $TEST_APP --git-deploy-branch" + echo "output: $output" + echo "status: $status" + assert_success + assert_output "master" +}