diff --git a/README.md b/README.md index 854f58f..9c71534 100644 --- a/README.md +++ b/README.md @@ -16,8 +16,12 @@ The following environment variables are supported: - `BRANCH`: description: The branch to deploy when pushing to Dokku required: false - default: "master" -- `command`: + default: ''master +- `CI_BRANCH_NAME` + description: The branch name that triggered the deploy + required: false + default: '' +- `COMMAND`: description: The command to run for the action required: false default: 'deploy' @@ -30,11 +34,11 @@ The following environment variables are supported: - `REVIEW_APP_NAME`: description: The name of the review app to create or destroy required: false - default: '' + default: 'review-$APP_NAME-$CI_BRANCH_NAME' - `SSH_HOST_KEY`: description: The results of running `ssh-keyscan -t rsa $HOST` required: false - default: "" + default: '' - `SSH_PRIVATE_KEY`: description: A private SSH key that has push acces to your Dokku instance required: true diff --git a/bin/dokku-deploy b/bin/dokku-deploy index 54115a8..7c04e0e 100755 --- a/bin/dokku-deploy +++ b/bin/dokku-deploy @@ -3,16 +3,43 @@ set -e setup-ssh +app_name="$(parse-app-name)" +ssh_remote="ssh://dokku@$(parse-ssh-host):$(parse-ssh-port)" + if [ "$COMMAND" = "review-apps:create" ] || [ "$COMMAND" = 'review-apps:destroy' ]; then if [ -z "$REVIEW_APP_NAME" ]; then - log-error "No review app name specified" - exit 1 + CI_BRANCH_NAME="" + if [ -n "$CI_COMMIT_REF_NAME" ]; then + # gitlab-ci + CI_BRANCH_NAME="$CI_COMMIT_REF_NAME" + elif [ -n "$GITHUB_REF" ]; then + # github actions + CI_BRANCH_NAME="${GITHUB_REF#refs/heads/}" + elif [ -n "$CIRCLE_BRANCH" ]; then + # circleci + CI_BRANCH_NAME="$CIRCLE_BRANCH" + elif [ -n "$TRAVIS_BRANCH" ]; then + # travisci + CI_BRANCH_NAME="$TRAVIS_BRANCH" + elif [ -n "$SEMAPHORE_GIT_BRANCH" ]; then + # semaphoreci + CI_BRANCH_NAME="SEMAPHORE_GIT_BRANCH" + elif [ -n "$CI_BRANCH" ]; then + # cloudbees + CI_BRANCH_NAME="CI_BRANCH" + elif [ -n "$DRONE_COMMIT_BRANCH" ]; then + # drone + CI_BRANCH_NAME="DRONE_COMMIT_BRANCH" + else + log-error "Unable to detect branch name and cannot generate review app name" + exit 1 + fi + + REVIEW_APP_NAME="review-${app_name}-${CI_BRANCH_NAME}" + log-info "No review app name specified, using $REVIEW_APP_NAME" fi fi -app_name="$(parse-app-name)" -ssh_remote="ssh://dokku@$(parse-ssh-host):$(parse-ssh-port)" - if [ "$COMMAND" = "review-app:destroy" ]; then log-info "Destroying review app '${REVIEW_APP_NAME}'" ssh "$ssh_remote" -- --force apps:destroy "$REVIEW_APP_NAME"