Skip to content

Commit

Permalink
feat: automatically generate branch name if not specified
Browse files Browse the repository at this point in the history
Useful for auto-creating review apps
  • Loading branch information
Jose Diaz-Gonzalez committed Dec 4, 2020
1 parent f84ab1b commit 3b6b29a
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 9 deletions.
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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
Expand Down
37 changes: 32 additions & 5 deletions bin/dokku-deploy
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 3b6b29a

Please sign in to comment.