Skip to content

Commit

Permalink
Merge pull request #1554 from OctopusDeploy/update-jenkins-queue-job
Browse files Browse the repository at this point in the history
Updating jenkins step template
  • Loading branch information
twerthi authored Sep 4, 2024
2 parents 1b3fae4 + eceb640 commit 1585471
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions step-templates/Jenkins-Queue-Job.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
"Name": "Jenkins - Queue Job",
"Description": "Trigger a job in Jenkins",
"ActionType": "Octopus.Script",
"Version": 7,
"Version": 8,
"CommunityActionTemplateId": null,
"Packages": [],
"Properties": {
"Octopus.Action.Script.Syntax": "PowerShell",
"Octopus.Action.Script.ScriptSource": "Inline",
"Octopus.Action.RunOnServer": "false",
"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"
"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$waitForComplete = $OctopusParameters['jqj_WaitForComplete']\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 $result = \"\"\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 \n if ($waitForComplete -eq \"True\")\n {\n while ($isBuilding -eq \"True\") { \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 \n Write-Host \"Retry Job Status: \" $result \" BuildNumber: \" $buildNumber \" IsBuilding: \" $isBuilding \n }\n }\n else\n {\n \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 \n } \n if ($failBuild) {\n if ($result -ne \"SUCCESS\") {\n if (![string]::IsNullOrWhitespace($result))\n {\n Write-Host \"Build ended with status: $result\"\n }\n else\n {\n Write-Host \"BUILD FAILURE: build status could not be obtained.\"\n }\n exit 1\n }\n }\n else\n {\n if ([string]::IsNullOrWhitespace($result))\n {\n Write-Warning \"Time-out expired before a status was returned.\"\n }\n else\n {\n Write-host \"Process ended with status: $result.\"\n }\n }\n}\ncatch {\n Write-Host \"Exception in jenkins job: $($_.Exception.Message)\"\n exit 1\n}\n"
},
"Parameters": [
{
Expand All @@ -27,8 +27,8 @@
{
"Id": "a52f7318-6f45-4e9f-b825-b3ae767608f8",
"Name": "jqj_FailBuild",
"Label": "Fail Build",
"HelpText": "Should this fail the deployment?",
"Label": "Fail Deployment",
"HelpText": "Fail the deployment if the job fails or time-out occurs.",
"DefaultValue": "false",
"DisplaySettings": {
"Octopus.ControlType": "Checkbox"
Expand All @@ -46,6 +46,16 @@
},
"Links": {}
},
{
"Id": "be446e7d-a1db-41e1-9df2-be2246d93052",
"Name": "jqj_WaitForComplete",
"Label": "Wait for complete",
"HelpText": "Wait until the job has completed, overrides `Timeout Duration`.",
"DefaultValue": "False",
"DisplaySettings": {
"Octopus.ControlType": "Checkbox"
}
},
{
"Id": "70e9cf06-3712-4950-a174-a5c5c7bd5858",
"Name": "jqj_BuildParam",
Expand Down Expand Up @@ -123,10 +133,10 @@
}
],
"LastModifiedOn": "2024-07-16T18:49:59.8950000Z",
"LastModifiedBy": "mspikes",
"LastModifiedBy": "twerthi",
"$Meta": {
"ExportedAt": "2021-09-14T13:38:58.1830000Z",
"OctopusVersion": "2024.1.11966",
"ExportedAt": "2024-09-03T21:12:00.926Z",
"OctopusVersion": "2024.2.9427",
"Type": "ActionTemplate"
},
"Category": "jenkins"
Expand Down

0 comments on commit 1585471

Please sign in to comment.