diff --git a/bash/create_deployment.sh b/bash/create_deployment.sh index bf773bf..47db3ad 100644 --- a/bash/create_deployment.sh +++ b/bash/create_deployment.sh @@ -25,6 +25,7 @@ function call_api { -H "Umbraco-Cloud-Api-Key: $apiKey" \ -H "Content-Type: application/json" \ -d "{\"commitMessage\":\"$commitMessage\"}") + responseCode=${response: -3} content=${response%???} @@ -36,12 +37,20 @@ function call_api { return fi fi - + ## Let errors bubble forward - echo "Unexpected API Response Code: $responseCode" - echo "---Response Start---" - echo $content - echo "---Response End---" + errorResponse=$content + echo "Unexpected API Response Code: $responseCode - More details below" + # Check if the input is valid JSON + echo "$errorResponse" | jq . > /dev/null 2>&1 + if [ $? -ne 0 ]; then + echo "--- Response RAW ---\n" + echo $errorResponse + else + echo "--- Response JSON formatted ---\n" + echo $errorResponse | jq . + fi + echo "\n---Response End---" exit 1 } diff --git a/bash/get_changes_by_id.sh b/bash/get_changes_by_id.sh index bc63ff6..1c2900d 100644 --- a/bash/get_changes_by_id.sh +++ b/bash/get_changes_by_id.sh @@ -45,10 +45,20 @@ function get_changes { remoteChanges="yes" return fi - echo "---Response Start---" - echo $filePath - echo -e "\n---Response End---" - echo "Unexpected response - see above" + + ## Let errors bubble forward + errorResponse=$filePath + echo "Unexpected API Response Code: $responseCode - More details below" + # Check if the input is valid JSON + cat "$errorResponse" | jq . > /dev/null 2>&1 + if [ $? -ne 0 ]; then + echo "--- Response RAW ---" + cat "$errorResponse" + else + echo "--- Response JSON formatted ---" + cat "$errorResponse" | jq . + fi + echo "---Response End---" exit 1 } diff --git a/bash/get_deployment_status.sh b/bash/get_deployment_status.sh index 9fb510d..45b77f1 100644 --- a/bash/get_deployment_status.sh +++ b/bash/get_deployment_status.sh @@ -41,10 +41,18 @@ function call_api { fi ## Let errors bubble forward - echo "Unexpected API Response Code: $responseCode" - echo "---Response Start---" - echo $content - echo "---Response End---" + errorResponse=$content + echo "Unexpected API Response Code: $responseCode - More details below" + # Check if the input is valid JSON + echo "$errorResponse" | jq . > /dev/null 2>&1 + if [ $? -ne 0 ]; then + echo "--- Response RAW ---\n" + echo $errorResponse + else + echo "--- Response JSON formatted ---\n" + echo $errorResponse | jq . + fi + echo "\n---Response End---" exit 1 } diff --git a/bash/get_latest_deployment.sh b/bash/get_latest_deployment.sh index a91c578..5e4f163 100644 --- a/bash/get_latest_deployment.sh +++ b/bash/get_latest_deployment.sh @@ -48,12 +48,20 @@ function call_api { return fi - ## Let errors bubble forward - echo "Unexpected API Response Code: $responseCode" - echo "---Response Start---" - echo $response - echo "---Response End---" + ## Let errors bubble forward + errorResponse=$response + echo "Unexpected API Response Code: $responseCode - More details below" + # Check if the input is valid JSON + echo "$errorResponse" | jq . > /dev/null 2>&1 + if [ $? -ne 0 ]; then + echo "--- Response RAW ---\n" + echo $errorResponse + else + echo "--- Response JSON formatted ---\n" + echo $errorResponse | jq . + fi + echo "\n---Response End---" exit 1 } diff --git a/bash/start_deployment.sh b/bash/start_deployment.sh index 83c9700..5e540e7 100644 --- a/bash/start_deployment.sh +++ b/bash/start_deployment.sh @@ -36,11 +36,20 @@ function call_api { fi ## Let errors bubble forward - echo "Unexpected API Response Code: $responseCode" - echo "---Response Start---" - echo $content - echo "---Response End---" + errorResponse=$content + echo "Unexpected API Response Code: $responseCode - More details below" + # Check if the input is valid JSON + echo "$errorResponse" | jq . > /dev/null 2>&1 + if [ $? -ne 0 ]; then + echo "--- Response RAW ---\n" + echo $errorResponse + else + echo "--- Response JSON formatted ---\n" + echo $errorResponse | jq . + fi + echo "\n---Response End---" exit 1 + } call_api diff --git a/bash/upload_package.sh b/bash/upload_package.sh index fe8d852..1f335a3 100644 --- a/bash/upload_package.sh +++ b/bash/upload_package.sh @@ -13,6 +13,16 @@ if [[ -z "$baseUrl" ]]; then baseUrl="https://api.cloud.umbraco.com" fi +if [[ -z "$filePath" ]]; then + echo "filePath is empty" + exit 1 +fi + +if [[ ! -f "$filePath" ]]; then + echo "filePath does not contain a file" + exit 1 +fi + ### Endpoint docs # https://docs.umbraco.com/umbraco-cloud/set-up/project-settings/umbraco-cicd/umbracocloudapi#upload-zip-source-file # @@ -32,10 +42,18 @@ function call_api { fi ## Let errors bubble forward - echo "Unexpected API Response Code: $responseCode" - echo "---Response Start---" - echo $content - echo "---Response End---" + errorResponse=$content + echo "Unexpected API Response Code: $responseCode - More details below" + # Check if the input is valid JSON + echo "$errorResponse" | jq . > /dev/null 2>&1 + if [ $? -ne 0 ]; then + echo "--- Response RAW ---\n" + echo $errorResponse + else + echo "--- Response JSON formatted ---\n" + echo $errorResponse | jq . + fi + echo "\n---Response End---" exit 1 } diff --git a/powershell/Add-DeploymentPackage.ps1 b/powershell/Add-DeploymentPackage.ps1 index e312746..5086829 100644 --- a/powershell/Add-DeploymentPackage.ps1 +++ b/powershell/Add-DeploymentPackage.ps1 @@ -6,7 +6,7 @@ param( [Parameter(Position=1)] [string] $DeploymentId, - + [Parameter(Position=2)] [string] $ApiKey, @@ -20,11 +20,24 @@ param( $BaseUrl = "https://api.cloud.umbraco.com" ) + ### Endpoint docs # https://docs.umbraco.com/umbraco-cloud/set-up/project-settings/umbraco-cicd/umbracocloudapi#upload-zip-source-file # $url = "$BaseUrl/v1/projects/$ProjectId/deployments/$DeploymentId/package" +# test if file is present +if (-not $filePath) { + Write-Host "filePath is empty" + exit 1 +} + +if (-not (Test-Path -Path $filePath -PathType Leaf)) { + Write-Host "filePath does not contain a file" + exit 1 +} +# end test + $fieldName = 'file' $contentType = 'application/zip' $umbracoHeader = @{ 'Umbraco-Cloud-Api-Key' = $ApiKey } @@ -58,6 +71,12 @@ try { catch { Write-Host "---Error---" - Write-Host $_ + Write-Host $_.Exception.Message + if ($_.Exception.Response -ne $null) { + $responseStream = $_.Exception.Response.GetResponseStream() + $reader = New-Object System.IO.StreamReader($responseStream) + $responseBody = $reader.ReadToEnd() + Write-Host "Response Body: $responseBody" + } exit 1 } \ No newline at end of file diff --git a/powershell/Get-ChangesById.ps1 b/powershell/Get-ChangesById.ps1 index c132fff..a68191d 100644 --- a/powershell/Get-ChangesById.ps1 +++ b/powershell/Get-ChangesById.ps1 @@ -89,6 +89,12 @@ try { } catch { Write-Host "---Error---" - Write-Host $_ + Write-Host $_.Exception.Message + if ($_.Exception.Response -ne $null) { + $responseStream = $_.Exception.Response.GetResponseStream() + $reader = New-Object System.IO.StreamReader($responseStream) + $responseBody = $reader.ReadToEnd() + Write-Host "Response Body: $responseBody" + } exit 1 } \ No newline at end of file diff --git a/powershell/Get-LatestDeployment.ps1 b/powershell/Get-LatestDeployment.ps1 index 31e59d5..7764a66 100644 --- a/powershell/Get-LatestDeployment.ps1 +++ b/powershell/Get-LatestDeployment.ps1 @@ -81,6 +81,12 @@ try{ } catch { Write-Host "---Error---" - Write-Host $_ + Write-Host $_.Exception.Message + if ($_.Exception.Response -ne $null) { + $responseStream = $_.Exception.Response.GetResponseStream() + $reader = New-Object System.IO.StreamReader($responseStream) + $responseBody = $reader.ReadToEnd() + Write-Host "Response Body: $responseBody" + } exit 1 } \ No newline at end of file diff --git a/powershell/New-Deployment.ps1 b/powershell/New-Deployment.ps1 index d6e0991..d000c7f 100644 --- a/powershell/New-Deployment.ps1 +++ b/powershell/New-Deployment.ps1 @@ -74,6 +74,12 @@ try { } catch { Write-Host "---Error---" - Write-Host $_ + Write-Host $_.Exception.Message + if ($_.Exception.Response -ne $null) { + $responseStream = $_.Exception.Response.GetResponseStream() + $reader = New-Object System.IO.StreamReader($responseStream) + $responseBody = $reader.ReadToEnd() + Write-Host "Response Body: $responseBody" + } exit 1 } diff --git a/powershell/Start-Deployment.ps1 b/powershell/Start-Deployment.ps1 index 80fc675..05a163a 100644 --- a/powershell/Start-Deployment.ps1 +++ b/powershell/Start-Deployment.ps1 @@ -48,6 +48,12 @@ try { } catch { Write-Host "---Error---" - Write-Host $_ + Write-Host $_.Exception.Message + if ($_.Exception.Response -ne $null) { + $responseStream = $_.Exception.Response.GetResponseStream() + $reader = New-Object System.IO.StreamReader($responseStream) + $responseBody = $reader.ReadToEnd() + Write-Host "Response Body: $responseBody" + } exit 1 } \ No newline at end of file diff --git a/powershell/Test-DeploymentStatus.ps1 b/powershell/Test-DeploymentStatus.ps1 index 21387a0..b48f128 100644 --- a/powershell/Test-DeploymentStatus.ps1 +++ b/powershell/Test-DeploymentStatus.ps1 @@ -47,7 +47,13 @@ function Request-Deployment-Status ([INT]$run){ catch { Write-Host "---Error---" - Write-Host $_ + Write-Host $_.Exception.Message + if ($_.Exception.Response -ne $null) { + $responseStream = $_.Exception.Response.GetResponseStream() + $reader = New-Object System.IO.StreamReader($responseStream) + $responseBody = $reader.ReadToEnd() + Write-Host "Response Body: $responseBody" + } exit 1 } }