Advertisement
Guest User

description.sh

a guest
Jul 24th, 2013
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.58 KB | None | 0 0
  1. #! /bin/bash
  2. # description.sh
  3. ########################################################################
  4. # CHANGE JENKINS BUILD DESCRIPTION
  5. #
  6. USAGE="
  7.    description.sh [-a|-p] [-u <JenkinsURL>] -j <JenkinsJob> \\
  8.     -b <jenkinsBuild> -d <description> [-c <color>]
  9.  
  10.     where:
  11.         -a: Append new description to current description
  12.         -p: Prepend new description to current description
  13.         Description replaced if neither -p or -a are
  14.         specified.
  15.         -d: Description for build
  16.         -u: Full URL of Jenkins server. If not given, it will be taken
  17.             from value embedded in program.
  18.         -j: Jenkins 'Job' name (aka 'project).
  19.         -b: Build Number
  20.         -c: Color of text
  21.         -h: Display this help text
  22. "
  23. #
  24. ########################################################################
  25.  
  26. ########################################################################
  27. # FUNCTIONS
  28. #
  29. function error {
  30.     printf "ERROR: $1\n$USAGE\n" 1>&2
  31.     exit 2
  32. }
  33. #
  34. ########################################################################
  35.  
  36. ########################################################################
  37. # MAIN PROGRAM
  38. #
  39.  
  40. jenkins_url="http://xxxxxxxxxxxxxx/jenkins"
  41. prepend_flag=""
  42. append_flag=""
  43. readonly USER="xxxxxxxxxx"
  44. readonly PASSWORD="xxxxxxxxxxxxxxxxxxxxxxxxxxxx"
  45.  
  46. #
  47. # Set options
  48. #
  49. while getopts :aphd:u:j:b:c: option
  50. do
  51.     case $option in
  52.     a)  append_flag=1;;
  53.     p)  prepend_flag=1;;
  54.     u)  jenkins_url="$OPTARG";;
  55.     j)  job_name="$OPTARG";;
  56.     b)  build_number="$OPTARG";;
  57.     c)  color="$OPTARG";;
  58.     d)  new_description="$OPTARG";;
  59.     h)  printf "\n$USAGE\n"
  60.         exit 0;;
  61.     *)  error "Invalid Argument";;
  62.     esac
  63. done
  64. shift $(( $OPTIND - 1 ))
  65.  
  66. #
  67. # Verify options
  68. #
  69.  
  70. [ -z "$new_description" ]   && error "Missing parameter '-d': Description"
  71. [ -z "$build_number" ]      && error "Missing parameter '-b': Build Number"
  72. [ -z "$job_name" ]      && error "Missing parameter '-j': Job Name"
  73.  
  74. #
  75. # Get old description (if -a or -p flag)
  76. #
  77.  
  78. if [ -n "$append_flag" -o -n "$prepend_flag" ]
  79. then
  80.     old_description=$(curl -s --user "$USER:$PASSWORD" --data-urlencode "tree=description" \
  81.     "$jenkins_url/job/$job_name/$build_number/api/json")
  82.     [ $? -ne 0 ] && error "Could not get description: $?"
  83. fi
  84. old_description=${old_description#*:\"} #Remove JSON garbage
  85. old_description=${old_description%\"\}} #Remove JSON garbage
  86.  
  87. #
  88. # Translate linefeeds
  89. #
  90. old_description=$(sed 's/\\r\\n/\
  91. /g' <<<$old_description)
  92.  
  93. echo "Old Description = '$old_description'"
  94.  
  95. #
  96. # Create the new description
  97. #
  98. if [ -n "$color" ] #Add color to the description
  99. then
  100.     new_description="<font color=$color><b>$new_description</b></font>"
  101. fi
  102.  
  103. if [ "$prepend_flag" -a -n "$old_description" ] #Prepend new description to old description
  104. then
  105.     new_description="$new_description<br/>
  106. $old_description"
  107. elif [ "$append_flag" -a -n "$old_description" ] #Append new description to old description
  108. then
  109.     new_description="$old_description<br/>
  110. $new_description"
  111. fi
  112.  
  113. #
  114. # Now set the description
  115. #
  116.  
  117. if curl -u $USER:$PASSWORD   --data-urlencode "description=$new_description" \
  118.     --data-urlencode "Submit=Submit" \
  119.     "$jenkins_url/job/$job_name/$build_number/submitDescription"
  120. then
  121.     echo "Description successfully changed on Build #$build_number in Jenkins job $job_name"
  122. else
  123.     echo "WARNING: Description was not set. Manually change the descripiton of the build"
  124.     echo "         for Build #$build_number in Jenkins job $job_name"
  125. fi
  126. echo "The description appears below"
  127. echo "$new_description"
  128. #
  129. # DONE
  130. ########################################################################
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement