Skip to content

Instantly share code, notes, and snippets.

@nehemiahj
Created August 25, 2023 20:37
Show Gist options
  • Save nehemiahj/6f4ed8dc2a9080bd55fdccafb753a485 to your computer and use it in GitHub Desktop.
Save nehemiahj/6f4ed8dc2a9080bd55fdccafb753a485 to your computer and use it in GitHub Desktop.
XM Cloud Retrieve Latest Deployment Status
<#
.SYNOPSIS
Get the status of latest XM Cloud Deployment in an environment
.NOTES
NEHEMIAH JEYAKUMAR
#>
# !!! SENSITTIVE INFORMATION !!!
# Store it in Encrypted Connection String and retrieve it or Key Vault
$orgClientId = 'XXXXXXXXXXXXXXXXXX'
$orgClientSecret = 'XXXXXXXXXXXXXXXXXXXXXX'
$envId = 'XXXXXXXXXXXXXXXXXXXXXXXX'
$body = @{
client_id = $orgClientId
audience = "https://api.sitecorecloud.io"
client_secret = $orgClientSecret
grant_type = "client_credentials"
}
try { $tokenRequest = Invoke-WebRequest -Method Post -Uri "https://auth.sitecorecloud.io/oauth/token" -ContentType "application/x-www-form-urlencoded" -Body $body -UseBasicParsing -ErrorAction Stop }
catch { Write-Host "Unable to obtain access token, aborting..."; return }
$token = ($tokenRequest.Content | ConvertFrom-Json).access_token
# ==================================
# Optional 1 START
### You can have additional check whether the current hostname matches with the environment.
#$authHeader1 = @{
# 'Content-Type'='application\json'
# 'Authorization'="Bearer $token"
#}
#$URL = "https://xmclouddeploy-api.sitecorecloud.io/api/environments/v1/$envId"
#$envResponse = Invoke-WebRequest -Headers $AuthHeader1 -Uri $URL
#$envObj = ($envResponse.Content | ConvertFrom-Json)
#$envObj.Host
# Optional 1 END
# ==================================
$authHeader1 = @{
'Content-Type'='application\json'
'Authorization'="Bearer $token"
}
$URL = "https://xmclouddeploy-api.sitecorecloud.io/api/environments/v1/$envId/deployments"
$depResponse = Invoke-WebRequest -Headers $AuthHeader1 -Uri $URL -UseBasicParsing -ErrorAction Stop
$depObj = ($depResponse.Content | ConvertFrom-Json)
$lastDeployment = $depObj | Sort-Object -Property createdAt -Descending | Select-Object -First 1
if ($lastDeployment) {
$status = switch($lastDeployment.calculatedStatus){
0 {"Queued"; break}
1 {"In Progress"; break}
2 {"Completed"; break }
3 {"Failed"; break}
}
$dataPoints = $status, $lastDeployment.id, $lastDeployment.createdAt , $lastDeployment.triggerMessage
$result = $dataPoints -join ' | '
Show-Alert "Last Deployment: $result"
}
else {
Show-Alert "There is no deployment history."
}
Close-Window
# ==================================
# Optional 2 START
### To show all the recent deployment requests in a report window
#$queuedRequest = $depObj | Sort-Object -Property createdAt -Descending | Where-Object {$_.calculatedStatus -eq 0} | Select-Object -First 1
#$inProgress = $depObj | Where-Object {$_.calculatedStatus -eq 1} | Select-Object -First 1
#$lastCompleted = $depObj | Sort-Object -Property createdAt -Descending | Where-Object {$_.calculatedStatus -eq 2} | Select-Object -First 1
#$lastFailed = $depObj | Sort-Object -Property createdAt -Descending | Where-Object {$_.calculatedStatus -eq 3} | Select-Object -First 1
#$results = @()
#if ($queuedRequest) {
# $results += [pscustomobject]@{
# "Environment"=$queuedRequest.environmentName
# "Status"="Queued"
# "Id"=$queuedRequest.id
# "Created"=$queuedRequest.createdAt
# "Message" = $queuedRequest.triggerMessage
# "Is Deployment Failed?"=$queuedRequest.isDeploymentFailed
# "Cancelled?" = $queuedRequest.isCanceled
# }
#}
#if ($inProgress) {
# $results += [pscustomobject]@{
# "Environment"=$inProgress.environmentName
# "Status"="In Progress"
# "Id"=$inProgress.id
# "Created"=$inProgress.createdAt
# "Message" = $inProgress.triggerMessage
# "Is Deployment Failed?"=$inProgress.isDeploymentFailed
# "Cancelled?" = $inProgress.isCanceled
# }
#}
#if ($lastCompleted) {
# $results += [pscustomobject]@{
# "Environment"=$lastCompleted.environmentName
# "Status"="Last Completed"
# "Id"=$lastCompleted.id
# "Created"=$lastCompleted.createdAt
# "Message" = $lastCompleted.triggerMessage
# "Is Deployment Failed?"=$lastCompleted.isDeploymentFailed
# "Cancelled?" = $lastCompleted.isCanceled
# }
#}
#if ($lastFailed) {
# $results += [pscustomobject]@{
# "Environment"=$lastFailed.environmentName
# "Status"="Last Failed"
# "Id"=$lastFailed.id
# "Created"= $lastFailed.createdAt
# "Message" = $lastFailed.triggerMessage
# "Is Deployment Failed?"=$lastFailed.isDeploymentFailed
# "Cancelled?" = $lastFailed.isCanceled
# }
#}
#$results | Sort-Object -Property createdAt -Descending | Show-ListView -Show None
# Optional 2 END
# ==================================
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment