How to use deploy specific .env in Vuejs - vue.js

I have several environments that I build my Vuejs application to. Each of those environments has a different API that I'd like to call.
My ideal scenario is that I have an .env file in the root of each of the servers I deploy to, so that when I deploy my build, the Vue app looks at the .env that is in it's environment specific location and pulls the relevant API path variables from inside of it.
This way, I can build the app once, and have it deployed on multiple servers and I don't need to make a specific build for each server.
I've tried using dotenv-webpack but it seems that the .env that's included in the build is just from whatever the value is when I create the build, and not fetched from any .env on the individual servers.
I've found that I can use axios to GET a js file from /static but this doesn't really seem like the cleanest approach.

Related

Application environment variables undefined when application is served by Amplify

I have a Vue.js app hosted by AWS Amplify.
In Vue, env vars can be set application-wide by using .env. files.
I currently use such files for development and for production modes, containing different values.
When locally building and serving my application the above works as expected. However, once Amplify deploys my app (in my case I use Amplify's CD feature), these variables are not defined.
I know I can define the same env vars in Amplify, but that would mean I need to manage these values in two places since won't be redeploying while developing. so this seems to be prone to errors (I will need to remember to update the vars on both the application end and amplify console whenever I need to make a change for example).
I wonder if this behavior is expected or is there something I am missing in my setup.
Thanks!
I was also facing the same issue in my React app.
The thing is, you need to have a .env file in your app with all the environment variables.
Why? — The reason behind that is, it generates static HTML, CSS and JS files. Those files can't access process during the runtime.
After adding all the environment variables in Amplify, you have to add one more command in your build stage in App build specification.
You can refer to this official documentation on how to implement this.
If you don't care much about your environment variables, you can use this hack: printenv. This will store all the environment variables of your OS and your application in the .env file.
My config looks something like this:
build:
commands:
- 'printenv >> .env'
- 'npm run build'

Getting environment variables at runtime in a compiled VueJS app

I'm working on a VueJS app that uses env vars in multiple places. It runs on GCloud with nginx serving the files compiled with vue-cli-service build.
When developing everything works well with the env vars set in .env.development and .env.development.local files and used in JS with process.env.VUE_APP_FOO. I'm not using .env.production as some of these env vars shouldn't be committed to our repository.
For the staging and prod environment of all of our projects, we use GCloud's config maps which let us provide env vars to the pods. The issue in this project is that vue-cli-service build requires the env vars to be available at build time, which is not the case in our setup. Config maps are only available in the pods that run the images.
Out of curiosity, I checked the compiled code and all uses of process.env are quite simply replaced by an object with all vue env vars (basic ones + VUE_APP_* ones). So for example,
console.log(process.env.VUE_APP_FOO);
is compiled to
console.log(Object({NODE_ENV: "production", BASE_URL: "/", VUE_APP_FOO: "bar"}).VUE_APP_FOO);
Except that in our case, VUE_APP_FOO is missing from the object as it's not available in the environment when building the app.
So as is, it doesn't seem possible to provide env vars when the server is started or the JS file is served. Is there a way to tell vue-cli-service to not compile the env that way? Or any other alternative?
The only one I found so far is to replace the uses of env vars with their actual value directly in the compiled JS file when the pod starts using sed, but that's pretty ugly and could break easily.
One approach you can follow is to provide the production values when building locally. Another approach is to setup a continuous integration workflow that fetches your environment variables from wherever it is stored, builds the apps and pushes to production servers. I personally work with approach 2.
It is relatively easy to setup a github workflow that runs whenever your code is pushed to a particlar branch

Is there is any way to get the appcenter config from env file in React Native

One of my android application is developed using React-Native and I have plans to distribute this using app-center as this app is for Internal users. In App-Center I have different projects (created new app) for the different environments - one for dev, one for uat and one for production.
My issue here is whenever I create build package (apk file) to deploy to App Center project, I need to manually change the app-center config. I have used .env config file to get config for different environment. But I can't use this to set app-center config. Can anyone guide me how I can automate this process by either configuring the app-center in .env file or somewhere in code?
If you want to use a specific configuration for each build with the same code, you should use the appcenter's environment variable.
Just add an "app_secret" environment variable, and use a pre build script to create a appcenter-config.json file containing the app_secret

dynamically select appconfig in vue

I know that you can create .env.xxxx files in vue and then run a dev, test and production builds to have environment specific environmental variables.
But what if I want to do one build and then when the app is running it could dynamically select the app config file based on the environment the app is hosted in. It's given that the host environment could have VUE_APP_ENVIRONMENT environmental variable and could use this to decide what settings to use.
What's a good way to do this?

export and maintain vue application

I have developed a vue application and did run npm run build
After that I uploaded the content in the dist file to my webpage but it returned a blank page.
Since I did this for testing I uploaded it to a folder in my public_html/mypage.com/vueapplication To get all the paths right I added a vue.config.js with this content:
// vue.config.js
module.exports = {
publicPath: '/vueapplication/'
}
The application now works but I wounder however:
how do I best publish/upload the application to my site? Just by simply dragging the content inte the right folder?
how can I best maintain my site? Do I need to build again and upload, overwriting my files when everytime I make an update on my site?
And what is the difference between build and deploy your application?
Drag and dropping your code should work. But as your app grows you may want to look into automating this. For instance if you use an S3 bucket you can use the aws cli to automate the upload.
Yes, you should overwrite your deploy folder(s). You need to also take care of deploying different binary files, that have the same name. An example is if you have a global css file (main.css for instance). The file will probably change content between deployments, but keep the same name. Browsers may cache the file so users that downloaded older versions of the file will not use the new one. There are different techniques to handle this, but if you use webpack, it uses cache busting techniques and you should be fine.
Build is the process of transforming source code into an artifact(s). Exactly what this means differs from language to language, platform to platform. In the vuejs world this usually means a couple of js files, a couple of css files and some assets.
Deploying means taking the output of a build and making it available to your users. Again this differs from project to project. In the vuejs world this usually means taking the artifacts from the build and uploading them to an http enabled web server.