Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Heroku pipeline - staging env variable carried into production

Tags:

I've recently deployed a React/Express app to Heroku, but I'm having an issue with environment variables that are part of the built app and the Heroku deployment pipeline - in a nutshell, the values of environment variables from the app's staging release are being carried over when promoting to production - the only way that I can get the environment variables to set correctly is to push the app directly to production, which really defeats the purpose of the deployment pipeline in the first place. Here's a summary of the scenario:

The environment variable in question is API_URL, which is referenced in webpack.config.js like so:

plugins: [
    new webpack.DefinePlugin({
        'API_URL': JSON.stringify(process.env.API_URL || 'http://localhost:4000/api/v1')
    })
]

The API is itself another Heroku app with staging and production releases, and so the values of the API_URL environment variable are set in my React app Heroku configuration as https://staging-api-12345.herokuapp.com/api/v1 and https://production-api-12345.herokuapp.com/api/v1, respectively.

When I push my React app up to staging, it works perfectly - however, when I promote the app to production and make the first call to the API, it is still pointing to https://staging-api-12345.herokuapp.com/api/v1. Ok well, I understand why that is the case - the app was built when being pushed to staging... so I tried rebuilding the app after promoting to production, but that did not work, it still used the staging environment variables.

When using the Heroku deployment pipeline, is there a way to force the app slug to rebuild so that it will catch the different environment variables?

like image 320
skwidbreth Avatar asked Jul 12 '17 18:07

skwidbreth


People also ask

What is staging in Heroku?

Deploy your app on Heroku You will most likely push a local branch on the heroku staging branch to test some features, potentially modify this local branch after testing it and then push the final version of this local branch to your heroku production branch.

How does heroku pipeline work?

A common pipeline workflow has the following steps: A developer creates a pull request to make a change to the codebase. Heroku automatically creates a review app for the pull request, allowing developers to test the change. When the change is ready, it's merged into the codebase's master branch.

How do I set up a heroku pipeline?

Create pipeline.Navigate to your Heroku Dashboard, https://dashboard.heroku.com/. Click the development DreamHouse app listed in the Heroku apps, that is, dhdev-UNIQUE_ID . Select the option More and Add to Pipeline. In the Add this app to a pipeline section, click Choose a pipeline and select Create new pipeline.

What is heroku CI?

Heroku CI is a low-setup, visual test runner that integrates with Heroku Pipelines to automatically run your tests on every push to GitHub, using disposable apps with strong dev-prod parity.


1 Answers

You cannot rebuild the slug, the main point of pipelines is to move the same slug between apps.

What you need to do is to fetch API_URL at runtime and not during build process. You can put all envs in one file, for example env.js

export const API_URL = process.env.API_URL;
export const OTHER_VAR = process.env.OTHER_VAR;

And then just import what you need in other files

import { API_URL, OTHER_VAR } from 'env.js';
like image 198
Michał Młoźniak Avatar answered Oct 12 '22 02:10

Michał Młoźniak