Skip to content

Commit 1585471

Browse files
authored
Merge pull request #1554 from OctopusDeploy/update-jenkins-queue-job
Updating jenkins step template
2 parents 1b3fae4 + eceb640 commit 1585471

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

step-templates/Jenkins-Queue-Job.json

+17-7
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
"Name": "Jenkins - Queue Job",
44
"Description": "Trigger a job in Jenkins",
55
"ActionType": "Octopus.Script",
6-
"Version": 7,
6+
"Version": 8,
77
"CommunityActionTemplateId": null,
88
"Packages": [],
99
"Properties": {
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$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"
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$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"
1414
},
1515
"Parameters": [
1616
{
@@ -27,8 +27,8 @@
2727
{
2828
"Id": "a52f7318-6f45-4e9f-b825-b3ae767608f8",
2929
"Name": "jqj_FailBuild",
30-
"Label": "Fail Build",
31-
"HelpText": "Should this fail the deployment?",
30+
"Label": "Fail Deployment",
31+
"HelpText": "Fail the deployment if the job fails or time-out occurs.",
3232
"DefaultValue": "false",
3333
"DisplaySettings": {
3434
"Octopus.ControlType": "Checkbox"
@@ -46,6 +46,16 @@
4646
},
4747
"Links": {}
4848
},
49+
{
50+
"Id": "be446e7d-a1db-41e1-9df2-be2246d93052",
51+
"Name": "jqj_WaitForComplete",
52+
"Label": "Wait for complete",
53+
"HelpText": "Wait until the job has completed, overrides `Timeout Duration`.",
54+
"DefaultValue": "False",
55+
"DisplaySettings": {
56+
"Octopus.ControlType": "Checkbox"
57+
}
58+
},
4959
{
5060
"Id": "70e9cf06-3712-4950-a174-a5c5c7bd5858",
5161
"Name": "jqj_BuildParam",
@@ -123,10 +133,10 @@
123133
}
124134
],
125135
"LastModifiedOn": "2024-07-16T18:49:59.8950000Z",
126-
"LastModifiedBy": "mspikes",
136+
"LastModifiedBy": "twerthi",
127137
"$Meta": {
128-
"ExportedAt": "2021-09-14T13:38:58.1830000Z",
129-
"OctopusVersion": "2024.1.11966",
138+
"ExportedAt": "2024-09-03T21:12:00.926Z",
139+
"OctopusVersion": "2024.2.9427",
130140
"Type": "ActionTemplate"
131141
},
132142
"Category": "jenkins"

0 commit comments

Comments
 (0)