Spin up a Fly.io preview environment for every pull request
The moment a pull request opens, we launch a live preview of that branch on Fly.io and post the link right on the PR and in your team's Slack channel.
Trigger this workflow with a webhook whenever a pull request is opened in my GitHub repository. Read the pull request number, the head branch name, and the repository from the webhook payload so the rest of the run can reference them.
Let me configure two settings up front: the name of my Fly.io preview app that should host the branch, and the container image the Machine should boot from. Use those two values everywhere below.
When the pull request opens, call the Fly.io Create Machine operation in my preview app to launch a Machine for this branch, booting it from the configured image. Tag the Machine with the pull request number in the Machine metadata so I can always tell which preview belongs to which pull request.
Fly.io can refuse to place a Machine when the target region is out of capacity, and the API does not queue the request, so you own the retries. If Create Machine cannot place the Machine, use the Fly.io Get Placements operation to see which regions currently have room, then retry Create Machine in a fallback region. Work through a few sensible regions before giving up. If every attempt fails, post a comment on the pull request explaining that no region had capacity right now.
Once the Machine is created, use the Fly.io Wait for State operation to block until it reaches the started state, so I only share the preview once it is actually running.
When the Machine is running, build the preview URL from the configured preview app name (the app's public Fly.io URL) and post it back onto the pull request using the GitHub Create an Issue Comment operation, addressed to the pull request number. Keep the comment short: the preview link plus one line telling reviewers they can click straight through.
Then send a short heads-up to my team's Slack channel with the Slack Bot Send a Message operation, including the pull request title, its number, and the preview link, so the team can jump straight into review.
Additional information
What does this prompt do?
- Watches your repository and reacts the moment a pull request is opened, with nothing to kick off by hand.
- Launches a fresh, isolated preview of that branch on Fly.io so reviewers can click through the actual running app instead of imagining the change.
- Waits until the preview is fully up, then drops the link as a comment on the pull request and sends a heads-up to your team's Slack channel.
- Automatically retries in another region if the first location is out of capacity, so the preview still comes up instead of quietly failing.
What do I need to use this?
- A Fly.io account with a preview app set aside to host branch builds.
- A container image for your app that the preview should boot from.
- A GitHub repository where teammates open pull requests.
- A Slack workspace and a channel where the review heads-up should land.
How can I customize it?
- Point it at the Fly.io app you want previews to run in.
- Choose which container image each preview boots from.
- Change which Slack channel gets pinged and reword the message to match your team's voice.
Frequently asked questions
When does the preview get created?
What happens if the first region is out of capacity?
Where do I find the preview link?
Can I use my own app image?
Does the preview wait until the app is actually running?
Related templates
Give every pull request its own live preview.
Open a PR and get a working preview link on the pull request and in Slack, without touching your terminal.