Java not finding environment variables defined in Jenkins - java

I have a snippet of code that is obtaining an environment variable as follows: System.getenv("MY_VAL")
locally on my windows machine this works fine.
However, on my Jenkins CI Server which is running CentOS I am encountering some issues
I have tried setting the value of MY_VAL through both the envinject plugin as well as the global jenkins settings
If i do a pre-build step to echo the value out, it works fine, however inside my java code this is not being resolved.
How do I get this to be resolved?

You can achieve that by installing EnvInject plugin.
1) After installing check the Prepare an environment for the job option in the job configuration screen. This option will display several field for you to fill.
UPDATE
2) Fill the Script Content area with a command touch env.properties to create the file.
3) Fill the Properties Contentt field with the variables you want to inject inside your recently created env.properties file by doing so. Place one variable per line ex:
VARIABLE1=value
VARIABLE2=value
4) Reference env.properties file you've just created in the file path area.
5) At runtime Jenkins will inject those variables and they will be available to your program.

You need to inject this variable into property.file and then access this from property file. e.g. in execute shell you can define "echo MY_VAL=default > property.file" .. Later in subsequent jobs you can pass through using "Jenkins Parameterized Trigger plugin" where you have option to access parameters from property file.

Related

Sharing environment variables in IntelliJ JUnit run configurations

I'm using IntelliJ IDEA to write and run e2e tests with JUnit. My tests require some environment variables to be set, so I set these in the Run Configurations dialog (through Run-->Edit Configurations, Environment Variables field).
This works fine, but the problem is whenever I run a single test (e.g. by clicking the green "play" icon to the left of the code editor), it creates a new run configuration for me to run this single test without the environment variables I defined, which obviously causes it to fail. I need to open the Run Configurations dialog again and copy the environment variables from another configuration to the new one. Note that I need to do it for every single test that I run, which is very annoying.
Is there a way to set the environment variables only once and use them for every test that I run?
You can expad the "Defaults" in the list to the left, pick JUnit and then set the env variables there. This config will then get copied to whatever new configurations are created afterwards:

cassandra 3.5 fails to load trigger class

I am trying to get started with Cassandra triggers, but I cannot get Cassandra to load them. I have built jar files from here and here, and put them under C:\Program Files\DataStax-DDC\apache-cassandra\conf\triggers. I have restarted the DataStax_DDC_Server service (on Windows) and reopened the CQLSH command line, but trying to use the trigger class in a create trigger command gives me only:
ConfigurationException: <ErrorMessage code=2300 [Query invalid because of configuration issue] message="Trigger class 'org.apache.cassandra.triggers.InvertedIndex' doesn't exist">
I checked the jar files, and they include the class files.
The only thing I could find in the log files of cassandra is Trigger directory doesn't exist, please create it and try again. But I don't know if that is relevant.
EDIT: Following the last line shown here, I edited the cassandra.bat file. Now if I stop the DataStax_DDC_Server service and run the bat file directly, the create trigger command succeeds. Nevertheless, the service seems to be independent of this bat file. The question now is how to apply the same config to the service?
After googling creatively, I found a solution. As mentioned here you need to explicitly set the cassandra.triggers_dir variable, but for the service to pick it up, as explained here, you must configure it in the registry. So the answer is to update the registry key
HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Apache Software Foundation\Procrun 2.0\DataStax_CDC_Server\Parameters\Java\Options
and add the line
-Dcassandra.triggers_dir=C:\Program Files\DataStax-DDC\apache-cassandra\conf\triggers
Note that the path should not be enclosed in quotations, or it won't work.
Don't forget to restart the service.
Above solution is working for window. it's difficult in window to find registry option. so to find registry option go to start menu and type "regedit" it will open registry window then you can do above settings.

How to make .properties read parameters from Jenkins

I use Jenkins to run regression periodically
I have java-maven project with 'ATC.properties' where I can choose browser, environment etc. by uncommenting appropriate one
#### browser ######
browser.name=firefox
#browser.name=chrome
#browser.name=ie
So I have to commit it, push and only after that job on Jenkins will run build with chosen parameters in 'ATC.properties' as well
How can I make my maven project read parameters from parametrised Jenkins build.
Can any one give me some example with browser ?
Do I have to use another one '.properties' file with described variables
like
browser.name=${browser.name} ...
in my project ?
Parameters defined in Jenkins will be expanded at run in Maven Build , Below process does not require additional property file :
Define jenkins choice parameter :
browser_name
Provide all your Browsers options as choices and select the required option at run time.
Now replace your pom.xml with ${browser_name} where you required the option of reading browser value instead of reading value from property file.
pass parameter at run time as below
mvn clean install -Dbrowser_name=%browser_name% [incase of windows]
mvn clean install -Dbrowser_name=$browser_name [incase of linux]

How to buld a Maven project with a specified java home property in eclipse?

I want to build a maven integrated project with a given java home property. At exactly i want to write a code in java that sets the java home property for an InvocationRequest object. The main goal is to build a project with the runtime given(by an algorithm) java home. So i would call getInvoker().execute(request); to execute maven goals where request is an InvocationRequest object.
I tried to set the request java.home property with properties.setProperty(Goals.JAVA_HOME, javaHomePath); and call the method executeGoals(pom, new String[] { Goals.INSTALL, Goals.CLEAN }, properties); . This executeGoals(...) method contains getInvoker().execute(request) call and the request object definition too.
Output is: Missing: 1) com.sun:tools:jar:1.5.0 #Solved
EDIT: solved the output problem, but a new one appeared:
class file has wrong version 50.0, should be 49.0. Maybe i changed the jre home, so i think i'm compiling with a newer version of java than i'm running with.
Reminder: i want to build with a specified java home property = i want to change the compiler java home(or version) to the specified one. (In eclipse)
I would appreciate any help.
I dont think it is configurable. It is part of the Maven Core to use the JAVA_HOME environment variable. Please see the accepted answer of:
How to set specific java version to Maven
Also it is not possible to set or change environment variables (not system properties) within a Java process (for the current process). If you create another process from within your Java process, there will be methods to specify environment variables for this sub process.
Maybe the solution will be to execute a Maven command e.g. "mvn clean install" with a specific JAVA_HOME variable set as sub process (this requires, that Maven is installed and mvn is available as command). Use the ProcessBuilder to switch into the working directory, where the pom.xml of the target project is located and set the appropriate environment variable(s) before starting the process.
If Maven should not be installed at the enviroment your application is running on, you could also distribute a Maven installation with your application (maybe in a separate directory). Then you could run against the mvn.bat or mvn.sh of this distribution (depending on the os).
When using Eclipse, Build in Run as Configuration, go to the Environment tab and add the new JAVA_HOME variable.
Do not forget to check the Replace native environment ..... option.
This will override your default OS variable. No need to change at the OS level.

Want to know which Jenkins job is executing now, in java

I have 3 Jenkins job. Below are the name of those Jenkins job:
test_existing_api
test_others_api
test_new_api
I have a config file in java project which have 3 different configuration. I want to pick the configuration depend upon Jenkins job. So first I want to check which Jenkins job is executing and then I will take configuration according to that job. That configuration will further require in java code.
Please help me to understand how can I check which Jenkins job is executing now in Java.
Assuming you want to get this data within the job that is executing, see manual entry:
Jenkins Set Environment Variables
When a Jenkins job executes, it sets some environment variables that you may use in your shell script, batch command, Ant script or
Maven POM 1. The following table contains a list of all of these
environment variables.
Environment Variable Description
BUILD_NUMBER The current build number, such as "153"
BUILD_ID The current build id, such as "2005-08-22_23-59-59" (YYYY-MM-DD_hh-mm-ss)
BUILD_URL The URL where the results of this build can be found (e.g. http://buildserver/jenkins/job/MyJobName/666/)
NODE_NAME The name of the node the current build is running on. Equals 'master' for master node.
JOB_NAME Name of the project of this build. This is the name you gave your job when you first set it up. It's the third column of the Jenkins Dashboard main page.
BUILD_TAG String of jenkins-${JOB_NAME}-${BUILD_NUMBER}. Convenient to put into a resource file, a jar file, etc for easier identification.
JENKINS_URL Set to the URL of the Jenkins master that's running the build. This value is used by Jenkins CLI for example
EXECUTOR_NUMBER The unique number that identifies the current executor (among executors of the same machine) that's carrying out this build. This is the number you see in the "build executor status", except that the number starts from 0, not 1.
JAVA_HOME If your job is configured to use a specific JDK, this variable is set to the JAVA_HOME of the specified JDK. When this variable is set, PATH is also updated to have $JAVA_HOME/bin.
WORKSPACE The absolute path of the workspace.
SVN_REVISION For Subversion-based projects, this variable contains the revision number of the module. If you have more than one module specified, this won't be set.
CVS_BRANCH For CVS-based projects, this variable contains the branch of the module. If CVS is configured to check out the trunk, this environment variable will not be set.
GIT_COMMIT For Git-based projects, this variable contains the Git hash of the commit checked out for the build (like ce9a3c1404e8c91be604088670e93434c4253f03) (all the GIT_* variables require git plugin)
GIT_URL For Git-based projects, this variable contains the Git url (like git#github.com:user/repo.git or https://github.com/user/repo.git)\\
GIT_BRANCH For Git-based projects, this variable contains the Git branch that was checked out for the build (normally origin/master)
Sorry about bad formatting (SO doesn't support nice tables), but you should be able to retrieve these variables with System.getEnv(). This means you don't need to add anything to your Jenkins configuration, just read from java what it already sets.
http://<Jenkins_server>/job/<Job_name>/lastBuild/api/xml?depth=1
Above url will give you the xml structured data, in which you can check <building>false</building> tag value from your java code by parsing the xml.
If value is true than jenkins job is running at the time.
To check which of given three job is running, you can check the status of each job by parsing xml in java code for each job and get configuration file of running job.
P.S. : Replace the place holders in url with applicable data. <Jenkins_server> and <Job_name>
You could pass a system property from the job to the JVM when launching the project:
...whatever... -Dconfig=test_existing_api
and retrieve it in your classes with:
System.getProperty("config")

Categories