From 0aeee7ea28d7678f45be7fa5708fb91a5c21a108 Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Thu, 26 Jan 2017 00:32:23 -0700 Subject: [PATCH] Allow specifying the deploy branch via DOKKU_DEPLOY_BRANCH Closes #2494 --- docs/deployment/application-deployment.md | 12 ++++++++++ plugins/git/functions | 27 +++++++++++++++++++++-- tests/unit/20_config.bats | 14 ++++++++++++ tests/unit/test_helper.bash | 11 ++++----- 4 files changed, 57 insertions(+), 7 deletions(-) diff --git a/docs/deployment/application-deployment.md b/docs/deployment/application-deployment.md index 903add341..c8f0d2e9a 100644 --- a/docs/deployment/application-deployment.md +++ b/docs/deployment/application-deployment.md @@ -90,6 +90,18 @@ Dokku supports deploying applications via [Heroku buildpacks](https://devcenter. Dokku only supports deploying from its master branch, so if you'd like to deploy a different local branch use: ```git push dokku :master``` +An alternative is to use the `DOKKU_DEPLOY_BRANCH` application config value to specify a branch that should be deployed. The implicit default is master, and this can be modified both at the app and global level: + +```shell +# on your Dokku host + +# set it globally +dokku config:set --global DOKKU_DEPLOY_BRANCH=some-branch + +# override for a specific app +dokku config:set ruby-rails-sample DOKKU_DEPLOY_BRANCH=some-branch +``` + You can also support pushing multiple branches using the [receive-branch](/dokku/development/plugin-triggers/#receive-branch) plugin trigger in a custom plugin. ### Skipping deployment diff --git a/plugins/git/functions b/plugins/git/functions index 96ce9b9aa..5e0da3726 100755 --- a/plugins/git/functions +++ b/plugins/git/functions @@ -55,16 +55,39 @@ suppress_output() { return 0 } +git_deploy_branch() { + declare desc="retrieve the deploy branch for a given application" + local cmd="git-hook" + local APP="$1" + + local DOKKU_DEPLOY_BRANCH="$(config_get "$APP" DOKKU_DEPLOY_BRANCH || true)" + local DOKKU_GLOBAL_DEPLOY_BRANCH="$(config_get --global DOKKU_DEPLOY_BRANCH || true)" + if [[ -n "$DOKKU_DEPLOY_BRANCH" ]]; then + echo "$DOKKU_DEPLOY_BRANCH" + elif [[ -n "$DOKKU_GLOBAL_DEPLOY_BRANCH" ]]; then + echo "$DOKKU_GLOBAL_DEPLOY_BRANCH" + else + echo "master" + fi +} + git_hook_cmd() { declare desc="kick off receive-app trigger from git prereceive hook" local cmd="git-hook" - local APP="$2" + local APP="$2" + local DOKKU_DEPLOY_BRANCH="$(git_deploy_branch "$APP")" + + if ! git check-ref-format --branch "$DOKKU_DEPLOY_BRANCH" 2> /dev/null; then + echo $'\e[1G\e[K'"-----> WARNING: Invalid branch name '$DOKKU_DEPLOY_BRANCH' specified via DOKKU_DEPLOY_BRANCH." + echo $'\e[1G\e[K'"-----> For more details, please see the man page for 'git-check-ref-format.'" + return + fi local oldrev newrev refname while read -r oldrev newrev refname; do # Only run this script for the master branch. You can remove this # if block if you wish to run it for others as well. - if [[ $refname = "refs/heads/master" ]]; then + if [[ $refname = "refs/heads/${DOKKU_DEPLOY_BRANCH}" ]]; then # broken out into plugin so we might support other methods to receive an app # shellcheck disable=SC2086 plugn trigger receive-app $APP $newrev diff --git a/tests/unit/20_config.bats b/tests/unit/20_config.bats index a5894372c..87dafa332 100644 --- a/tests/unit/20_config.bats +++ b/tests/unit/20_config.bats @@ -145,3 +145,17 @@ teardown() { echo "status: "$status assert_success } + +@test "(config) deploy specific DOKKU_DEPLOY_BRANCH" { + run ssh dokku@dokku.me config:set --global DOKKU_DEPLOY_BRANCH=global-branch + GIT_REMOTE_BRANCH=global-branch deploy_app + echo "output: "$output + echo "status: "$status + assert_success + + run ssh dokku@dokku.me config:set $TEST_APP DOKKU_DEPLOY_BRANCH=app-branch + GIT_REMOTE_BRANCH=app-branch deploy_app + echo "output: "$output + echo "status: "$status + assert_success +} diff --git a/tests/unit/test_helper.bash b/tests/unit/test_helper.bash index e6d16da86..b59841339 100644 --- a/tests/unit/test_helper.bash +++ b/tests/unit/test_helper.bash @@ -196,10 +196,11 @@ assert_http_redirect() { } deploy_app() { - local APP_TYPE="$1"; local APP_TYPE=${APP_TYPE:="nodejs-express"} - local GIT_REMOTE="$2"; local GIT_REMOTE=${GIT_REMOTE:="dokku@dokku.me:$TEST_APP"} - local CUSTOM_TEMPLATE="$3"; local TMP=$(mktemp -d "/tmp/dokku.me.XXXXX") - local CUSTOM_PATH="$4" + declare APP_TYPE="$1" GIT_REMOTE="$2" CUSTOM_TEMPLATE="$3" CUSTOM_PATH="$4" + local APP_TYPE=${APP_TYPE:="nodejs-express"} + local GIT_REMOTE=${GIT_REMOTE:="dokku@dokku.me:$TEST_APP"} + local GIT_REMOTE_BRANCH=${GIT_REMOTE_BRANCH:="master"} + local TMP=$(mktemp -d "/tmp/dokku.me.XXXXX") rmdir "$TMP" && cp -r "./tests/apps/$APP_TYPE" "$TMP" @@ -218,7 +219,7 @@ deploy_app() { [[ -f gitignore ]] && mv gitignore .gitignore git add . git commit -m 'initial commit' - git push target master || destroy_app $? + git push target "master:${GIT_REMOTE_BRANCH}" || destroy_app $? } setup_client_repo() {