IS_PULL_REQUEST is true on the main branch?

I’m deploying the boilerplate gatsby static site.
During build time, I’m taking the IS_PULL_REQUEST env variable and storing it in a .env file as GATSBY_ IS_PULL_REQUEST so that the browser can have access to it. However, on the main branch this variable is always “true”.

Here is my bash script that is running during build time echo "GATSBY_IS_PULL_REQUEST=$IS_PULL_REQUEST" > .env.render

Here is my code in production reading the env var: const isPR = process.env.GATSBY_IS_PULL_REQUEST === "true";

How is this possible?

Hi Jake,

Thanks for reaching out.

I’m not currently seeing any issues with the IS_PULL_REQUEST environment variable being set incorrectly in my testing.

Please could you share a little more information on your setup here? For example, details like:

  • Are you using a Static Site and trying to use the GATSBY_IS_PULL_REQUEST in your build process? Or, are you using a Web Service and using Gatsby dynamically?
  • How is the .env.render being loaded? Is it defined in the service dashboard “Environment” tab or pulled in at runtime with something like dotenv?

Thanks

Alan

Thanks very much for your reply. Please don’t take the short answers as attitude, I’m very grateful for your help :smiley:

I’m not currently seeing any issues with the IS_PULL_REQUEST

Very interested to see how this is happening. These are the problems that get me up in the morning :smiley:

Are you using a Static Site

Yes, the environment variables from the build process are hardcoded in a static site.

use the GATSBY_IS_PULL_REQUEST in your build process?

I’m setting GATSBY_IS_PULL_REQUEST using IS_PULL_REQUEST in the bash script during the build process.

How is the .env.render being loaded

During build time, it is being loaded with dotenv.

Hi Jake,

I put a couple checks in one of my Gatsby apps and worked through a similar scenario to what you shared, outputting at each stage.

I tested with a build.sh set at the build command, it contained:

#!/usr/bin/env bash
# exit on error
set -o errexit

# testing $IS_PULL_REQUEST
echo "1. The value of IS_PULL_REQUEST is ${IS_PULL_REQUEST}"
echo "2. echoing IS_PULL_REQUEST into .env.render"
echo "GATSBY_IS_PULL_REQUEST=$IS_PULL_REQUEST" > .env.render
echo "3. The contents of .env.render are (cat .env.render): $(cat .env.render)"
echo "4. Calling loading & calling .env.render through Node & dotenv: node test_env.js"
echo "$(node test_env.js)"

gatsby build

This followed the same echo into .env.render, then called it back with a basic Node script in test_env.js containing. (The repo does not contain a .env.render file it created in the build script):

const dotenv = require('dotenv').config({path: './.env.render'})
console.log(process.env.GATSBY_IS_PULL_REQUEST)
console.log(`GATSBY_IS_PULL_REQUEST === 'true'? - ${process.env.GATSBY_IS_PULL_REQUEST === "true"}`)

I pushed these scripts to my main branch and the deploy logs output:

Feb 22 04:00:19 PM ==> Running build command './build.sh'...
Feb 22 04:00:19 PM 1. The value of IS_PULL_REQUEST is false
Feb 22 04:00:19 PM 2. echoing IS_PULL_REQUEST into .env.render
Feb 22 04:00:19 PM 3. The contents of .env.render are (cat .env.render): GATSBY_IS_PULL_REQUEST=false
Feb 22 04:00:19 PM 4. Calling loading & calling .env.render through Node & dotenv: node test_env.js
Feb 22 04:00:19 PM false
Feb 22 04:00:19 PM GATSBY_IS_PULL_REQUEST === 'true'? - false

I then raised a PR with PR previews enabled and the deploy logs output:

Feb 22 04:08:04 PM ==> Running build command './build.sh'...
Feb 22 04:08:04 PM 1. The value of IS_PULL_REQUEST is true
Feb 22 04:08:04 PM 2. echoing IS_PULL_REQUEST into .env.render
Feb 22 04:08:04 PM 3. The contents of .env.render are (cat .env.render): GATSBY_IS_PULL_REQUEST=true
Feb 22 04:08:04 PM 4. Calling loading & calling .env.render through Node & dotenv: node test_env.js
Feb 22 04:08:04 PM true
Feb 22 04:08:04 PM GATSBY_IS_PULL_REQUEST === 'true'? - true

From those results it appears to work as I would expect and the issue may be in your code somewhere, are you loading the .env.render explicitly?

I hope the above helps in your debugging, please let us know if we can assist any further.

Kind regards

Alan