feat: add ability to only build synced repo if there are changes

Closes #6242
This commit is contained in:
Jose Diaz-Gonzalez
2024-03-14 03:49:46 -04:00
parent e470a880f9
commit 4de97a1094
3 changed files with 88 additions and 2 deletions

View File

@@ -10,7 +10,7 @@ git:from-archive [--archive-type ARCHIVE_TYPE] <app> <archive-url> [<git-usernam
git:from-image [--build-dir DIRECTORY] <app> <docker-image> [<git-username> <git-email>] # Updates an app's git repository with a given docker image git:from-image [--build-dir DIRECTORY] <app> <docker-image> [<git-username> <git-email>] # Updates an app's git repository with a given docker image
git:generate-deploy-key # Generates a deploy ssh key git:generate-deploy-key # Generates a deploy ssh key
git:load-image [--build-dir DIRECTORY] <app> <docker-image> [<git-username> <git-email>] # Updates an app's git repository with a docker image loaded from stdin git:load-image [--build-dir DIRECTORY] <app> <docker-image> [<git-username> <git-email>] # Updates an app's git repository with a docker image loaded from stdin
git:sync [--build] <app> <repository> [<git-ref>] # Clone or fetch an app from remote git repo git:sync [--build|build-if-changes] <app> <repository> [<git-ref>] # Clone or fetch an app from remote git repo
git:initialize <app> # Initialize a git repository for an app git:initialize <app> # Initialize a git repository for an app
git:public-key # Outputs the dokku public deploy key git:public-key # Outputs the dokku public deploy key
git:report [<app>] [<flag>] # Displays a git report for one or more apps git:report [<app>] [<flag>] # 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 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 ### Initializing from private repositories
> [!IMPORTANT] > [!IMPORTANT]

View File

@@ -179,6 +179,7 @@ cmd-git-sync() {
local cmd="git:sync" local cmd="git:sync"
[[ "$1" == "$cmd" ]] && shift 1 [[ "$1" == "$cmd" ]] && shift 1
declare APP GIT_REMOTE GIT_REF FLAG declare APP GIT_REMOTE GIT_REF FLAG
local CURRENT_REF DOKKU_DEPLOY_BRANCH SHOULD_BUILD UPDATED_REF
ARGS=() ARGS=()
for arg in "$@"; do for arg in "$@"; do
@@ -187,6 +188,11 @@ cmd-git-sync() {
continue continue
fi fi
if [[ "$arg" == "--build-if-changes" ]]; then
FLAG="--build-if-changes"
continue
fi
ARGS+=("$arg") ARGS+=("$arg")
done done
@@ -202,6 +208,11 @@ cmd-git-sync() {
fi fi
local APP_ROOT="$DOKKU_ROOT/$APP" 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 if [[ "$(fn-git-cmd "$APP_ROOT" count-objects)" == "0 objects, 0 kilobytes" ]]; then
dokku_log_info1_quiet "Cloning $APP from $GIT_REMOTE#$GIT_REF" 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"
@@ -209,7 +220,22 @@ cmd-git-sync() {
fn-git-fetch "$APP" "$GIT_REMOTE" "$GIT_REF" fn-git-fetch "$APP" "$GIT_REMOTE" "$GIT_REF"
fi 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 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 if [[ -n "$GIT_REF" ]]; then
GIT_REF="$(fn-git-cmd "$APP_ROOT" rev-parse "$GIT_REF")" GIT_REF="$(fn-git-cmd "$APP_ROOT" rev-parse "$GIT_REF")"
plugn trigger receive-app "$APP" "$GIT_REF" plugn trigger receive-app "$APP" "$GIT_REF"

View File

@@ -144,12 +144,18 @@ teardown() {
assert_success 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" run /bin/bash -c "dokku git:sync --build $TEST_APP https://github.com/dokku/smoke-test-app.git"
echo "output: $output" echo "output: $output"
echo "status: $status" echo "status: $status"
assert_success assert_success
assert_output_contains "Application deployed" 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]" { @test "(git) git:sync new [--build branch]" {
@@ -158,6 +164,12 @@ teardown() {
echo "status: $status" echo "status: $status"
assert_success assert_success
assert_output_contains "Application deployed" 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]" { @test "(git) git:sync new [--build tag]" {
@@ -166,6 +178,12 @@ teardown() {
echo "status: $status" echo "status: $status"
assert_success assert_success
assert_output_contains "Application deployed" 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]" { @test "(git) git:sync new [--build commit]" {
@@ -174,6 +192,12 @@ teardown() {
echo "status: $status" echo "status: $status"
assert_success assert_success
assert_output_contains "Application deployed" 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]" { @test "(git) git:sync existing [errors]" {
@@ -262,6 +286,12 @@ teardown() {
echo "status: $status" echo "status: $status"
assert_success assert_success
assert_output_contains "Application deployed" 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]" { @test "(git) git:sync existing [--build branch]" {
@@ -275,6 +305,12 @@ teardown() {
echo "status: $status" echo "status: $status"
assert_success assert_success
assert_output_contains "Application deployed" 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]" { @test "(git) git:sync existing [--build tag]" {
@@ -288,6 +324,12 @@ teardown() {
echo "status: $status" echo "status: $status"
assert_success assert_success
assert_output_contains "Application deployed" 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]" { @test "(git) git:sync existing [--build commit]" {
@@ -301,6 +343,12 @@ teardown() {
echo "status: $status" echo "status: $status"
assert_success assert_success
assert_output_contains "Application deployed" 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" { @test "(git) git:sync private" {
@@ -328,6 +376,12 @@ teardown() {
echo "output: $output" echo "output: $output"
echo "status: $status" echo "status: $status"
assert_success 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" { @test "(git) git:public-key" {