|
| 1 | +param ( |
| 2 | + [string]$organizationName, |
| 3 | + [string]$projectName, |
| 4 | + [string]$personalAccessToken, |
| 5 | + [string]$pipelineNamePart = "Continuous Delivery", |
| 6 | + [switch]$skipDestroy = $false, |
| 7 | + [int]$maximumRetries = 10, |
| 8 | + [int]$retryCount = 0, |
| 9 | + [int]$retryDelay = 10000 |
| 10 | +) |
| 11 | + |
| 12 | +function Invoke-Pipeline { |
| 13 | + param ( |
| 14 | + [string]$organizationName, |
| 15 | + [string]$projectName, |
| 16 | + [int]$pipelineId, |
| 17 | + [string]$pipelineAction = "", |
| 18 | + [hashtable]$headers |
| 19 | + ) |
| 20 | + $pipelineDispatchUrl = "https://dev.azure.com/$organizationName/$projectName/_apis/pipelines/$pipelineId/runs?api-version=7.2-preview.1" |
| 21 | + Write-Host "Pipeline Dispatch URL: $pipelineDispatchUrl" |
| 22 | + |
| 23 | + $pipelineDispatchBody = @{} |
| 24 | + if($pipelineAction -eq "") { |
| 25 | + $pipelineDispatchBody = @{ |
| 26 | + "resources" = @{ |
| 27 | + "repositories" = @{ |
| 28 | + "self" = @{ |
| 29 | + "refName" = "refs/heads/main" |
| 30 | + } |
| 31 | + } |
| 32 | + } |
| 33 | + } | ConvertTo-Json -Depth 100 |
| 34 | + } else { |
| 35 | + $pipelineDispatchBody = @{ |
| 36 | + "resources" = @{ |
| 37 | + "repositories" = @{ |
| 38 | + "self" = @{ |
| 39 | + "refName" = "refs/heads/main" |
| 40 | + } |
| 41 | + } |
| 42 | + } |
| 43 | + "templateParameters" = @{ |
| 44 | + "terraform_action" = $pipelineAction |
| 45 | + } |
| 46 | + } | ConvertTo-Json -Depth 100 |
| 47 | + } |
| 48 | + |
| 49 | + $result = Invoke-RestMethod -Method POST -Uri $pipelineDispatchUrl -Headers $headers -Body $pipelineDispatchBody -StatusCodeVariable statusCode -ContentType "application/json" |
| 50 | + if ($statusCode -ne 200) { |
| 51 | + throw "Failed to dispatch the pipeline." |
| 52 | + } |
| 53 | + |
| 54 | + # Get the pipeline run id |
| 55 | + $pipelineRunId = $result.id |
| 56 | + Write-Host "Pipeline Run ID: $pipelineRunId" |
| 57 | + return [int]$pipelineRunId |
| 58 | +} |
| 59 | + |
| 60 | +function Wait-ForPipelineRunToComplete { |
| 61 | + param ( |
| 62 | + [string]$organizationName, |
| 63 | + [string]$projectName, |
| 64 | + [int]$pipelineId, |
| 65 | + [int]$pipelineRunId, |
| 66 | + [hashtable]$headers |
| 67 | + ) |
| 68 | + |
| 69 | + $pipelineRunUrl = "https://dev.azure.com/$organizationName/$projectName/_apis/pipelines/$pipelineId/runs/$($pipelineRunId)?api-version=7.2-preview.1" |
| 70 | + Write-Host "Pipeline Run URL: $pipelineRunUrl" |
| 71 | + |
| 72 | + $pipelineRunStatus = "" |
| 73 | + $pipelineRunResult = "" |
| 74 | + while($pipelineRunStatus -ne "completed") { |
| 75 | + Start-Sleep -Seconds 10 |
| 76 | + $pipelineRun = Invoke-RestMethod -Method GET -Uri $pipelineRunUrl -Headers $headers -StatusCodeVariable statusCode |
| 77 | + if ($statusCode -lt 300) { |
| 78 | + $pipelineRunStatus = $pipelineRun.state |
| 79 | + $pipelineRunResult = $pipelineRun.result |
| 80 | + Write-Host "Pipeline Run Status: $pipelineRunStatus - Conclusion: $pipelineRunResult" |
| 81 | + } else { |
| 82 | + Write-Host "Failed to find the pipeline run. Status Code: $statusCode" |
| 83 | + throw "Failed to find the pipeline run." |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + if($pipelineRunResult -ne "succeeded") { |
| 88 | + throw "The pipeline run did not complete successfully. Conclusion: $pipelineRunResult" |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +# Setup Variables |
| 93 | +$token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$personalAccessToken")) |
| 94 | +$headers = @{ |
| 95 | + "Authorization" = "Basic $token" |
| 96 | + "Accept" = "application/json" |
| 97 | +} |
| 98 | + |
| 99 | +# Run the Module in a retry loop |
| 100 | +$success = $false |
| 101 | + |
| 102 | +do { |
| 103 | +$retryCount++ |
| 104 | +try { |
| 105 | + # Get the pipeline id |
| 106 | + Write-Host "Getting the pipeline id" |
| 107 | + $pipelinesUrl = "https://dev.azure.com/$organizationName/$projectName/_apis/pipelines?api-version=7.2-preview.1" |
| 108 | + Write-Host "Pipelines URL: $pipelinesUrl" |
| 109 | + $pipelines = Invoke-RestMethod -Method GET -Uri $pipelinesUrl -Headers $headers -StatusCodeVariable statusCode |
| 110 | + if ($statusCode -ne 200) { |
| 111 | + throw "Failed to find the pipelines." |
| 112 | + } |
| 113 | + $pipeline = $pipelines.value | Where-Object { $_.name -like "*$pipelineNamePart*" } |
| 114 | + |
| 115 | + $pipelineId = $pipeline.id |
| 116 | + Write-Host "Pipeline ID: $pipelineId" |
| 117 | + |
| 118 | + $pipelineAction = "" |
| 119 | + if(!($skipDestroy)) { |
| 120 | + $pipelineAction = "apply" |
| 121 | + } |
| 122 | + |
| 123 | + # Trigger the apply pipeline |
| 124 | + Write-Host "Triggering the $pipelineAction pipeline" |
| 125 | + $pipelineRunId = Invoke-Pipeline -organizationName $organizationName -projectName $projectName -pipelineId $pipelineId -pipelineAction $pipelineAction -headers $headers |
| 126 | + Write-Host "$pipelineAction pipeline triggered successfully" |
| 127 | + |
| 128 | + # Wait for the apply pipeline to complete |
| 129 | + Write-Host "Waiting for the $pipelineAction pipeline to complete" |
| 130 | + Wait-ForPipelineRunToComplete -organizationName $organizationName -projectName $projectName -pipelineId $pipelineId -pipelineRunId $pipelineRunId -headers $headers |
| 131 | + Write-Host "$pipelineAction pipeline completed successfully" |
| 132 | + |
| 133 | + if($skipDestroy) { |
| 134 | + $success = $true |
| 135 | + break |
| 136 | + } |
| 137 | + |
| 138 | + $pipelineAction = "destroy" |
| 139 | + |
| 140 | + # Trigger the destroy pipeline |
| 141 | + Write-Host "Triggering the $pipelineAction pipeline" |
| 142 | + $pipelineRunId = Invoke-Pipeline -organizationName $organizationName -projectName $projectName -pipelineId $pipelineId -pipelineAction "destroy" -headers $headers |
| 143 | + Write-Host "$pipelineAction pipeline triggered successfully" |
| 144 | + |
| 145 | + # Wait for the apply pipeline to complete |
| 146 | + Write-Host "Waiting for the $pipelineAction pipeline to complete" |
| 147 | + Wait-ForPipelineRunToComplete -organizationName $organizationName -projectName $projectName -pipelineId $pipelineId -pipelineRunId $pipelineRunId -headers $headers |
| 148 | + Write-Host "$pipelineAction pipeline completed successfully" |
| 149 | + |
| 150 | + $success = $true |
| 151 | +} catch { |
| 152 | + Write-Host $_ |
| 153 | + Write-Host "Failed to trigger the pipeline successfully, trying again..." |
| 154 | +} |
| 155 | +} while ($success -eq $false -and $retryCount -lt $maximumRetries) |
| 156 | + |
| 157 | +if ($success -eq $false) { |
| 158 | + throw "Failed to trigger the pipeline after $maximumRetries attempts." |
| 159 | +} |
0 commit comments