Getting the remote IP of the client which triggered a build

153 views
Skip to first unread message

Andreas Wuz

unread,
Nov 22, 2016, 7:16:45 AM11/22/16
to Jenkins Users
Hi @ all,

I am implementing an infrastructure with Jenkins. I hava a parameterized job with a basic shell script. This script can be run remotely, i.e. it could be triggered over an http-request like http://testserver.de:8009/job/receiveCSR/build?token=123. Now I need the IP of the client, which set triggered this build. When I see the console output

Started by remote host 10.1.1.111

I think, this should be possible, isn't it? If not, is there any plugin which I can use for this?

Thanks for your help!

Ted Xiao

unread,
Nov 22, 2016, 10:56:27 PM11/22/16
to Jenkins Users
You call access it by RemoteCause, pseudo code:


for (CauseAction action : build.getActions(CauseAction.class)) {
   
for (Cause cause : action.getCauses()) {
       
if(cause instanceof RemoteCause){
       
//blah
       
}
   
}
}

Ted Xiao

unread,
Nov 22, 2016, 10:58:55 PM11/22/16
to Jenkins Users

Ted Xiao

unread,
Nov 23, 2016, 2:13:35 AM11/23/16
to Jenkins Users
If you want to access it via shell script, you can install the EnvInject plugin and configure the plugin to Evaluated Groovy script

import hudson.model.*
import static hudson.model.Cause.RemoteCause


def ipaddress=""
for (CauseAction action : currentBuild.getActions(CauseAction.class)) {

   
for (Cause cause : action.getCauses()) {
       
if(cause instanceof RemoteCause){

             ipaddress
=cause.addr
             
break;
       
}
   
}
}
return ["ip":ipaddress]

then access the variable $ip in shell script

Andreas Wuz

unread,
Nov 24, 2016, 2:47:06 AM11/24/16
to Jenkins Users
Thanks, it works fine!
Reply all
Reply to author
Forward
0 new messages