|
3 | 3 | "Name": "Jenkins - Queue Job",
|
4 | 4 | "Description": "Trigger a job in Jenkins",
|
5 | 5 | "ActionType": "Octopus.Script",
|
6 |
| - "Version": 7, |
| 6 | + "Version": 8, |
7 | 7 | "CommunityActionTemplateId": null,
|
8 | 8 | "Packages": [],
|
9 | 9 | "Properties": {
|
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$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" |
14 | 14 | },
|
15 | 15 | "Parameters": [
|
16 | 16 | {
|
|
27 | 27 | {
|
28 | 28 | "Id": "a52f7318-6f45-4e9f-b825-b3ae767608f8",
|
29 | 29 | "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.", |
32 | 32 | "DefaultValue": "false",
|
33 | 33 | "DisplaySettings": {
|
34 | 34 | "Octopus.ControlType": "Checkbox"
|
|
46 | 46 | },
|
47 | 47 | "Links": {}
|
48 | 48 | },
|
| 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 | + }, |
49 | 59 | {
|
50 | 60 | "Id": "70e9cf06-3712-4950-a174-a5c5c7bd5858",
|
51 | 61 | "Name": "jqj_BuildParam",
|
|
123 | 133 | }
|
124 | 134 | ],
|
125 | 135 | "LastModifiedOn": "2024-07-16T18:49:59.8950000Z",
|
126 |
| - "LastModifiedBy": "mspikes", |
| 136 | + "LastModifiedBy": "twerthi", |
127 | 137 | "$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", |
130 | 140 | "Type": "ActionTemplate"
|
131 | 141 | },
|
132 | 142 | "Category": "jenkins"
|
|
0 commit comments