Created
April 26, 2015 14:30
-
-
Save SteveGilham/0539620ac5c95ad0714c to your computer and use it in GitHub Desktop.
Configuring Jenkins with PowerShell
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Fill in the blanks | |
$UserName = ??? | |
$JenkinsAPIToken = ??? | |
$JENKINS_URL = ??? | |
$JOB_URL = ??? | |
# Create client with pre-emptive authentication | |
# see => http://www.hashemian.com/blog/2007/06/http-authorization-and-net-webrequest-webclient-classes.htm | |
$webClient = new-object System.Net.WebClient | |
$webclient.Headers.Add("Authorization","Basic "+ | |
[System.Convert]::ToBase64String( | |
[System.Text.Encoding]::ASCII.GetBytes("$($UserName):$JenkinsAPIToken"))) | |
# fetch CSRF token as authenticated user | |
# see => https://wiki.jenkins-ci.org/display/JENKINS/Remote+access+API | |
$crumbURL = $JENKINS_URL + "/crumbIssuer/api/xml" | |
$crumbs = [xml]$webClient.DownloadString($crumbURL) | |
# set the CSRF token in the headers | |
$webclient.Headers.Add($crumbs.defaultCrumbIssuer.crumbRequestField, $crumbs.defaultCrumbIssuer.crumb) | |
# GET the job configuration (you don't actually need the CSRF token for this | |
# but it's better to get that token once at the top in case you want to do multiple | |
# operations e.g. setting parameters on many jobs, in this script) | |
$configURL = $JOB_URL + "config.xml" | |
$config = [xml]$webClient.DownloadString($configURL) | |
# do whatever transformation you need to the XML | |
# POST back | |
# see => http://stackoverflow.com/questions/5401501/how-to-post-data-to-specific-url-using-webclient-in-c-sharp | |
try { | |
$webClient.Encoding = [System.Text.Encoding]::UTF8 | |
$webclient.Headers.Add([System.Net.HttpRequestHeader]::ContentType,"application/xml") | |
$webclient.UploadString($configURL, $config.OuterXml) | |
} | |
finally ## reset headers for client re-use if you're processing many jobs | |
## (or .Dispose() the client if you're done) | |
{ | |
$webClient.Headers.Remove([System.Net.HttpRequestHeader]::ContentType) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment