Skip to content

Commit

Permalink
Merge pull request #118 from dokku/deploy-hooks
Browse files Browse the repository at this point in the history
feat: add review-app destroy hooks and a post-deploy hook
  • Loading branch information
josegonzalez authored Sep 14, 2024
2 parents 04d6ba0 + 2e3716d commit c3b0e87
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 6 deletions.
41 changes: 35 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
19 changes: 19 additions & 0 deletions bin/dokku-deploy
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

0 comments on commit c3b0e87

Please sign in to comment.