Accessing Variables Specified for Jenkins Groovy Plugin Script - groovy

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

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 setup multiple paths in user environmental variables in windows 10

I want to set up multiple paths in the user environmental variables in windows 10. but can only set a single path, how to overcome this issue?
If you want to add user defined environment variable then use react-native-config library. So in short if there are variables defined in .env file then you can use it anywhere in the react-native app like below:-
.env file
API_URL = "https://baseurl/endpoint"
//To use the above environment variable
import config react-native-config
function apiCall() { axios(config) }
With the help of react-native-config you can use environment variable in native code also and vise-versa.
For better use define .env.staging file. By default config lib finds .env file. To run the application with .env.staging file see below command.
$ ENVFILE=.env.staging react-native run-ios # bash
$ SET ENVFILE=.env.staging && react-native run-ios # windows
$ env:ENVFILE=".env.staging"; react-native run-ios # powershell
For more information checkout :- https://github.com/luggit/react-native-config
Happy coding mate :)
to add multiple paths for a single environment variable, list them all with a semicolon(";") in between like that:
C:\Cpp_Headers\pybind11;C:\Cpp_Headers\cpython\Include;C:\Users\admin\Cpp_Headers\cpython\PC
(and so forth). From this input Windows will automatically recognize multiple definitions.

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.

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 to add environment variables to openshift upon git push

I am new to openshift and I've tried hard to modify my env upon git push so that I don't need to rhc env set ENV_VAR=value -a appname everytime I push. According to the documentation, I can do export in one of the action hooks, but whenever I did so, the environment variable will not register..
What is the best way to register those variables automatically, rather than needing to execute rhc command or ssh into the machine and export?
The documentation seems to be outdated as the method of exporting in action_hooks doesn't work anymore
https://developers.openshift.com/en/managing-environment-variables.html
I see that you have your answer already, but in case others come here for the same question, I'd like to mention that the rhc env set command actually sets a variable persistently, so it "survives" the code push, build and gear restart.
The documentation linked in the question says that the export can be used to view environment variables during build; it does not recommend setting environment variables using hooks.
The variables' listing itself, using the build hook, should work just fine. (worked for me at the time of writing this)
In case the export in the build action hook seems not to work (does not list the variables), it is typically caused by the hook file not being set executable (or by a syntax error within the file).
Yes, the action hook way is already broken, even though you export through the hook, you can see that there is no declare -x statements thrown out like stated in the documentation anymore.
One other method you can do is to use the action hook to write to files in this directory:
$HOME/.env/user_vars
for example, if you want to set RAILS_ENV=development, write a script that churns out this file:
$HOME/.env/user_vars/RAILS_ENV
with this content:
development
Spent an awful lots of time to find alternative ways too, but this guy nailed it out, copied it in case the link becomes broken in the future:
If you need to set some environment variables in your GEAR you can use an action hook.
The pre-start action hook will serve you well but if you need to restore those variables after a gear restart, pre-start action hook won’t work.
Post-restart action hook, on the other hand, will execute its actions but I haven’t managed to get the environment variables working. After its execution all environment variables that should have a value were empty.
What I did was to modify pre-start action hook to create environment variables as files under $HOME/.env/user_vars
# Actual script
export OPENSHIFT_POSTGRESQL_DB_HOST="xxx.xxx.xxx.xxx"
export OPENSHIFT_POSTGRESQL_DB_PORT="***"
export OPENSHIFT_POSTGRESQL_DB_NAME="***"
export OPENSHIFT_POSTGRESQL_DB_USERNAME="***""
# Added script for post restart variables
echo "xxx.xxx.xxx.xxx" > OPENSHIFT_POSTGRESQL_DB_HOST
echo "***" > OPENSHIFT_POSTGRESQL_DB_PORT
echo "***" > OPENSHIFT_POSTGRESQL_DB_USERNAME
echo "***" > OPENSHIFT_POSTGRESQL_DB_PASSWORD
After this, if you execute gear restart, the environment variables will exist and will be accesible from your application.
Reference:
https://guilleml.wordpress.com/2015/02/17/setting-environment-variables-in-openshift/

Resources