Skip to content

Instantly share code, notes, and snippets.

@harniman
Last active June 13, 2016 16:42
Show Gist options
  • Save harniman/8f1418af794d26035171 to your computer and use it in GitHub Desktop.
Save harniman/8f1418af794d26035171 to your computer and use it in GitHub Desktop.
package net.harniman.workflow.jenkins
String test (host, user, pass, cmd) {
node("shared") {
init="curl -s -c .cookies ${host}"
userAttr="j_username=${user}"
passAttr="j_password=${pass}"
jsonAttr="json=%7B%22j_username%22%3A+%22${user}%22%2C+%22j_password%22%3A+%22${pass}%22%2C+%22remember_me%22%3A+false%2C+%22from%22%3A+%22%2F%22%7D"
login="curl -i -s -b .cookies -c .cookies -d $userAttr -d $passAttr -d 'from=%2F' -d $jsonAttr -d 'Submit=log+in' $host/j_acegi_security_check"
cmd="curl -L -s -b .cookies -c .cookies $host/$cmd"
echo "Initialising HTTP Connection"
sh "${init} > .init 2>&1"
echo "Performing Login"
sh "${login} > .login 2>&1"
def loginresponse = readFile '.login'
if (loginresponse =~ /Location:.*loginError/) {
echo "Error loging in"
error"Unable to login. Response = $loginresponse"
}
echo "Invoking command"
sh "${cmd} > .output 2>&1"
def output = readFile '.output'
sh "rm .init .login .output"
return output
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment