Skip to content

Commit e516454

Browse files
committed
Update Jenkins Step Template
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.
1 parent a491fb3 commit e516454

1 file changed

Lines changed: 26 additions & 6 deletions

File tree

step-templates/Jenkins-Queue-Job.json

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"Octopus.Action.Script.Syntax": "PowerShell",
1111
"Octopus.Action.Script.ScriptSource": "Inline",
1212
"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"
1414
},
1515
"Parameters": [
1616
{
@@ -100,14 +100,34 @@
100100
"Octopus.ControlType": "SingleLineText"
101101
},
102102
"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.",
119+
"DefaultValue": "5",
120+
"DisplaySettings": {
121+
"Octopus.ControlType": "SingleLineText"
122+
}
103123
}
104124
],
105-
"LastModifiedOn": "2022-07-05T17:39:00.183Z",
106-
"LastModifiedBy": "brianpendell",
125+
"LastModifiedOn": "2024-07-16T18:49:59.8950000Z",
126+
"LastModifiedBy": "mspikes",
107127
"$Meta": {
108-
"ExportedAt": "2021-09-14T13:38:58.183Z",
109-
"OctopusVersion": "2021.3.2156",
128+
"ExportedAt": "2021-09-14T13:38:58.1830000Z",
129+
"OctopusVersion": "2024.1.11966",
110130
"Type": "ActionTemplate"
111131
},
112132
"Category": "jenkins"
113-
}
133+
}

0 commit comments

Comments
 (0)