Skip to content

fix: add deploy-related environment variables to local builds #4209

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 2, 2022

Conversation

ehmicky
Copy link
Contributor

@ehmicky ehmicky commented Mar 2, 2022

Fixes #3904

This PR builds on top of #3931 which was created by @lukasholzer a few months ago to solve this issue.

This adds dummy values for 4 environment variables in local builds (not production builds): DEPLOY_ID, BUILD_ID, DEPLOY_URL and DEPLOY_PRIME_URL.

This is a rather small change from a production code standpoint. However, it updates a high amount of test snapshots, but those changes are all identical, essentially just adding those new default values to the test snapshots.


For us to review and ship your PR efficiently, please perform the following steps:

  • Open a bug/issue before writing your code 🧑‍💻. This ensures we can discuss the changes and get feedback from everyone that should be involved. If you`re fixing a typo or something that`s on fire 🔥 (e.g. incident related), you can skip this step.
  • Read the contribution guidelines 📖. This ensures your code follows our style guide and
    passes our tests.
  • Update or add tests (if any source code was changed or added) 🧪
  • Update or add documentation (if features were changed or added) 📝
  • Make sure the status checks below are successful ✅

Sorry, something went wrong.


const NETLIFY_DEFAULT_DOMAIN = '.netlify.app'
// `site.name` is `undefined` when there is no token or siteId
const DEFAULT_SITE_NAME = 'site-name'
Copy link
Contributor Author

@ehmicky ehmicky Mar 2, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In local builds, this default value for the site name is used by the DEPLOY_URL and DEPLOY_PRIME_URL environment variables, but intentionally not by the SITE_NAME environment variable. This is to be consistent with SITE_ID which is also unavailable when the authentication token or siteId has not been provided.

Please note this situation is an edge case, since it cannot happen in neither production nor CLI builds, only when @netlify/build is called programmatically (which at the moment is mostly only used in @netlify/build automated tests).

@ehmicky ehmicky force-pushed the fix/local-deploy-urls branch from 1918a60 to 6ea1b65 Compare March 2, 2022 17:18

Verified

This commit was signed with the committer’s verified signature.
devnexen David CARLIER
@ehmicky ehmicky force-pushed the fix/local-deploy-urls branch from 6ea1b65 to 2f57561 Compare March 2, 2022 17:19
Copy link
Contributor

@erezrokah erezrokah left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @ehmicky, the code looks great.

I added 2 comments on possible edge cases to handle.

}
}

const NETLIFY_DEFAULT_DOMAIN = '.netlify.app'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if NETLIFY_DEFAULT_DOMAIN is still used when a user has a custom domain configured.
What will be the host for DEPLOY_PRIME_URL and DEPLOY_URL in that case?
If not, we might be able to take them from the siteInfo (if it is available)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question. Yes, .netlify.app is still used even with a custom domain, but only for DEPLOY_URL and DEPLOY_PRIME_URL, not for URL. This is documented as such, but I also confirmed this with a manual test on my test site which uses a custom domain (see build logs).

return {
URL: sslUrl,
REPOSITORY_URL,
DEPLOY_PRIME_URL: `https://${branch}--${name}${NETLIFY_DEFAULT_DOMAIN}`,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where do we get branch and deployId from?

branch might not be available if running from a non git folder

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

deployId is taken from:

deployId: combinedEnv.DEPLOY_ID,

Before this PR, it was set either the @netlify/config flag deployId (which we use in production) or the DEPLOY_ID environment variable. With this PR, it also defaults to 0 for CLI and programmatic builds.


branch is taken from:

https://github.com/netlify/build/blob/main/packages/config/src/options/branch.js#L3-L9

It has a default value when used in a non-git environment.

@ehmicky ehmicky requested a review from erezrokah March 2, 2022 17:46
@kodiakhq kodiakhq bot merged commit a1b2133 into main Mar 2, 2022
@kodiakhq kodiakhq bot deleted the fix/local-deploy-urls branch March 2, 2022 18:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
automerge type: bug code to address defects in shipped code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

DEPLOY_PRIME_URL, DEPLOY_URL, DEPLOY_ID and BUILD_ID are undefined in local builds
2 participants