Chapter 10: Running a Jenkins job on a Dynamic Node

As a part of my work I had to identify a logged-in user and run a Jenkins Job on a node that was the his default server. I built a Job that uses the username a passes it to the next Job as the parameter node.

In order to run these jobs i used few plugins and scripts:

  1. NodeLabel Parameter Plugin – to enable the Job to run a node that has been passed as a parameter.
  2. EnvInject Plugin – This plugin makes it possible to have an isolated environment for your jobs.

How to do it?

  • Job A triggers Job B with Node paramter (called Node)

Job A

  • Adding NodeLabel Parameter Plugin and using it in Job A

JobANodeLabel

  1. Adding a new parameter called SERVERNAME to enable the user to type his own server to run on Job B as a Node
  2. Execute batch command with:
    • getting the servers_list file (line 1)
    • getting the CI_SERVERNAME from FOR /F command (line 2)
    • Deciding the SERVERNAME from the parameter or the server_list according to the logged_in user. (line 3)
    • Copying files to server (line 4)
    • Adding Execute batch command called ReplaceRTServerData.cmd that gets parameters (line 5)
      • build_number – build number to identify specific job run
      • ActualRTServerHost – Actual server (default of logged-in user or typed by the user)
      • BUILD_USER_ID – logged-in

(line 1)

set “ServersFile=\\netapp07b\AutomationReport\CI_Automation\RTI_Main\DevKitResources\servers_list.txt”

(line 2)

FOR /F “tokens=1,2,3 delims=,” %%I IN (‘ type “%ServersFile%” ^|findstr /i /r /c:”^%BUILD_USER_ID%,”‘) DO (set “BuildUserName=%%~nxI” && set “RTServerHost=%%~nxJ” && set “RTServerHostIP=%%~nxK”)

echo BuildUserName is:%BuildUserName%
echo RTServerHost is:%RTServerHost%
echo RTServerHostIP is:%RTServerHostIP%
echo RTServerGivenName is:%CI_SERVERNAME%

(line 3)

if “%CI_SERVERNAME%”==”” (goto NullValue) else (goto NotNullValue)

:NullValue
set ActualRTServerHost=%RTServerHost%
set ActualRTServerHostIP=%RTServerHostIP%
echo ActualRTServerHost is NullValue: %ActualRTServerHost%
echo ActualRTServerHostIP is NullValue: %ActualRTServerHostIP%
goto eof

:NotNullValue
FOR /F “skip=4 tokens=2 delims=:” %%i in (‘nslookup %CI_SERVERNAME%.e-glue.com 2^> nul’) DO (set “CI_SERVERNAME_IP=%%i”)
set “CI_SERVERNAME_IP=%CI_SERVERNAME_IP: =%”
echo %CI_SERVERNAME_IP%
set ActualRTServerHost=%CI_SERVERNAME%
set ActualRTServerHostIP=%CI_SERVERNAME_IP%
echo ActualRTServerHost is NotNullValue: %ActualRTServerHost%
echo ActualRTServerHostIP is NotNullValue: %ActualRTServerHostIP%
goto eof

:eof
echo Copying Configuration Parameteres Devkit files to %ActualRTServerHost%

(line 4)

echo Copying ReplaceRTServerData file to workspace
COPY /Y “\\netapp07b.nice.com\AutomationReport\CI_Automation\RTI_Main\DevKitResources\JenkinsResources\ReplaceRTServerData.cmd” “%workspace%\%build_number%_ReplaceRTServerData.cmd” || exit /b 1

(line 5)

echo Running string replacements
cd /d “%workspace%\”
“%build_number%_ReplaceRTServerData.cmd” %build_number% %ActualRTServerHost% %BUILD_USER_ID%

Here is the ReplaceRTServerData.cmd to run on the Jenkins workspace (you need to copy it by the batch command:

@echo off &setlocal
set “textfile=%1_ServerDetails.properties”

set “newfile=%1_ServerDetailsNew.properties”
set “Buildtextfile=%1_UserId.properties”

set “Buildnewfile=%1_UserIdNew.properties”
set BuildNumber=%1

set RTServerName=%2

set BuildUser=%3
echo NODE=%RTServerName% > “%newfile%”

del “%textfile%”

rename “%newfile%” “%textfile%”
echo BUILD_USER_ID=%BuildUser% > “%Buildnewfile%”

del “%Buildtextfile%”

rename “%Buildnewfile%” “%Buildtextfile%”

exit /b 0

JobABatch

3. Adding Inject Environment variables – we can use these parameters from the files with the EnvInject plugin.

For the UserId:

Properties file path: ${BUILD_NUMBER}_UserId.properties

properties content: PATH=$PTAH;${WORKSPACE}

For the Server details:

Properties file path: ${BUILD_NUMBER}_ServerDetails.properties

properties content: PATH=$PTAH;${WORKSPACE}

jobAEnvInject

4. Adding the trigger of Job B with the NODE

JobANODE

Job B

Adding parameter NODE (from NodeLabel plugin) as a parameterized job

JobBNode

Leave a comment