Access Jenkins Environment Variables in Java Program

安稳与你 提交于 2021-02-10 08:10:08

问题


Is there any way to access jenkins environment variables (BUILD_NUMBER etc)in a java program without doing any changes in jenkins job.

I am thinking if there is any listener (Jenkins or Maven) that I can hook into for getting jenkins enviroment variables and then I can set them as System properties and access anywhere in my java program.

I came across EnvInject plugin but that would require job changes. I am looking for a solution where I can get access programmatically!!

Ref: https://wiki.jenkins-ci.org/display/JENKINS/Building+a+software+project#Buildingasoftwareproject-JenkinsSetEnvironmentVariables


回答1:


From that documentation is clear, that you can pass these values as VM options in this style:

clean install -DBUILD_NUMBER=${BUILD_NUMBER} -DBUILD_ID=${BUILD_ID} ... etc.

(and access them in your java program via System.getProperty(...)).




回答2:


In Jenkins if you want to set an environment variable and access it in your java code you can use below steps :

  1. Create an environment variable in Jenkins in Configure system menu in Manage Jenkins page. For example, you have added an environment variable named 'Browser'.
  2. You can access the same environment variable in java program using below code.

    System.out.println(System.getenv("Browser"));
    



回答3:


A "hacky" but works, you can create a groovy script that gets the environment variables from the build, and then trigger it with the jenkins-cli.

for example, groovy script for getting environment variables from a build:

import hudson.model.*
import jenkins.model.*

def jenkins = Jenkins.getInstance()
def jobName = this.args[0]
def buildNumber = Integer.valueOf(this.args[1])
def job = jenkins.getItem(jobName)
def bld = job.getBuildByNumber(buildNumber)
bld.environment.each{
  println it
}

to trigger it with the jenkins-cli use the following command:

java -jar pathToCli/jenkins-cli.jar -s http://myJenkins:8080/ groovy getBuildEnvVars.groovy jobName bldNum

then to trigger it from java code, you can do something like:

public static void main(String[] args) {
        Process proc = null;
        String jenkinsCliPath = <path to jenkins cli jar>;
        String jenkinsUrl = <jenkins url>;
        String groovyScriptPath =<pathToGroovyScript>;
        String jobName = <jobName> ;
        int buildNumber = <build number>;

        try {
            String cliCmd = "java -jar "+ jenkinsCliPath +"  -s "
                    + jenkinsUrl +" groovy " + groovyScriptPath + " " + jobName + " "
                    + buildNumber;
            proc = Runtime.getRuntime().exec(cliCmd);
            proc.waitFor();
            InputStream is = proc.getInputStream();
            byte b[] = new byte[is.available()];
            is.read(b, 0, b.length);
            System.out.println(new String(b));

        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

Note: The user that runs the script have to be logged in. so wither perform a login with the cli, or add the public key of the user to the relevant user in Jenkins.




回答4:


Already the jenkins job are predefined with set of some environment variables. These list of variables with name and description will be available in the below url

http:// Your Jenkin URL/env-vars.html (Ex:http://cptest-Jenkins/env-vars.html)

These list of environment variables can be easily accessed using below code.

System.out.println("Build Number:"+System.getenv("BUILD_NUMBER")); //BUILD_NUMBER - name of the environment variable

This will print the current build number. Same way you can access other variables also.



来源:https://stackoverflow.com/questions/43064139/access-jenkins-environment-variables-in-java-program

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!