How can I run script after deploy to heroku - node.js

I have an application in node.js which depends on env variables. I made some changes in code and now one of this vars should be changed after deploy. I don't want to do this manually. What is the best practise to do this automatically.
I guess that running some script after deploy could be solution, but I want to run this script only once (with this one particular change).
My only idea is that I should have script that will be checking (after each deploy) some directory if there is another script to run and then run it and remove it. But how can I achieve that?

The best way to approach this is to use the Heroku Toolbelt to set your environment variables as described here:
heroku config:set GITHUB_USERNAME=joesmith
You can then refer to these variables in your Node.js application by using the following syntax:
var dbUsername = process.env.DB_USERNAME;
Assuming you set a DB_USERNAME variable like this:
heroku config:set DB_USERNAME=myAppUserName
I like to ensure there's a fallback if the environment variable is not set, you can achieve that like this:
var dbUsername = process.env.DB_USERNAME || 'fallbackUsername';
// The string after || will be used if the process.env.DB_USERNAME variable is undefined (not set)

Related

Build variables not accessible in my react native app

I need to inject env variables into my code.
I'm using azure pipelines to build my android app in react native.
I have set env variables in the build configuration and I have created a file called appcenter-post-clone.sh. The contents of this file are as follows:
ENV ADMIN_HOST= $ADMIN_HOST
And in my build configuration I have defined
ADMIN_HOST = https://example.com.
But I'm getting this error, [command]/bin/bash /Users/runner/runners/2.160.1/work/1/s/appcenter-post-clone.sh
ENV: https://example.com: No such file or directory. What I fail to understand here is, why is azure treating the value of my env variables as a file? How do I make this work?
The blunder I made here is, I should have used
ENV ADMIN_HOST=$ADMIN_HOST
Without the space. That solved it for me.

Setting environmental variables for node from host machine for build server

I'm using bitbucket pipelines as a build server.
I need to pass environmental variables from a host machine into a .env file which will then set the var values to be used in the build.
For example, lets say an environmental variable in a docker container running the build is AWS_ACCESS_KEY_ID.
In my .env file I'd like something like the following:
ACCESS_KEY=${AWS_ACCESS_KEY_ID}
I would then run the build and the ACCESS_KEY var would have a value equal to the env var in the docker container.
My current idea for a solution right now involves replacing values with sed, but that feels pretty hacky. Example:
.env file contains the following line:
ACCESS_KEY=<_access_key_replace_me_>
sed "s/<_access_key_replace_me_>/${AWS_ACCESS_KEY_ID}/g" .env
Any better solution than this?

Not able to access Gitlab env variables either in node.js or react

I have two different projects checked in Gitlab, frontend and backend.
For both the projects i have a Dockerfile each.
I have set the env variables in gitlab ci/cd .
I am running docker container in kubernetes,
but i am not able to access the gitlab env variables either in my react or node.js (express) application.
I was thinking that those env variable would be available to me when i do process.env.variable_name, but i am not able to access them.
What’s the best way to access Gitlab env variables in kubernetes (deployment.yaml) env variables ?
UPDATE
I have found that we can specify env variables in kubernetes, deployment.yaml file (under env section). How can i pass gitlab env variables to deployment.yaml?
Docker containers require that you set environment variables when you run them:
https://docs.docker.com/engine/reference/commandline/run/#set-environment-variables--e---env---env-file
--env , -e Set environment variables
I was able to access environment variable in Node js application using Gitlab K8S_SECRET_ variable naming convention.
For example define a variable like K8S_SECRET_MY_TEST_API in gitlab.
In Node js, you can access this variable using process.env.MY_TEST_API.
Only issue I am facing is that those variables are not available in React app. Still trying to figure that out. I will update here once I resolve that issue.
To inject environment variables to a react app created with create-react-app you should add the prefix REACT_APP_ to every env var.
During the build, webpack will pick all the environment variables with that prefix and will add them to environment.

How can I make node application see system variables on Google Cloud?

I have variable set in my .bash_rc file:
whoami#cloudshell:~/source/NodePrototype (x-alcove-9999999)$ echo $APP_ENVIRONMENT
LIVE
Yet node.js application out of:
const app_environment_config=require('./APP_ENVIRONMENT/' + process.env.APP_ENVIRONMENT)
produce
2019-02-21 14:18:16 default[20190221t141628] Error: Cannot find module './APP_ENVIRONMENT/undefined'
Eventhough when I enter node shell:
whoami#cloudshell:~/source/NodePrototype (x-alcove-9999999)$ node
> process.env.APP_ENVIRONMENT
'LIVE'
The same part works locally.
It depends on how your Node app is being launched, because looks like is not running in an environment where that variable exists, to make sure print all your current env vars to make this sure: console.log(process.env).
Also, a good practice, when you need something like that, is to use .env files with this module: https://www.npmjs.com/package/dotenv is a good practice to pass configuration to your Node apps.

Environment variables are not found in Jenkins

I want to set quite a few variables in Jenkins. I have tried putting them in .bashrc, .bash_profile and .profile of the jenkins user but Jenkins cannot find them when a build is happening.
The only way that is working is to put all the env variables inside the Jenkinsfile like this:
env.INTERCOM_APP_ID = '12312'
env.INTERCOM_PERSONAL_ACCESS_TOKEN = '1231'
env.INTERCOM_IDENTITY_VERIFICATION_KEY='asadfas'
But I don't think this is a good way of doing it.
What is the correct way of setting env variables in Jenkins?
To me, it seems very normal. INTERCOM_PERSONAL_ACCESS_TOKEN and INTERCOM_IDENTITY_VERIFICATION_KEY should be considered as text credentials and you can use the environment directive to add environment variables.
stages {
stage('Example') {
environment {
INTERCOM_APP_ID = '12312'
INTERCOM_PERSONAL_ACCESS_TOKEN = credentials('TokenCrednetialsID')
INTERCOM_IDENTITY_VERIFICATION_KEY = credentials('VerificationCrednetialsID')
}
steps {
echo "Hello ${env.INTERCOM_APP_ID}"
}
}
}
If you need to keep environment variables separate from JenkinsFile you can create a groovy file which contains all of those and then load that file into Jenkinsfile using
load "$JENKINS_HOME/.envvars/stacktest-staging.groovy"
For more information take a look at following links
https://jenkins.io/doc/pipeline/steps/workflow-cps/
SO: Load file with environment variables ...
Jenkins resets environment variables to some defaults for their jobs. Best way to set them is in jenkins configuration. You can set global vars, local for project or local for node.
Now i do not remember if this feature is build in or provided by some plugin.

Resources