想知道现在在java中执行哪个Jenkins作业吗

想知道现在在java中执行哪个Jenkins作业吗,java,jenkins,Java,Jenkins,我有3个詹金斯的工作。下面是詹金斯工作的名字: 测试现有的api 测试\u其他\u api 测试新api 我在java项目中有一个配置文件,它有3种不同的配置。我想根据Jenkins的工作选择配置。所以首先我想检查哪个Jenkins作业正在执行,然后我将根据该作业进行配置。该配置将进一步需要java代码 请帮助我了解如何检查Java中正在执行的Jenkins作业。启动项目时,可以将作业的系统属性传递给JVM: ...whatever... -Dconfig=test_existing_api

我有3个詹金斯的工作。下面是詹金斯工作的名字:

  • 测试现有的api
  • 测试\u其他\u api
  • 测试新api
  • 我在java项目中有一个配置文件,它有3种不同的配置。我想根据Jenkins的工作选择配置。所以首先我想检查哪个Jenkins作业正在执行,然后我将根据该作业进行配置。该配置将进一步需要java代码


    请帮助我了解如何检查Java中正在执行的Jenkins作业。

    启动项目时,可以将作业的系统属性传递给JVM:

    ...whatever... -Dconfig=test_existing_api
    
    并使用以下工具在类中检索它:

    System.getProperty("config")
    

    假设要在正在执行的作业中获取此数据,请参阅:

    Jenkins设置环境变量

    当Jenkins作业执行时,它会设置一些环境变量,您可以在shell脚本、批处理命令、Ant脚本或脚本中使用这些变量 马文波姆1。下表列出了所有这些问题 环境变量

    很抱歉格式错误(因此不支持漂亮的表),但您应该能够使用检索这些变量。这意味着您不需要向Jenkins配置中添加任何内容,只需从java中读取它已经设置的内容即可

    http:///job//lastBuild/api/xml?depth=1

    上面的url将为您提供xml结构化数据,您可以通过解析xml从java代码中检查
    false
    标记值

    如果值为
    true
    ,则此时jenkins作业正在运行

    要检查给定三个作业中的哪一个正在运行,可以通过检查每个作业的状态,并获取正在运行的作业的配置文件


    注意:用适用数据替换url中的占位符

    您的意思是要从正在执行的作业内部进行检查,还是从Jenkins之外的其他软件进行检查?我要在作业内部进行检查..谢谢您的建议。我应该在Build-->Execute shell中传递系统属性(来自Jenkins作业),或者从其他地方传递系统属性,具体取决于如何启动java应用程序。如果您执行一个shell脚本,那么是的,如果它是一个maven作业,那么将其添加到
    目标和选项
    行,etcI执行shell脚本,我更新执行shell如下:1)pushd src 2)chmod+x./setenv.sh 3)source./setenv.sh 4)ant cleanall 5)ant compile 6)-Dconfig=test_existing_api 7)ant runAllTests和getting behing-exception+-Dconfig=test_existing_api/tmp/hudson5074006777121298600.sh:第7行:-Dconfig=test_existing_api:command not found这一点你的问题变得不清楚了。如果需要该系统参数来访问测试,则可能需要将属性从jenkins传递到ant[6)ant Dconfig=test\u existing\u api runAllTests],然后从ant传递到junit测试[您可以使用
    system.getProperty(“config”)访问该测试]
    。如果您能更新问题,完整描述您的jenkins工作,并在java课程中包括您需要在哪里做出决定,这将非常有帮助
    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)