Deploy Previews for GitLab
Overview
Deploy Previews for GitLab are made possible through the Heroku CLI and GitLab’s Environments feature.
Whenever you open a merge request on your GitLab repository, Heroku will automatically spin up a disposable app, reflecting the changes proposed in your merge request.
GitLab Environments
- GitLab’s environments allow you to track deployments dynamically.
- Each merge request can have its own environment.
- You need to define environments in your
.gitlab-ci.ymlfile. - Ephemeral environments (like review apps) can be spun up and later torn down with a stop job (
action: stop).
Heroku CLI
- The Heroku CLI allows you to dynamically generate deployments.
- Each Deploy Preview that is generated with the Heroku CLI will be tracked within GitLab Environments.
- Please install the Heroku CLI before continuing with the setup process.
Setup and Configuration
Step 1: Generate a long-lived auth token with the Heroku CLI & store it in GitLab variables
After installing and logging in with the Heroku CLI you’ll need to generate a long-lived auth token.
- Run
heroku authorizations:createto generate a token that expires in 1 year. - Copy the token and also create a reminder to generate a new token before the current one expires.
- Back in GitLab — Navigate to Settings » CI/CD and expand the “Variables” section.
- Click “Add variable”, set visibility to “Masked and hidden”, set key to “HEROKU_API_KEY” (no quotes), with the value you copied from the CLI command earlier.
- Click “Add variable” to save it.
Step 2: If you don’t already have the variable HEROKU_GIT_URL for your production app configured, set that up now
Now that we’re going to be both publishing to your production application and dynamically generating Deploy Previews we want to make sure we have the HEROKU_GIT_URL variable configured correctly.
- Validate if the variable exists.
- If not, create the variable using the same settings as in the last step above. For the key you’ll use “HEROKU_GIT_URL” (no quotes) with the value being the Git URL of your production app.
- Your production application should reside within your new Heroku Team. To locate your Git URL you’ll need to navigate to your Heroku Dashboard and then click into your production app. From there you’ll click the “Settings” tab and your Heroku Git URL will be located there.
- After setting the
HEROKU_GIT_URLvariable we can move onto configuring the.gitlab-ci.ymlfile.
Step 3: New .gitlab-ci.yml configuration
With our variables configured correctly let’s setup the .gitlab-ci.yml to match the below configuration. Be sure to replace your-team-here references (theres only 1) with your actual team name provided earlier on during this process.
Step 4: Save the changes, push, and test!
Let’s get these changes merged in so we can test!
- After these changes are merged — let’s create a new branch with a minor change.
- Create an MR and verify the environment is created. Environments can be viewed via Operate » Environments.
- When the build completes you should be able to view your first Deploy Preview! 🚀
- Merge the MR and a couple things should happen:
- The Deploy Preview should destroy the review app within Heroku and clean up any resources used.
- The normal deploy to production should activate and move forward as expected. 🎉🎉🎉
- All done!