Skip to content

Commit 8b642db

Browse files
committed
Merge branch 'develop'
2 parents 19e5b93 + 77e50e8 commit 8b642db

13 files changed

+1055
-7
lines changed
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
param(
2+
[Parameter(Position=0)]
3+
[string]
4+
$ProjectId,
5+
6+
[Parameter(Position=1)]
7+
[string]
8+
$ApiKey,
9+
10+
[Parameter(Position=2)]
11+
[string]
12+
$FilePath,
13+
14+
[Parameter(Position=3)]
15+
[string]
16+
$Description = $null,
17+
18+
[Parameter(Position=4)]
19+
[string]
20+
$Version = $null,
21+
22+
[Parameter(Position=5)]
23+
[string]
24+
$PipelineVendor, ## GITHUB or AZUREDEVOPS
25+
26+
[Parameter(Position=6)]
27+
[string]
28+
$BaseUrl = "https://api.cloud.umbraco.com"
29+
)
30+
31+
### Endpoint docs
32+
# https://docs.umbraco.com/umbraco-cloud/set-up/project-settings/umbraco-cicd/umbracocloudapi/todo-v2
33+
#
34+
$url = "$BaseUrl/v2/projects/$ProjectId/deployments/artifacts"
35+
36+
# test if file is present
37+
if (-not $FilePath) {
38+
Write-Host "FilePath is empty"
39+
exit 1
40+
}
41+
42+
if (-not (Test-Path -Path $FilePath -PathType Leaf)) {
43+
Write-Host "FilePath does not contain a file"
44+
exit 1
45+
}
46+
# end test
47+
48+
$Headers = @{
49+
'accept' = 'application/json'
50+
'Content-Type' = 'multipart/form-data'
51+
'Umbraco-Cloud-Api-Key' = $ApiKey
52+
}
53+
$contentType = 'application/zip'
54+
55+
$multipartContent = [System.Net.Http.MultipartFormDataContent]::new()
56+
57+
$fileStream = [System.IO.FileStream]::new($filePath, [System.IO.FileMode]::Open)
58+
$fileHeader = [System.Net.Http.Headers.ContentDispositionHeaderValue]::new('form-data')
59+
$fileHeader.Name = 'file'
60+
$fileHeader.FileName = Split-Path -leaf $filePath
61+
$fileContent = [System.Net.Http.StreamContent]::new($fileStream)
62+
$fileContent.Headers.ContentDisposition = $fileHeader
63+
$fileContent.Headers.ContentType = [System.Net.Http.Headers.MediaTypeHeaderValue]::Parse($contentType)
64+
65+
$multipartContent.Add($fileContent)
66+
67+
$stringHeader = [System.Net.Http.Headers.ContentDispositionHeaderValue]::new("form-data")
68+
$stringHeader.Name = "description"
69+
$StringContent = [System.Net.Http.StringContent]::new($Description)
70+
$StringContent.Headers.ContentDisposition = $stringHeader
71+
$multipartContent.Add($stringContent)
72+
73+
$stringHeader = [System.Net.Http.Headers.ContentDispositionHeaderValue]::new("form-data")
74+
$stringHeader.Name = "version"
75+
$StringContent = [System.Net.Http.StringContent]::new($Version)
76+
$StringContent.Headers.ContentDisposition = $stringHeader
77+
$multipartContent.Add($stringContent)
78+
79+
80+
try {
81+
$response = Invoke-WebRequest -Body $multipartContent -Headers $Headers -Method 'POST' -Uri $url
82+
if ($response.StatusCode -ne 200)
83+
{
84+
Write-Host "---Response Start---"
85+
Write-Host $response
86+
Write-Host "---Response End---"
87+
Write-Host "Unexpected response - see above"
88+
exit 1
89+
}
90+
91+
$responseJson = $response.Content | ConvertFrom-Json
92+
$artifactId = $responseJson.artifactId
93+
94+
switch ($PipelineVendor) {
95+
"GITHUB" {
96+
"artifactId=$($artifactId)" | Out-File -FilePath $env:GITHUB_OUTPUT -Append
97+
}
98+
"AZUREDEVOPS" {
99+
Write-Host "##vso[task.setvariable variable=artifactId;isOutput=true]$($artifactId)"
100+
}
101+
"TESTRUN" {
102+
Write-Host $PipelineVendor
103+
}
104+
Default {
105+
Write-Host "Please use one of the supported Pipeline Vendors or enhance script to fit your needs"
106+
Write-Host "Currently supported are: GITHUB and AZUREDEVOPS"
107+
Exit 1
108+
}
109+
}
110+
111+
Write-Host "Artifact uploaded - Artifact Id: $($artifactId)"
112+
Write-Host "--- Upload Response ---"
113+
Write-Output $responseJson
114+
115+
exit 0
116+
}
117+
catch
118+
{
119+
Write-Host "---Error---"
120+
Write-Host $_.Exception.Message
121+
if ($null -ne $_.Exception.Response) {
122+
$responseStream = $_.Exception.Response.GetResponseStream()
123+
$reader = New-Object System.IO.StreamReader($responseStream)
124+
$responseBody = $reader.ReadToEnd()
125+
Write-Host "Response Body: $responseBody"
126+
}
127+
exit 1
128+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
param (
2+
[Parameter(Position=0)]
3+
[string]
4+
$PatchFile,
5+
6+
[Parameter(Position=1)]
7+
[string]
8+
$LatestDeploymentId,
9+
10+
[Parameter(Position=3)]
11+
[string]
12+
$PipelineVendor,
13+
14+
[Parameter(Position=3)]
15+
[string]
16+
$GitUserName,
17+
18+
[Parameter(Position=4)]
19+
[string]
20+
$GitUserEmail
21+
)
22+
23+
git config user.name $GitUserName
24+
git config user.email $GitUserEmail
25+
26+
If ($PipelineVendor -eq "AZUREDEVOPS"){
27+
# we need to checkout the specific branch to be able to commit bad to repo in Azure DevOps
28+
git checkout $env:BUILD_SOURCEBRANCHNAME
29+
}
30+
31+
Write-Host "Testing the patch - errors might show up, and that is okay"
32+
Write-Host "=========================================================="
33+
# Check if the patch has been applied already, skip if it has
34+
git apply $PatchFile --reverse --ignore-space-change --ignore-whitespace --check
35+
If ($LASTEXITCODE -eq 0) {
36+
Write-Host "Patch already applied === concluding the apply patch part"
37+
Exit 0
38+
} Else {
39+
Write-Host "Patch not applied yet"
40+
}
41+
42+
Write-Host "Checking if patch can be applied..."
43+
# Check if the patch can be applied
44+
git apply $PatchFile --ignore-space-change --ignore-whitespace --check
45+
If ($LASTEXITCODE -eq 0) {
46+
Write-Host "Patch needed, trying to apply now"
47+
Write-Host "================================="
48+
git apply $PatchFile --ignore-space-change --ignore-whitespace
49+
50+
switch ($PipelineVendor) {
51+
"GITHUB" {
52+
git add *
53+
git commit -m "Adding cloud changes since deployment $LatestDeploymentId [skip ci]"
54+
git push
55+
$updatedSha = git rev-parse HEAD
56+
57+
# Record the new sha for the deploy
58+
"updatedSha=$($updatedSha)" | Out-File -FilePath $env:GITHUB_OUTPUT -Append
59+
}
60+
"AZUREDEVOPS" {
61+
git add --all
62+
git commit -m "Adding cloud changes since deployment $LatestDeploymentId [skip ci]"
63+
git push --set-upstream origin $env:BUILD_SOURCEBRANCHNAME
64+
65+
# Record the new sha for the deploy
66+
$updatedSha = git rev-parse HEAD
67+
Write-Host "##vso[task.setvariable variable=updatedSha;isOutput=true]$($updatedSha)"
68+
}
69+
"TESTRUN" {
70+
Write-Host $PipelineVendor
71+
}
72+
Default {
73+
Write-Host "Please use one of the supported Pipeline Vendors or enhance script to fit your needs"
74+
Write-Host "Currently supported are: GITHUB and AZUREDEVOPS"
75+
Exit 1
76+
}
77+
}
78+
79+
Write-Host "Changes are applied successfully"
80+
Write-Host ""
81+
Write-Host "Updated SHA: $updatedSha"
82+
Exit 0
83+
} Else {
84+
Write-Host ""
85+
Write-Host "Patch cannot be applied - please check the output below for the problematic parts"
86+
Write-Host "================================================================================="
87+
Write-Host ""
88+
git apply -v --reject $PatchFile --ignore-space-change --ignore-whitespace --check
89+
Exit 1
90+
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
# Set variables
2+
param(
3+
[Parameter(Position=0)]
4+
[string]
5+
$ProjectId,
6+
7+
[Parameter(Position=1)]
8+
[string]
9+
$ApiKey,
10+
11+
[Parameter(Position=2)]
12+
[string]
13+
$DeploymentId,
14+
15+
[Parameter(Position=3)]
16+
[string]
17+
$TargetEnvironmentAlias,
18+
19+
[Parameter(Position=4)]
20+
[string]
21+
$DownloadFolder,
22+
23+
[Parameter(Position=5)]
24+
[string]
25+
$PipelineVendor, ## GITHUB or AZUREDEVOPS
26+
27+
[Parameter(Position=6)]
28+
[string]
29+
$BaseUrl = "https://api.cloud.umbraco.com"
30+
)
31+
32+
### Endpoint docs
33+
# https://docs.umbraco.com/umbraco-cloud/set-up/project-settings/umbraco-cicd/umbracocloudapi/todo-v2
34+
#
35+
$ChangeUrl = "$BaseUrl/v2/projects/$ProjectId/deployments/$DeploymentId/diff?targetEnvironmentAlias=$TargetEnvironmentAlias"
36+
37+
$Headers = @{
38+
'Umbraco-Cloud-Api-Key' = $ApiKey
39+
'Content-Type' = 'application/json'
40+
}
41+
42+
if ($GetDiffDeploymentId -eq '') {
43+
Write-Host "I need a DeploymentId of an older deployment to download a git-patch"
44+
Exit 1;
45+
}
46+
47+
# ensure folder exists
48+
if (!(Test-Path $DownloadFolder -PathType Container)) {
49+
Write-Host "Creting folder $($DownloadFolder)"
50+
New-Item -ItemType Directory -Force -Path $DownloadFolder
51+
}
52+
53+
try {
54+
$Response = Invoke-WebRequest -URI $ChangeUrl -Headers $Headers
55+
$StatusCode = $Response.StatusCode
56+
57+
switch ($StatusCode){
58+
"204" {
59+
Write-Host "No Changes - You can continue"
60+
$remoteChanges = "no"
61+
}
62+
"200" {
63+
Write-Host "Changes detected"
64+
$Response | Select-Object -ExpandProperty Content | Out-File "$DownloadFolder/git-patch.diff"
65+
$remoteChanges = "yes"
66+
}
67+
Default {
68+
Write-Host "---Response Start---"
69+
Write-Host $Response
70+
Write-Host "---Response End---"
71+
Write-Host "Unexpected response - see above"
72+
exit 1
73+
}
74+
}
75+
76+
switch ($PipelineVendor) {
77+
"GITHUB" {
78+
"remoteChanges=$($remoteChanges)" | Out-File -FilePath $env:GITHUB_OUTPUT -Append
79+
}
80+
"AZUREDEVOPS" {
81+
Write-Host "##vso[task.setvariable variable=remoteChanges;isOutput=true]$($remoteChanges)"
82+
}
83+
"TESTRUN" {
84+
Write-Host $PipelineVendor
85+
}
86+
Default {
87+
Write-Host "Please use one of the supported Pipeline Vendors or enhance script to fit your needs"
88+
Write-Host "Currently supported are: GITHUB and AZUREDEVOPS"
89+
Exit 1
90+
}
91+
}
92+
Exit 0
93+
}
94+
catch {
95+
Write-Host "---Error---"
96+
Write-Host $_.Exception.Message
97+
if ($null -ne $_.Exception.Response) {
98+
$responseStream = $_.Exception.Response.GetResponseStream()
99+
$reader = New-Object System.IO.StreamReader($responseStream)
100+
$responseBody = $reader.ReadToEnd()
101+
Write-Host "Response Body: $responseBody"
102+
}
103+
exit 1
104+
}

0 commit comments

Comments
 (0)