0

I am building a jenkins pipeline and the job can be triggered by remote. I have the requirement to know which IP triggered the job. So I have a little groovy script, which returns the remote IP. With the EnvInject-plugin I can easily use this variable in a normal freestyle job, but how can I use this in the pipeline scirpt? I can't use the EnvInject-plugin with the pipeline-plugin :(

Here is the little script for getting the IP:

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]

1 Answer 1

0

You can create a shared library function (see here for examples and the directory structure). This is one of the undocumented (or really hard to find any documentation) features of Jenkins.

If you would put a file triggerIp.groovy in the directory vars, which is in the directory workflow-libs at the root level of JENKINS_HOME and put your code in that file. The full filename then will be $JENKINS_HOME/workflow-libs/vars/ipTrigger.groovy (You can even make a git repo for your shared libraries and clone it in that directory)

// workflow-libs/vars/ipTrigger.groovy
import hudson.model.*
import static hudson.model.Cause.RemoteCause

@com.cloudbees.groovy.cps.NonCPS
def call(currentBuild) {
    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]
}

After a restart of Jenkins, from your pipeline script, you can call the method by the filename you gave it.

So from your pipeline just call def trigger = ipTrigger(currentBuild)

The the ipaddress will be, trigger.ip (sorry for the bad naming, couldn't come up with something original)

Sign up to request clarification or add additional context in comments.

7 Comments

Maybe a better name would be trigger.originIPAddress which would make it a bit more clear what's behind it.
Thanks for your anwser! I did not have a vars-folder, so I created one. After a restart I get: java.lang.NoSuchMethodError: No such DSL method 'ipTrigger' found among steps, so I think he doesn't find the method? The name of the file is ipTrigger.groovy and call in the pipeline script is the same as you did (could it be, that the name of your file and the call does'nt match?) Did I do somethink wrong?
Sorry, my mistake. the directory structure should be $JENKINS_HOME/workflow-libs/vars/ipTrigger.groovy. Note the workflow-libs directory I omitted in my example. Updated answer as well
Thanks, this works, but where do you get the currentBuild-variable in the pipeline script?
Ok thanks, but I get an error in the script. Maybe you know the answer here, too: stackoverflow.com/questions/41563550/…
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.