Skip to content

Instantly share code, notes, and snippets.

@redsox2002
Last active March 28, 2017 15:03
Show Gist options
  • Save redsox2002/aaad60c6823617cb858ff78a40bb9ff8 to your computer and use it in GitHub Desktop.
Save redsox2002/aaad60c6823617cb858ff78a40bb9ff8 to your computer and use it in GitHub Desktop.
This will get the user who kicked off the Jenkins job using the Jenkins API
#!/bin/bash
# This script will get the pipeline user who kicked off the job in Jenkins. This is useful if you want to display that user in Slack or another notification system
# Insert your Jenkins URL in the $JENKINS_URL variable
export URL=https://$JENKINS_URL/$BUILD_NUMBER/api/xml
# Insert your GitHub organization as well as the repo name
export WORKSPACE=/srv/jenkins/workspace/Organization/project-pipeline-master
cd $WORKSPACE
# Installs necessary package to use xmllint command for RHEL-based systems
sudo yum -y install libxml2-devel.x86_64
# GIT_CREDS will have your username and personal access token while crumb can be generated by curl https://$JENKINS_URL/crumbIssuer/api/xml --user user:token
curl -X POST $URL -u $GIT_CREDS -H $CRUMB > results.xml
# This will format the xml after it downloads
xmllint --format results.xml > formatted_results.xml
# This will awk for the user who kicked off the Jenkins job
user=$(grep 'fullName' formatted_results.xml | awk -F">" '{print $2}' | awk -F"<" '{print $1}')
echo "The user who kicked off Jenkins pipeline is $user"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment