Get username logged in Jenkins from Jenkins Workflow (Pipeline) Plugin

后端 未结 10 687
忘了有多久
忘了有多久 2020-12-25 12:36

I am using the Pipeline plugin in Jenkins by Clouldbees (the name was Workflow plugin before), I am trying to get the user name in the Groovy script but I am not able to ach

10条回答
  •  难免孤独
    2020-12-25 13:01

    //Below is a generic groovy function to get the XML metadata for a Jenkins build.
    //curl the env.BUILD_URL/api/xml parse it with grep and return the string
    //I did an or true on curl, but possibly there is a better way
    //echo -e "some_string \c" will always return some_string without \n char     
    //use the readFile() and return the string
    def GetUserId(){
     sh """
     /usr/bin/curl -k -s -u \
     \$USERNAME:\$PASSWORD -o \
     /tmp/api.xml \
     \$BUILD_URL/api/xml || true 
    
     THE_USERID=`cat /tmp/api.xml | grep -oP '(?<=).*?(?=)'`
     echo -e "\$THE_USERID \\c" > /tmp/user_id.txt                               
     """
    def some_userid = readFile("/tmp/user_id.txt")
    some_userid
    }
    

提交回复
热议问题