Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Improvements to Cloud Sync #5

Open
wants to merge 17 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions bash/apply_patch.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#!/bin/bash

# Set required variables
patchFile="$1"
latestDeploymentId="$2"
pipelineVendor="$3"
gitUserName="$4"
gitUserEmail="$5"

git config user.name "$gitUserName"
git config user.email "$gitUserEmail"

if [[ "$pipelineVendor" == "AZUREDEVOPS" ]]; then
# we need to checkout the specific branch to be able to commit bad to repo in Azure DevOps
git checkout ${BUILD_SOURCEBRANCHNAME}
fi

echo "Testing the patch - errors might show up, and that is okay"
echo "=========================================================="
# Check if the patch has been applied already, skip if it has
if git apply "$patchFile" --reverse --ignore-space-change --ignore-whitespace --check; then
echo "Patch already applied === concluding the apply patch part"
exit 0
else
echo "Patch not applied yet"
fi

echo "Checking if patch can be applied..."
# check if the patch can be applied
if git apply "$patchFile" --ignore-space-change --ignore-whitespace --check; then
echo "Patch needed, trying to apply now"
echo "================================="
git apply "$patchFile" --ignore-space-change --ignore-whitespace

## Write the updated Sha to the pipelines variables for use in a later step
if [[ "$pipelineVendor" == "GITHUB" ]]; then
git add *
git commit -m "Adding cloud changes since deployment $latestDeploymentId [skip ci]"
git push

# record the new sha for the deploy
updatedSha=$(git rev-parse HEAD)
echo "updatedSha=$updatedSha" >> "$GITHUB_OUTPUT"

elif [[ "$pipelineVendor" == "AZUREDEVOPS" ]]; then
git add --all
git commit -m "Adding cloud changes since deployment $latestDeploymentId [skip ci]"
git push --set-upstream origin ${BUILD_SOURCEBRANCHNAME}

# Record the new sha for the deploy
updatedSha=$(git rev-parse HEAD)
echo "##vso[task.setvariable variable=updatedSha;isOutput=true]$updatedSha"

elif [[ "$pipelineVendor" == "TESTRUN" ]]; then
echo $pipelineVendor

else
echo "Please use one of the supported Pipeline Vendors or enhance script to fit your needs"
echo "Currently supported are: GITHUB and AZUREDEVOPS"
Exit 1
fi
echo "Changes are applied successfully"
echo ""
echo "Updated SHA: $updatedSha"
exit 0

# Handle the case where the patch cannot be applied
else
echo ""
echo "Patch cannot be applied - please check the output below for the problematic parts"
echo "================================================================================="
echo ""
git apply -v --reject "$patchFile" --ignore-space-change --ignore-whitespace --check
exit 1
fi
7 changes: 6 additions & 1 deletion bash/azuredevops/azure-release-pipeline.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ variables:
- name: MajorVersion
value: 0
- name: MinorVersion
value: 5
value: 0
- name: BuildConfiguration
value: 'Release'
- name: projectId
Expand Down Expand Up @@ -35,9 +35,14 @@ stages:
# Package and Deploy to Umbraco Cloud
- stage: CloudDeploymentStage
displayName: Deploy To Cloud
dependsOn: cloudSyncStage
condition: in(dependencies.cloudSyncStage.result, 'Succeeded', 'Skipped')
variables:
newCommitSha: $[ stageDependencies.cloudSyncStage.ApplyRemoteChanges.outputs['cloudGitApplyStep.updatedSha'] ]
jobs:
- template: cloud-deployment.yml
parameters:
projectId: $(projectId)
umbracoCloudApiKey: $(umbracoCloudApiKey)
newCommitSha: $(newCommitSha)

22 changes: 21 additions & 1 deletion bash/azuredevops/cloud-deployment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,34 @@ parameters:
- name: projectId
type: string
- name: umbracoCloudApiKey
- name: newCommitSha
type: string
default: ''

jobs:
- job: prepareDeployment
displayName: Prepare Deployment to cloud
steps:
- checkout: 'self'
persistCredentials: true
fetchDepth: 2
displayName: Check out

- script: |
if [ -n "$(newCommitSha)" ]; then
echo "Including the latest incoming changes"
git fetch
git checkout $(newCommitSha)
fi
env:
newCommitSha: ${{ parameters.newCommitSha }}
displayName: 'Checking out updated repo'

# switch the gitignore files so cloud does not ignore the build frontend assets
- script: cp cloud.gitignore .gitignore
displayName: Prepare Cloud Git Ignore


# zip everything, except what is defined in the 'cloud.zipignore'
- script: zip -r sources.zip . [email protected]
displayName: Zip Source Code

Expand Down
28 changes: 16 additions & 12 deletions bash/azuredevops/cloud-sync.yml
Original file line number Diff line number Diff line change
Expand Up @@ -86,19 +86,23 @@ jobs:
artifactName: PatchFile
targetPath: $(Pipeline.Workspace)/patch
condition: eq(variables.remoteChangesValue, 'yes')

# Using plain git to try an push changes back to local repo
# Depending on your setup you may need to change settings and permissions to better fit your needs
# --ignore-space-change --ignore-whitespace on the git apply allows for minor changes due to OS environment.
- pwsh: |
Write-Host "hello branch: $($env:BUILD_SOURCEBRANCHNAME)"
git checkout $env:BUILD_SOURCEBRANCHNAME
git config --global user.name "Azure Pipeline"
git config --global user.email "[email protected]"
git apply -v $(gitPatchFile) --ignore-space-change --ignore-whitespace
git add --all
git commit -m "Adding cloud changes since deployment $(latestDeploymentId) [skip ci]"
git push --set-upstream origin $env:BUILD_SOURCEBRANCHNAME
# This targets the same branch as the pipeline was triggered on.

## You can change the "Azure Pipeline" and "[email protected]" to whatever you want, this will show up in you git history on
## changes coming from Umbraco Cloud
- task: Bash@3
displayName: Applying git patch to branch
workingDirectory: $(System.DefaultWorkingDirectory)
name: cloudGitApplyStep
condition: eq(variables.remoteChangesValue, 'yes')
inputs:
targetType: 'filePath'
filePath: devops/scripts/apply_patch.sh
arguments: >
$(gitPatchFile)
$(latestDeploymentId)
$(pipelineVendor)
"Azure Pipeline"
"[email protected]"
53 changes: 30 additions & 23 deletions bash/github/cloud-deployment.yml
Original file line number Diff line number Diff line change
@@ -1,23 +1,31 @@
name: Deploy To Cloud

on:
workflow_dispatch:
inputs:
newSha:
required: false
type: string

workflow_call:
inputs:
newSha:
required: false
type: string
secrets:
projectId:
PROJECT_ID:
required: true
umbracoCloudApiKey:
UMBRACO_CLOUD_API_KEY:
required: true

env:
projectId: ${{ secrets.projectId }}
umbracoCloudApiKey: ${{ secrets.umbracoCloudApiKey }}

jobs:
prepareDeployment:
name: Prepare Deployment to cloud
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
ref: ${{ inputs.newSha }}

# switch the gitignore files so cloud does not ignore the build frontend assets
- name: Prepare Cloud Git Ignore
Expand All @@ -35,20 +43,20 @@ jobs:
name: source-artifact
path: ${{GITHUB.WORKSPACE}}/sources.zip
retention-days: 1

callUmbracoApi:
name: Upload Source And Start Deployment
runs-on: ubuntu-latest
needs: prepareDeployment
outputs:
outputs:
runningDeploymentId: ${{ steps.deployment-meta.outputs.deploymentId }}
steps:
- uses: actions/checkout@v4

- name: Get package for upload
uses: actions/download-artifact@v4
with:
name: source-artifact
name: source-artifact

# Request to prepare a deployment
# - sets the commit message to be used in cloud
Expand All @@ -58,8 +66,8 @@ jobs:
shell: bash
run: >
bash ${{GITHUB.WORKSPACE}}/.github/scripts/create_deployment.sh
${{ env.projectId }}
${{ env.umbracoCloudApiKey }}
${{ secrets.PROJECT_ID }}
${{ secrets.UMBRACO_CLOUD_API_KEY }}
"Run number ${{github.run_number}}"
GITHUB

Expand All @@ -68,35 +76,34 @@ jobs:
shell: bash
run: >
bash ${{GITHUB.WORKSPACE}}/.github/scripts/upload_package.sh
${{ env.projectId }}
${{ secrets.PROJECT_ID }}
${{ steps.deployment-meta.outputs.deploymentId }}
${{ env.umbracoCloudApiKey }}
${{ secrets.UMBRACO_CLOUD_API_KEY }}
${{ GITHUB.WORKSPACE }}/sources.zip

# Actually request to start the deployment process in cloud
- name: Request Start Deployment
shell: bash
run: >
bash ${{GITHUB.WORKSPACE}}/.github/scripts/start_deployment.sh
${{ env.projectId }}
${{ secrets.PROJECT_ID }}
${{ steps.deployment-meta.outputs.deploymentId }}
${{ env.umbracoCloudApiKey }}
${{ secrets.UMBRACO_CLOUD_API_KEY }}

awaitDeploymentFinished:
name: Await deployment to finish
runs-on: ubuntu-latest
needs: callUmbracoApi
steps:
- uses: actions/checkout@v4

# Poll until deployment finishes
- name: Wait for deployment completed
# Poll until deployment finishes
- name: Wait for deployment completed
shell: bash
env:
env:
runningDeploymentId: ${{ needs.callUmbracoApi.outputs.runningDeploymentId }}
run: >
bash ${{GITHUB.WORKSPACE}}/.github/scripts/get_deployment_status.sh
${{ env.projectId }}
${{ secrets.PROJECT_ID }}
${{ env.runningDeploymentId }}
${{ env.umbracoCloudApiKey }}

${{ secrets.UMBRACO_CLOUD_API_KEY }}
50 changes: 31 additions & 19 deletions bash/github/cloud-sync.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@ name: Umbraco Cloud Sync

on:
workflow_call:
outputs:
newSha:
description: 'The new SHA hash if there was a patch'
value: ${{ jobs.applyRemoteChanges.outputs.newSha }}
secrets:
projectId:
projectId:
required: true
umbracoCloudApiKey:
required: true

env:
projectId: ${{ secrets.projectId }}
umbracoCloudApiKey: ${{ secrets.umbracoCloudApiKey }}
Expand All @@ -18,7 +22,7 @@ jobs:
runs-on: ubuntu-latest
steps:
# Gets the latest CICD Flow deployment if there is any
# Will write "latestDeploymentId" to pipeline variables, value can be an uuid or empty string
# Will write "latestDeploymentId" to pipeline variables, value can be an uuid or empty string
- uses: actions/checkout@v4
- name: Get Latest Deployment
id: latest-deployment
Expand All @@ -28,9 +32,9 @@ jobs:
${{ env.projectId }}
${{ env.umbracoCloudApiKey }}
GITHUB
outputs:
latestDeployemntId: ${{ steps.latest-deployment.outputs.latestDeploymentId }}

outputs:
latestDeploymentId: ${{ steps.latest-deployment.outputs.latestDeploymentId }}

checkForChanges:
name: Check if there are changes since latest deployment
Expand All @@ -43,7 +47,7 @@ jobs:
# When "remoteChanges" is yes, there will also be downloaded a patch-file to the path you specified in -DownloadFolder parameter
- name: Fetch Changes From Cloud
env:
latestDeploymentId: ${{ needs.preflight.outputs.latestDeployemntId }}
latestDeploymentId: ${{ needs.preflight.outputs.latestDeploymentId }}
if: ${{ env.latestDeploymentId != '' }}
id: latest-changes
shell: bash
Expand All @@ -68,7 +72,7 @@ jobs:
path: ${{GITHUB.WORKSPACE}}/patch/git-patch.diff
retention-days: 1
outputs:
remoteChanges: ${{ steps.latest-changes.outputs.remoteChanges }}
remoteChanges: ${{ steps.latest-changes.outputs.remoteChanges }}

applyRemoteChanges:
name: Apply remote changes
Expand All @@ -90,21 +94,29 @@ jobs:
with:
name: git-patch
path: ${{GITHUB.WORKSPACE}}/patch

# Using plain git to try an push changes back to local repo
# Depending on your setup you may need to change settings and permissions to better fit your needs
# This targets the same branch as the pipeline was triggered on.
# Stopping a new pipeline run by using the "[skip ci]" as part of commit message
# --ignore-space-change --ignore-whitespace on the git apply allows for minor changes due to OS environment.

## You can change the gitName and gitEmail to whatever you want, this will show up in you git history on
## changes coming from Umbraco Cloud

- name: Applying git patch to branch
id: update-bundle
env:
remoteChanges: ${{ needs.checkForChanges.outputs.remoteChanges }}
latestDeploymentId: ${{ needs.preflight.outputs.latestDeployemntId }}
latestDeploymentId: ${{ needs.preflight.outputs.latestDeploymentId }}
gitName: github-actions
gitEmail: [email protected]
if: ${{ env.remoteChanges == 'yes' }}
run: |
git config user.name github-actions
git config user.email [email protected]
git apply ${{GITHUB.WORKSPACE}}/patch/git-patch.diff --ignore-space-change --ignore-whitespace
git add *
git commit -m "Adding cloud changes since deployment ${{ env.latestDeploymentId }} [skip ci]"
git push
shell: bash
run: >
bash ${{GITHUB.WORKSPACE}}/.github/scripts/apply_patch.sh
${{ GITHUB.WORKSPACE }}/patch/git-patch.diff
${{ env.latestDeploymentId }}
GITHUB
${{ env.gitName }}
${{ env.gitEmail }}
outputs:
newSha: ${{ steps.update-bundle.outputs.updatedSha }}
Loading