From 4de97a109456192cc642f4c7cff89388f4d57c1f Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Thu, 14 Mar 2024 03:49:46 -0400 Subject: [PATCH] feat: add ability to only build synced repo if there are changes Closes #6242 --- docs/deployment/methods/git.md | 8 ++++- plugins/git/internal-functions | 26 ++++++++++++++++ tests/unit/git_3.bats | 56 +++++++++++++++++++++++++++++++++- 3 files changed, 88 insertions(+), 2 deletions(-) diff --git a/docs/deployment/methods/git.md b/docs/deployment/methods/git.md index 1bfd498a4..eb46b3261 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] [] # Clone or fetch an app from remote git repo +git:sync [--build|build-if-changes] [] # 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 @@ -157,6 +157,12 @@ By default, this command does not trigger an application build. To do so during dokku git:sync --build node-js-app https://github.com/heroku/node-js-getting-started.git ``` +When running `git:sync` without a reference, it may be useful to only build when there are changes. To do so, specify the `--build-if-changes` flag. + +```shell +dokku git:sync --build-if-changes node-js-app https://github.com/heroku/node-js-getting-started.git +``` + ### Initializing from private repositories > [!IMPORTANT] diff --git a/plugins/git/internal-functions b/plugins/git/internal-functions index 8ce59e82f..11ad9e6a5 100755 --- a/plugins/git/internal-functions +++ b/plugins/git/internal-functions @@ -179,6 +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 ARGS=() for arg in "$@"; do @@ -187,6 +188,11 @@ cmd-git-sync() { continue fi + if [[ "$arg" == "--build-if-changes" ]]; then + FLAG="--build-if-changes" + continue + fi + ARGS+=("$arg") done @@ -202,6 +208,11 @@ cmd-git-sync() { fi local APP_ROOT="$DOKKU_ROOT/$APP" + + DOKKU_DEPLOY_BRANCH="$(fn-git-deploy-branch "$APP")" + CURRENT_REF="$(fn-git-cmd "$APP_ROOT" rev-parse "$DOKKU_DEPLOY_BRANCH" 2>/dev/null || true)" + dokku_log_verbose "Current ref for $APP is $CURRENT_REF" + if [[ "$(fn-git-cmd "$APP_ROOT" count-objects)" == "0 objects, 0 kilobytes" ]]; then dokku_log_info1_quiet "Cloning $APP from $GIT_REMOTE#$GIT_REF" fn-git-clone "$APP" "$GIT_REMOTE" "$GIT_REF" @@ -209,7 +220,22 @@ cmd-git-sync() { fn-git-fetch "$APP" "$GIT_REMOTE" "$GIT_REF" fi + UPDATED_REF="$(fn-git-cmd "$APP_ROOT" rev-parse "$DOKKU_DEPLOY_BRANCH" 2>/dev/null || true)" + dokku_log_verbose "Updated ref for $APP is $UPDATED_REF" + + local SHOULD_BUILD=false if [[ "$FLAG" == "--build" ]]; then + SHOULD_BUILD=true + elif [[ "$FLAG" == "--build-if-changes" ]]; then + if [[ "$CURRENT_REF" == "$UPDATED_REF" ]]; then + dokku_log_verbose "Skipping build as no changes were detected" + return + fi + + SHOULD_BUILD=true + fi + + if [[ "$SHOULD_BUILD" == "true" ]]; then if [[ -n "$GIT_REF" ]]; then GIT_REF="$(fn-git-cmd "$APP_ROOT" rev-parse "$GIT_REF")" plugn trigger receive-app "$APP" "$GIT_REF" diff --git a/tests/unit/git_3.bats b/tests/unit/git_3.bats index 1cec55596..0e867486a 100644 --- a/tests/unit/git_3.bats +++ b/tests/unit/git_3.bats @@ -144,12 +144,18 @@ teardown() { assert_success } -@test "(git) git:sync new [--build noarg]" { +@test "(git) git:sync new [--build noarg] 7" { run /bin/bash -c "dokku git:sync --build $TEST_APP https://github.com/dokku/smoke-test-app.git" echo "output: $output" echo "status: $status" assert_success assert_output_contains "Application deployed" + + run /bin/bash -c "dokku git:sync --build-if-changes $TEST_APP https://github.com/dokku/smoke-test-app.git" + echo "output: $output" + echo "status: $status" + assert_success + assert_output_contains "Skipping build as no changes were detected" } @test "(git) git:sync new [--build branch]" { @@ -158,6 +164,12 @@ teardown() { echo "status: $status" assert_success assert_output_contains "Application deployed" + + run /bin/bash -c "dokku git:sync --build-if-changes $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" } @test "(git) git:sync new [--build tag]" { @@ -166,6 +178,12 @@ teardown() { echo "status: $status" assert_success assert_output_contains "Application deployed" + + run /bin/bash -c "dokku git:sync --build-if-changes $TEST_APP https://github.com/dokku/smoke-test-app.git 1.0.0" + echo "output: $output" + echo "status: $status" + assert_success + assert_output_contains "Skipping build as no changes were detected" } @test "(git) git:sync new [--build commit]" { @@ -174,6 +192,12 @@ teardown() { echo "status: $status" assert_success assert_output_contains "Application deployed" + + run /bin/bash -c "dokku git:sync --build-if-changes $TEST_APP https://github.com/dokku/smoke-test-app.git 5c8a5e42bbd7fae98bd657fb17f41c6019b303f9" + echo "output: $output" + echo "status: $status" + assert_success + assert_output_contains "Skipping build as no changes were detected" } @test "(git) git:sync existing [errors]" { @@ -262,6 +286,12 @@ teardown() { echo "status: $status" assert_success assert_output_contains "Application deployed" + + run /bin/bash -c "dokku git:sync --build-if-changes $TEST_APP https://github.com/dokku/smoke-test-app.git" + echo "output: $output" + echo "status: $status" + assert_success + assert_output_contains "Skipping build as no changes were detected" } @test "(git) git:sync existing [--build branch]" { @@ -275,6 +305,12 @@ teardown() { echo "status: $status" assert_success assert_output_contains "Application deployed" + + run /bin/bash -c "dokku git:sync --build-if-changes $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" } @test "(git) git:sync existing [--build tag]" { @@ -288,6 +324,12 @@ teardown() { echo "status: $status" assert_success assert_output_contains "Application deployed" + + run /bin/bash -c "dokku git:sync --build-if-changes $TEST_APP https://github.com/dokku/smoke-test-app.git 2.0.0" + echo "output: $output" + echo "status: $status" + assert_success + assert_output_contains "Skipping build as no changes were detected" } @test "(git) git:sync existing [--build commit]" { @@ -301,6 +343,12 @@ teardown() { echo "status: $status" assert_success assert_output_contains "Application deployed" + + run /bin/bash -c "dokku git:sync --build-if-changes $TEST_APP https://github.com/dokku/smoke-test-app.git 5c8a5e42bbd7fae98bd657fb17f41c6019b303f9" + echo "output: $output" + echo "status: $status" + assert_success + assert_output_contains "Skipping build as no changes were detected" } @test "(git) git:sync private" { @@ -328,6 +376,12 @@ teardown() { echo "output: $output" echo "status: $status" assert_success + + run /bin/bash -c "dokku git:sync --build-if-changes $TEST_APP https://github.com/dokku/smoke-test-app-private.git" + echo "output: $output" + echo "status: $status" + assert_success + assert_output_contains "Skipping build as no changes were detected" } @test "(git) git:public-key" {