You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This update adds additional options when calling the Jenkins /API endpoint that can be used to rate limit the Invoke-RestMethod calls. These calls fetch the URL for the build and depending on the Jenkins server load, may cause excessive consecutive calls while trying to get the build URL. There is also an added dimension of sleeping between calls.
Copy file name to clipboardExpand all lines: step-templates/Jenkins-Queue-Job.json
+26-6Lines changed: 26 additions & 6 deletions
Original file line number
Diff line number
Diff line change
@@ -10,7 +10,7 @@
10
10
"Octopus.Action.Script.Syntax": "PowerShell",
11
11
"Octopus.Action.Script.ScriptSource": "Inline",
12
12
"Octopus.Action.RunOnServer": "false",
13
-
"Octopus.Action.Script.ScriptBody": "$jenkinsServer = $OctopusParameters['jqj_JenkinsServer'] \n$jenkinsUserName = $OctopusParameters['jqj_JenkinsUserName']\n$jenkinsUserPassword = $OctopusParameters['jqj_JenkinsUserPasword']\n$jobURL = $jenkinsServer + $OctopusParameters['jqj_JobUrl']\n$failBuild = [System.Convert]::ToBoolean($OctopusParameters['jqj_FailBuild'])\n$jobTimeout = $OctopusParameters['jqj_JobTimeout']\n$buildParam = $OctopusParameters['jqj_BuildParam']\n$checkIntervals = $OctopusParameters['jqj_checkInterval']\n\n$jobUrlWithParams = \"$jobURL$buildParam\"\n\nWrite-Host \"job url: \" $jobUrlWithParams \n\nfunction Get-JenkinsAuth\n{\n $params = @{}\n if (![string]::IsNullOrWhiteSpace($jenkinsUserName)) {\n $securePwd = ConvertTo-SecureString $jenkinsUserPassword -AsPlainText -Force \n $credential = New-Object System.Management.Automation.PSCredential ($jenkinsUserName, $securePwd) \n $head = @{\"Authorization\" = \"Basic \" + [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($jenkinsUserName + \":\" + $jenkinsUserPassword ))}\n $params = @{\n Headers = $head;\n Credential = $credential;\n ContentType = \"text/plain\";\n }\n }\n\n # If your Jenkins uses the \"Prevent Cross Site Request Forgery exploits\" security option (which it should), \n # when you make a POST request, you have to send a CSRF protection token as an HTTP request header.\n # https://wiki.jenkins.io/display/JENKINS/Remote+access+API\n try {\n $tokenUrl = $jenkinsServer + \"crumbIssuer/api/json?tree=crumbRequestField,crumb\"\n $crumbResult = Invoke-WebRequest -Uri $tokenUrl -Method Get @params -UseBasicParsing | ConvertFrom-Json\n Write-Host \"CSRF protection is enabled, adding CSRF token to request headers\"\n $params.Headers += @{$crumbResult.crumbRequestField = $crumbResult.crumb}\n } catch {\n Write-Host $Error[0]\n }\n return $params\n}\n\ntry {\n $authParams = Get-JenkinsAuth\n\n Write-Host \"Start the build\"\n $returned = Invoke-WebRequest -Uri $jobUrlWithParams -Method Post -UseBasicParsing @authParams\n Write-Host \"Job URL Link: $($returned.Headers['Location'])\"\n $jobResult = \"$($returned.Headers['Location'])/api/json\"\n $response = Invoke-RestMethod -Uri $jobResult -Method Get @authParams\n $buildUrl = $Response.executable.url\n while ($null -eq $buildUrl -or $buildUrl -eq \"\") {\n $response = Invoke-RestMethod -Uri $jobResult -Method Get @authParams\n $buildUrl = $Response.executable.url\n }\n Write-Host \"Build Number is: $($Response.executable.number)\"\n Write-Host \"Job URL Is: $($buildUrl)\"\n $buildResult = \"$buildUrl/api/json?tree=result,number,building\"\n \n $isBuilding = \"True\"\n $i = 0\n Write-Host \"Estimate Job Duration: \" $jobTimeout\n while ($isBuilding -eq \"True\" -and $i -lt $jobTimeout) { \n $i += 5\n Write-Host \"waiting $checkIntervals secs for build to complete\"\n Start-Sleep -s $checkIntervals\n $retyJobStatus = Invoke-RestMethod -Uri $buildResult -Method Get @authParams\n\n $isBuilding = $retyJobStatus[0].building\n $result = $retyJobStatus[0].result\n $buildNumber = $retyJobStatus[0].number\n Write-Host \"Retry Job Status: \" $result \" BuildNumber: \" $buildNumber \" IsBuilding: \" $isBuilding \n }\n if ($failBuild) {\n if ($result -ne \"SUCCESS\") {\n Write-Host \"BUILD FAILURE: build is unsuccessful or status could not be obtained.\"\n exit 1\n }\n }\n}\ncatch {\n Write-Host \"Exception in jenkins job: $($_.Exception.Message)\"\n exit 1\n}"
13
+
"Octopus.Action.Script.ScriptBody": "$jenkinsServer = $OctopusParameters['jqj_JenkinsServer'] \n$jenkinsUserName = $OctopusParameters['jqj_JenkinsUserName']\n$jenkinsUserPassword = $OctopusParameters['jqj_JenkinsUserPasword']\n$jobURL = $jenkinsServer + $OctopusParameters['jqj_JobUrl']\n$failBuild = [System.Convert]::ToBoolean($OctopusParameters['jqj_FailBuild'])\n$jobTimeout = $OctopusParameters['jqj_JobTimeout']\n$buildParam = $OctopusParameters['jqj_BuildParam']\n$checkIntervals = $OctopusParameters['jqj_checkInterval']\n$fetchBuildWait = $OctopusParameters['jqj_FetchBuildWait']\n$fetchBuildLimit = $OctopusParameters['jqj_FetchBuildLimit']\n\n$jobUrlWithParams = \"$jobURL$buildParam\"\n\nWrite-Host \"job url: \" $jobUrlWithParams \n\nfunction Get-JenkinsAuth\n{\n $params = @{}\n if (![string]::IsNullOrWhiteSpace($jenkinsUserName)) {\n $securePwd = ConvertTo-SecureString $jenkinsUserPassword -AsPlainText -Force \n $credential = New-Object System.Management.Automation.PSCredential ($jenkinsUserName, $securePwd) \n $head = @{\"Authorization\" = \"Basic \" + [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($jenkinsUserName + \":\" + $jenkinsUserPassword ))}\n $params = @{\n Headers = $head;\n Credential = $credential;\n ContentType = \"text/plain\";\n }\n }\n\n # If your Jenkins uses the \"Prevent Cross Site Request Forgery exploits\" security option (which it should), \n # when you make a POST request, you have to send a CSRF protection token as an HTTP request header.\n # https://wiki.jenkins.io/display/JENKINS/Remote+access+API\n try {\n $tokenUrl = $jenkinsServer + \"crumbIssuer/api/json?tree=crumbRequestField,crumb\"\n $crumbResult = Invoke-WebRequest -Uri $tokenUrl -Method Get @params -UseBasicParsing | ConvertFrom-Json\n Write-Host \"CSRF protection is enabled, adding CSRF token to request headers\"\n $params.Headers += @{$crumbResult.crumbRequestField = $crumbResult.crumb}\n } catch {\n Write-Host \"Failed to get CSRF token, CSRF may not be enabled\"\n Write-Host $Error[0]\n }\n return $params\n}\n\ntry {\n Write-Host \"Fetching Jenkins auth params\"\n $authParams = Get-JenkinsAuth\n\n Write-Host \"Start the build\"\n $returned = Invoke-WebRequest -Uri $jobUrlWithParams -Method Post -UseBasicParsing @authParams\n Write-Host \"Job URL Link: $($returned.Headers['Location'])\"\n $jobResult = \"$($returned.Headers['Location'])/api/json\"\n $response = Invoke-RestMethod -Uri $jobResult -Method Get @authParams\n $buildUrl = $Response.executable.url\n $c = 0\n while (($null -eq $buildUrl -or $buildUrl -eq \"\") -and ($c -lt $fetchBuildLimit) ) {\n $c += 1\n $response = Invoke-RestMethod -Uri $jobResult -Method Get @authParams\n $buildUrl = $Response.executable.url\n Start-Sleep -s $fetchBuildWait\n }\n Write-Host \"Build Number is: $($Response.executable.number)\"\n Write-Host \"Job URL Is: $($buildUrl)\"\n $buildResult = \"$buildUrl/api/json?tree=result,number,building\"\n \n $isBuilding = \"True\"\n $i = 0\n Write-Host \"Estimate Job Duration: \" $jobTimeout\n while ($isBuilding -eq \"True\" -and $i -lt $jobTimeout) { \n $i += 5\n Write-Host \"waiting $checkIntervals secs for build to complete\"\n Start-Sleep -s $checkIntervals\n $retyJobStatus = Invoke-RestMethod -Uri $buildResult -Method Get @authParams\n\n $isBuilding = $retyJobStatus[0].building\n $result = $retyJobStatus[0].result\n $buildNumber = $retyJobStatus[0].number\n Write-Host \"Retry Job Status: \" $result \" BuildNumber: \" $buildNumber \" IsBuilding: \" $isBuilding \n }\n if ($failBuild) {\n if ($result -ne \"SUCCESS\") {\n Write-Host \"BUILD FAILURE: build is unsuccessful or status could not be obtained.\"\n exit 1\n }\n }\n}\ncatch {\n Write-Host \"Exception in jenkins job: $($_.Exception.Message)\"\n exit 1\n}\n"
14
14
},
15
15
"Parameters": [
16
16
{
@@ -100,14 +100,34 @@
100
100
"Octopus.ControlType": "SingleLineText"
101
101
},
102
102
"Links": {}
103
+
},
104
+
{
105
+
"Id": "432dbaed-512e-4d29-9b4d-814a8b7c4846",
106
+
"Name": "jqj_FetchBuildWait",
107
+
"Label": "Fetch Build URL Wait(secs)",
108
+
"HelpText": "e.g. 10 Used when getting the build URL. Useful if Jenkins is busy and can't schedule job immediately or there are connection issues. Helps limit excess calls to /api",
109
+
"DefaultValue": "10",
110
+
"DisplaySettings": {
111
+
"Octopus.ControlType": "SingleLineText"
112
+
}
113
+
},
114
+
{
115
+
"Id": "cd222e86-cb94-495c-8a5a-8d4cd8b20d86",
116
+
"Name": "jqj_FetchBuildLimit",
117
+
"Label": "Fetch Build URL Limit",
118
+
"HelpText": "e.g. 5. Used to limit the number of times the script asks for the build URL. Used in with FetchBuildURLWait to limit the calls made to /api for build URL.",
0 commit comments