Passing System Env variable into Jenkins Slave

ε祈祈猫儿з 提交于 2020-01-14 08:05:10

问题


This is starting to drive me crazy .. but it seems like I am out of luck to figure it out by myself :/

I need to set up a mechanism to share string value between two slaves, for example, named slave A and slave B.

Currently most closest one I have found is this SO Question: Jenkins Slave Environment Variable Refresh

But it still aims to enable access in buildscripts, not in the Slaves environment variable itself. (related SO Question: How are environment variables used in Jenkins with Windows Batch Command?)

Currently I have this setup:

1) on A the job A is triggered.

It performs this windows batch code (actual paths, obviously, are replaced with '<>'):

cmd /c start java -jar <path_of_slave>\slave.jar -jnlpUrl  <url_of_slave>/slave-agent.jnlp

setenv.exe /m HOR_BUILD_ID -delete
setenv.exe /m HOR_BUILD_ID %BUILD_NUMBER%
setenv.exe /m HOR_UPSTREAM_ID -delete
setenv.exe /m HOR_UPSTREAM_ID %JOB_NAME%

Pretty self-explanatory and straight-forward imho.

2) The job on A have upstream job B, that is triggered on successful build. So, in the slave B the B build executes this batch code(actual paths, obviously, are replaced with '<>'):

SetLocal EnableDelayedExpansion

@echo off
@echo --- Refreshing Environemnt variables... 
@echo on
set prgfil=%ProgramFiles(x86)%
call "%WORKSPACE%\..\..\..\tools\misc\resetvars.bat"
@echo --- Reading environment variables... 
copy /y "<path_containing_%HOR_UPSTREAM_ID%_variable" "<path_containing_%HOR_BUILD_ID%_variable"
if !errorlevel!==0 goto ok1
goto error
:ok1

goto end

:error
EndLocal
exit /b 1

:end
EndLocal

NOTE: resetvars.bat and resetvars.vbs is taken from this SO Question: Is there a command to refresh environment variables from the command prompt in Windows?


The output (%HOR_BUILD_ID% and %HOR_UPSTREAM_ID% values), still, is outdated ... only way to get new variables refreshed is to restart that darn jenkins (master) service, but this is definetly no-go in the CI environment...

The question now surfaces - how to make those variables to be refreshed / re-read when the build job B on B is triggered, so I could access system variables from slave w/o using, for example, EnvInject plugin?


Another SO Question and answer that needs user interaction, therefore not acceptable in my case: Jenkins - passing variables between jobs?


This also do not work in my case: http://comments.gmane.org/gmane.comp.java.hudson.user/37897


Maybe the Slave and its coresponding node do not recieve variable / resolve its environment coreectly, causing to be unable to retrieve information form Global envirtonemtn variables? Cechked it out and suddenly some interesting Environment variable config data cought my eye:

NODE_LABELS | a prodreleases

NODE_NAME | a

... this is for slave B , but it should be different values as this is originally for slave A ... What a hell??????????????????!!!! :/

I am starting to have feeling that root problem is that Jenkins has bug when resolving multiple slave NODE information.

Decided to create a Issue @ Jenkins jira: https://issues.jenkins-ci.org/browse/JENKINS-15397


回答1:


Have you looked at adding parameterized trigger plugin?

https://wiki.jenkins-ci.org/display/JENKINS/Parameterized+Trigger+Plugin

This should allow you to pass parameters from the first job to the second (like the job name to delete).




回答2:


I believe you're looking for Jenkins Parameterized Builds! Follow the instructions here, and you'll be up and running in no-time!




回答3:


Seems like this (https://issues.jenkins-ci.org/browse/JENKINS-6604) fix made the the following changes:

  • fixed inpossibility to install more than one slave in Windows 2k8 (R2) x64 (Datacenter) machine ;
  • fixed inpossibility to correctly resolve values. The setup was not changed ... but it seems like reentering all the logins did the trick.
  • probobly broke the Tool Environemnt settings as the NODE_LABELS and NODE_NAME is not loaded in System Information config anymore xD ... but who cares as long as this setup actually works ( and is not broken in next jenkins update ... ) :D

For reference, the jira issue I created is still open ...



来源:https://stackoverflow.com/questions/12670572/passing-system-env-variable-into-jenkins-slave

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