diff --git a/README.md b/README.md index 1697acf..598c649 100644 --- a/README.md +++ b/README.md @@ -94,19 +94,41 @@ The following environment variables are supported: ### Hooks -#### bin/ci-pre-deploy +This image allows a variety of file-based hooks to be triggered during the app +deploy process. These hooks should be executables relative to the current working +directory in which `dokku-deploy` script is executed - typically your repository root. -If the file `bin/ci-pre-deploy` exists, it will be triggered after any app setup -but before the app is deployed. This can be used to preconfigure the remote -app prior to the actual deploy, but within the context of the SSH setup. The -following environment variables are available for usage in the script: +The following environment variables are available for usage in the script: - `APP_NAME`: The name of the remote app that will be deployed. This takes the parsed GIT_REMOTE_URL and REVIEW_APP_NAME into account. - `IS_REVIEW_APP`: `true` if a review app is being deployed, `false` otherwise. - `SSH_REMOTE`: The parsed ssh remote url. -The following is an example `bin/ci-pre-deploy` file: +The simplest hook is a shell script like so: + +```shell +#!/bin/sh -l + +echo "hello world" +``` + +> [!NOTE] +> The Docker image in use by this repository currently only supports `sh` as +> the interpreter. If another interpreter is desired, it should be added to the +> environment manually. + +To execute remote dokku commands, the `ssh` binary can be executed like so: + +```shell +#!/bin/sh -l + +ssh "$SSH_REMOTE" -- version +``` + +Additionally, if a Dokku command should be executed _only_ for review apps, +the `IS_REVIEW_APP` variable can be checked for the value `true` to wrap +review app-specific logic: ```shell #!/bin/sh -l @@ -116,6 +138,13 @@ if [ "$IS_REVIEW_APP" = "true" ]; then fi ``` +The following hooks are available: + +- `bin/ci-pre-deploy`: Triggered after any app setup but before the app is deployed +- `bin/ci-post-deploy`: Triggered after the app is deployed +- `bin/ci-pre-review-app-destroy`: Triggered before a review app is destroyed +- `bin/ci-post-review-app-destroy`: Triggered after a review app is deployed + ## Building ```text diff --git a/bin/dokku-deploy b/bin/dokku-deploy index cfce31f..bfd76a2 100755 --- a/bin/dokku-deploy +++ b/bin/dokku-deploy @@ -54,8 +54,21 @@ if [ "$COMMAND" = "review-apps:create" ] || [ "$COMMAND" = 'review-apps:destroy' fi if [ "$COMMAND" = "review-apps:destroy" ]; then + if [ -f "bin/ci-pre-review-app-destroy" ]; then + log-info "Executing bin/ci-pre-review-app-destroy script" + chmod +x bin/ci-pre-review-app-destroy + APP_NAME="$REVIEW_APP_NAME" IS_REVIEW_APP="true" SSH_REMOTE="$ssh_remote" bin/ci-pre-review-app-destroy + fi + log-info "Destroying review app '${REVIEW_APP_NAME}'" ssh "$ssh_remote" -- --force apps:destroy "$REVIEW_APP_NAME" + + if [ -f "bin/ci-post-review-app-destroy" ]; then + log-info "Executing bin/ci-post-review-app-destroy script" + chmod +x bin/ci-post-review-app-destroy + APP_NAME="$REVIEW_APP_NAME" IS_REVIEW_APP="true" SSH_REMOTE="$ssh_remote" bin/ci-post-review-app-destroy + fi + exit 0 fi @@ -103,3 +116,9 @@ else # shellcheck disable=SC2086 git push $GIT_PUSH_FLAGS "$GIT_REMOTE_URL" "$commit_sha:refs/heads/$BRANCH" fi + +if [ -f "bin/ci-post-deploy" ]; then + log-info "Executing bin/ci-post-deploy script" + chmod +x bin/ci-post-deploy + APP_NAME="$remote_app_name" IS_REVIEW_APP="$is_review_app" SSH_REMOTE="$ssh_remote" bin/ci-post-deploy +fi