Environment variables are not found in Jenkins - linux

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.

Related

how to prepare python code to run from command line on different environment?

how to run python(behave)code with environment parameters? e.g.
environment=X behave --tags #regression
what I have till now is
#given(u'user is on the firts page')
def step_impl(context):
context.first_page = FirstPage(context)
context.first_page.goto(url_config.URL["X env"])
and as dict URL
URL = {
"X env": "https://...",
"Y env": "https://..."
}
You should use environment variables for this. The pipeline script should include the following command to define which environment you want to run against:
export ENV=X_env
In your test scrip, get the environment variable and use that to get the appropriate url:
import os
#given(u'user is on the firts page')
def step_impl(context):
context.first_page = FirstPage(context)
execute_in_environment = os.environ.get("ENV")
context.first_page.goto(url_config.URL[execute_in_environment])
Note that reading the environment variable - so this line: execute_in_environment = os.environ.get("ENV") is typically done at a higher level in the test framework, somewhere along with other config stuff. But going strictly by what is shared in the question I have added it to the step implementation, which isn't best practice.
If you want to try it out on your Windows station first, then set the environment variable in the CMD prompt using:
set ENV=X_env
So to run your tests against a specific environment you would run these commands (this is a Linux example):
export ENV=X_env
behave --tags #regression

How to use gitlab CI environment variables in fastlane fastfile?

I am currently using a .env file to get environment variables in FASTFILE, but now I am trying to automate the fastlane using GitLab CI/CD.
Since the .env file which has all the keys can not be pushed to the branch I have to declare all the .env or the environment variables in the GitLab runner's environment variable.
I want to know how can I use the GitLab runners's environment variable in my fastfile.
lane :build_staging do |options|
environment_variable(set: { 'ENVFILE' => '.env.staging' }) // I want to use the GitLab environment variable
clean
gradle(task: options[:task], build_type: 'Staging', project_dir: 'android/')
end
In Settings > Variables, you can define the whole file as a variable with a specified scope :
In your gitlab-ci, you would use it by specifying the variable name (in my example $ENV_FILE) and the scope using stage keyword in your job :
build:
stage: staging
script:
# do your work here
You can find more info in the documentation for variable file type and scope.

How to interpolate environment variables within netlify.toml config

I want to proxy to a different api depending on the environment - I've tried a few variations on the following theme without any luck. What's the correct way of doing this, if its even possible?
[build.environment]
API_URI="https://dev-api.foo.com/:splat"
[context.production.environment]
API_URI="https://prod-api.foo.com/:splat"
[[redirects]]
from = "/api/*"
to = "$API_URI"
status = 200
force = true
This does not work.
Although the above config works when I hardcode a URI into the to field, it just fails when I try to interpolate an env var.
It's not supported, but Netlify suggest a work-around in their documentation (https://www.netlify.com/docs/netlify-toml-reference):
Using Environment Variables directly as values ($VARIABLENAME) in your
netlify.toml file is not supported. However, the following workflow
can be used to substitute values based on environment variables in the
file, assuming you are only trying to change headers or redirects. The
rest of the file is read BEFORE your build - but those sections are
read AFTER the build process.
Add a placeholder like
API_KEY_PLACEHOLDER somewhere in the netlify.toml redirects or headers
sections.
Create an Build Environment Variable, for example API_KEY,
with the desired value. You can do this in the toml file or in our UI
in the Build and Deploy Settings section of your configuration. You
might use the latter to keep sensitive values out of your repository.
Add a command like this one to your build command: sed -i
s/API_KEY_PLACEHOLDER/$API_KEY/g netlify.toml && normal build command.
Answering my own question - it's not supported, you have to manually interpolate env vars yourself as part of the build on Netlify.
Yes. It's possible. Here is the detailed docs: https://www.netlify.com/docs/continuous-deployment/#deploy-contexts
In my case, I need to set a REACT_APP_API_URL separate for production and all other branches. Here is what I use:
[context.production.environment]
REACT_APP_API_URL = "https://api.test.im"
[context.deploy-preview.environment]
REACT_APP_API_URL = "https://api-staging.test.im"
[context.branch-deploy.environment]
REACT_APP_API_URL = "https://api-staging.test.im"

How can I run script after deploy to heroku

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)

Accessing Variables Specified for Jenkins Groovy Plugin Script

When writing a script that is run by Jenkins Groovy Plugin as a build step (Execute System Groovy Script) one can specify 'variable bindings'. The helpline says: Define varibale bingings (in the properties file format). Spefified variables can be addressed from the script. [sic] How do I access those variables from the script? They are not set as environment variables for the build, neither are they present among System properties.
this.getBinding().getVariables()
or simply binding.variables
I wasn't able to use binding.variables directly, I only got listener, build, launcher and out from binding.variables.
Instead I was able to use build.environment(listener) to retrieve the environment variables as suggested in the responses to this question:
Access to build environment variables from a groovy script in a Jenkins build step ( Windows)
def config = new HashMap()
config.putAll(binding.variables)
def logger = config['out']
def envvars = new HashMap()
envvars.putAll(build.getEnvironment(listener))
def myvar= envvars['myvar']
This might have been different for me since I am only looking for system-wide environmental variables:
(checked) Prepare an environment for the run \
Keep Jenkins Environment Variables \ Keep Jenkins Build Variables

Resources