Teardown script or remote flyctl command

I’m using a GitHub action to setup a staging environment from pull requests, which is working great. I’m starting to look into the teardown side of it when the pull request is closed and can’t figure out a good way to run a custom script before the app is destroyed.

In my staging toml config I run the setup script like so:

  release_command = "/app/bin/setup-staging"

I’d like to be able to do the opposite of this. Is there a way to either have a “teardown” command that runs when the app is destroyed or run a remote command on the server using flyctl that I can run before running flyctl apps destroy?

You can run a command using flyctl ssh console -C, e.g.:

flyctl ssh console -a your-app-name -C "./app/bin/some_script"

That will open a console, run the command specified by -C, and then exit. Note that you might need to do something funny to handle exit codes, since I’m pretty sure flyctl ssh console won’t exit with the code that the script you run exits with, but rather with just 0 unless connecting via ssh fails for whatever reason.

1 Like

That’s exactly what I was looking for. Thanks!