Noel Yap
Noel Yap

Reputation: 19808

How programmatically to get Jenkins user id?

I would like to pass the user id of the person who started a Jenkins job to a script. The output of 'env' indicates no environment variables are set and the output of 'ant -v' indicates no properties are set. How do I access the user id of the person that started a job? (I understand that triggers can start jobs, but for this job, it will always be a person).

Upvotes: 2

Views: 4379

Answers (2)

Vasee
Vasee

Reputation: 1

BUILD_START_USER=`curl ${JOB_URL}'lastBuild/api/json' | python -c 'import json,sys;obj=json.loads(sys.stdin.read());print obj["'"actions"'"][1]["'"causes"'"][0]["'"userId"'"]'`

Upvotes: 0

Noel Yap
Noel Yap

Reputation: 19808

To get the job executor:

curl -s "${BUILD_URL}/api/json" | \
python -c '\
    import json; \
    import sys; \
    obj = json.loads(sys.stdin.read()); \
    print [ \
        cause["userId"] \
        for action in obj["actions"] \
        if "causes" in action \
        for cause in action["causes"] \
        if "userId" in cause][0];'

Also see How to set environment variables in Jenkins? which explains how to pass it into a script.

Upvotes: 3

Related Questions