PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, August 21, 2022

[FIXED] What is the best way to load environment variables in your build

 August 21, 2022     bitbucket, bitbucket-pipelines, environment-variables, npm, webpack     No comments   

Issue

For a project I am working on I am setting up a BitBucket pipeline. Everything works great except for the environment vars. One of the usages of my env vars is the API_URL. I am loading the url by using process.env.API_URL. Locally this works great since it loads the .env file by using dotenv-webpack. However, in the pipeline I don't know what the best way is to load these vars. Should I add all the vars to Bitbucket and add export API_URL=$API_URL for every var in the pipeline file or are there better ways to load the vars?


Solution

Workspace, Repository, and Deployment variables is the way to go. Which one you need depends on scope you'd like these variables to cover (naming is pretty self-explanatory: some variables shouldn't change their values across your entire account; others only make sense to a particular repository; finally, some values are specific to a deployment environment).

Just define the variables in the Bitbucket UI or using API, and refer to their values from bitbucket-pipelines.yml or any script that you invoke from it. You don't need to add any export statements. Here's a very good doc explaining the details - https://support.atlassian.com/bitbucket-cloud/docs/variables-and-secrets/ - and User-defined variables section is particularly relevant to your question.

Alternatively, if you committed your .env file, you can simply export its contents all at once as per this StackOverflow answer.

Obviously, don't commit .env if it contains any sensitive values.



Answered By - esimonov
Answer Checked By - Mildred Charles (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing