From 399073f77b1db34f958a691aefad17d4b5b41b83 Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Wed, 28 Feb 2018 02:27:29 -0500 Subject: [PATCH] feat: add support for checking if an application exists This is useful for creating review applications in CI/CD pipelines. --- docs/deployment/application-management.md | 15 +++++++++++++++ plugins/apps/functions | 9 +++++++++ plugins/apps/subcommands/exists | 11 +++++++++++ tests/unit/10_apps.bats | 19 +++++++++++++++++++ 4 files changed, 54 insertions(+) create mode 100755 plugins/apps/subcommands/exists diff --git a/docs/deployment/application-management.md b/docs/deployment/application-management.md index b1e809205..6caa19a58 100644 --- a/docs/deployment/application-management.md +++ b/docs/deployment/application-management.md @@ -6,6 +6,7 @@ apps:clone # Clones an app apps:create # Create a new app apps:destroy # Permanently destroy an app +apps:exists # Checks if an app exists apps:list # List your apps apps:rename # Rename an app apps:report [] [] # Display report about an app @@ -40,6 +41,20 @@ node-js-app python-app ``` +### Checking if an application exists + +For CI/CD pipelines, it may be useful to see if an application exists before creating a "review" application for a specific branch. You can do so via the `apps:exists` command: + +```shell +dokku apps:exists node-js-app +``` + +``` +App does not exist +``` + +The `apps:exists` command will return non-zero if the application does not exist, and zero if it does. + ### Manually creating an application A common pattern for deploying applications to Dokku is to configure an application before deploying it. You can do so via the `apps:create` command: diff --git a/plugins/apps/functions b/plugins/apps/functions index 815aa4d74..97eafdd00 100755 --- a/plugins/apps/functions +++ b/plugins/apps/functions @@ -49,3 +49,12 @@ apps_destroy() { plugn trigger post-delete "$APP" "$IMAGE_TAG" } + +apps_exists() { + declare desc="checks if an app exists" + declare APP="$1" + is_valid_app_name "$APP" + + [[ -d "$DOKKU_ROOT/$APP" ]] || dokku_log_fail "App does not exist" + dokku_log_info1 "App already exists" +} diff --git a/plugins/apps/subcommands/exists b/plugins/apps/subcommands/exists new file mode 100755 index 000000000..792590fa7 --- /dev/null +++ b/plugins/apps/subcommands/exists @@ -0,0 +1,11 @@ +#!/usr/bin/env bash +set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x +source "$PLUGIN_AVAILABLE_PATH/apps/functions" + +apps_exists_cmd() { + declare desc="checks if an app exists" + local cmd="apps:exists" + apps_exists "$2" +} + +apps_exists_cmd "$@" diff --git a/tests/unit/10_apps.bats b/tests/unit/10_apps.bats index 4cc4db573..d0ff646f2 100644 --- a/tests/unit/10_apps.bats +++ b/tests/unit/10_apps.bats @@ -132,3 +132,22 @@ teardown () { echo "status: "$status assert_success } + +@test "(apps) apps:exists" { + run dokku apps:exists $TEST_APP + echo "output: "$output + echo "status: "$status + assert_failure + + create_app + run dokku apps:exists $TEST_APP + echo "output: "$output + echo "status: "$status + assert_success + + run bash -c "dokku --force apps:destroy $TEST_APP" + echo "output: "$output + echo "status: "$status + assert_success + +} \ No newline at end of file