Developers may Sitecore be with you π
I hope you enjoyed Sitecore’s Virtual Developer Day 2022! And if you missed it, don’t worry π You will find all the wonderful recordings on YouTube – Sitecore Virtual Developer Day 2022
Tody’s post will be a “follow up” on the previous post – Run Sitecore CLI within your Kubernetes cluster using self-hosted GitHub actions
The scenario is this, I want to make it seamless for the developers to push the latest Sitecore changes(items) into a running environment(say test). In my previous post, I showed you how you can do this by combining GitHub Actions with Sitecore Cli – Run Sitecore CLI within your Kubernetes cluster using self-hosted GitHub actions. But that means that the developers need to locate the GitHub Action and manually execute it. So how can we make it easier for them? The best would be if they could do it within Sitecore. And guess what, it is possible! We will do this with “the missing piece of the puzzle” – Sitecore Powershell Extensions π
So how do we do this? We have a running self-hosted runner in our Kubernetes cluster to “host” GitHub Actions. And GitHub Actions can be triggered, yes we can trigger a GitHub Action from “the outside”
Workflow triggers are events that cause a workflow to run. These events can be:
Events that occur in your workflow’s repository
Events that occur outside of GitHub and trigger a
repository_dispatch
event on GitHubScheduled times
Manual
Let’s use the GitHub Action from the previous post – Run Sitecore CLI within your Kubernetes cluster using self-hosted GitHub actions). We will change it a little bit. Instead of triggering it manually, it will listen to a dispatch event – repository_dispatch:
You can use the GitHub API to trigger a webhook event called
repository_dispatch
when you want to trigger a workflow for activity that happens outside of GitHub. For more information, see “Create a repository dispatch event.”
Great, now the workflow can be triggered from “the outside”. We also need to give the event a name, “push-sitecore-items”.
name: Run dynamic Sitecore CLI command within kubernetes - Linux
on:
repository_dispatch:
types: [push-sitecore-items]
jobs:
push-content:
name: Push sitecore items
runs-on: [self-hosted, Linux]
steps:
- name: Checkout My very cool repo
uses: actions/checkout@v3
with:
repository: myorganization/myverycoolrepo
token: ${{ secrets.REPO_PAT }}
ref: main
- name: Install dot net
shell: bash
run: |
wget https://packages.microsoft.com/config/ubuntu/21.04/packages-microsoft-prod.deb -O packages-microsoft-prod.deb
sudo dpkg -i packages-microsoft-prod.deb
rm packages-microsoft-prod.deb
sudo apt-get update; \
sudo apt-get install -y apt-transport-https && \
sudo apt-get update && \
sudo apt-get install -y dotnet-sdk-3.1
- name: Install sitecore cli
shell: bash
run: |
dotnet nuget add source https://sitecore.myget.org/F/sc-packages/api/v3/index.json --name "Sitecore-Public-Nuget-Feed"
dotnet tool restore
- name: Login to sitecore
shell: bash
run: |
dotnet sitecore login --client-credentials true --auth http://id --cm http://cm --allow-write true --client-id "Automation ID" --client-secret "schhh_secrets_and_even_more secrets" --insecure
- name: Dynamic Sitecore CLI command
shell: bash
working-directory: ${{ github.workspace }}
run: |
${{github.event.client_payload.SitecoreSerCommand}}
There is one more (important) thing that we have changed in the workflow. Instead of “hard-coding” the Sitecore CLI command, we will send it from Sitecore as a client_payload.
run: |
${{github.event.client_payload.SitecoreSerCommand}}
This means we will have a “dynamic” Sitecore CLI workflow π
Next, is to trigger the workflow from Sitecore. And as I mentioned before, we will use the best Sitecore tool ever – Sitecore Powershell Extensions. I was thinking of adding a button to the Developer Ribbon. Let’s have a look at the great documentation and follow the instructions regarding Ribbon integration. We will have a ribbon button and a dialogue popup(So we can enter a Sitecore CLI command). Here is the end result π

Let’s have a look at the Powershell script, which will be executed when hitting the “OK” button. Here we will trigger the workflow(GitHub Action), with an endpoint to trigger the webhook event repository_dispatch.
$GithubOrganization = $env:GithubOrganization
$GithubRepo = $env:GithubRepo
$GithubToken = $env:GithubToken
$Uri = ('https://api.github.com/repos/{0}/{1}/dispatches' -f $GithubOrganization, $GithubRepo)
$SitecoreSerCommand = "dotnet sitecore ser push --force -i ModuleNamespaceA.YYYY ModuleNamespaceB.XXXX Feature.* Foundation.*"
$inputProps = @{
Parameters=@(
@{Name="SitecoreCliCommand"; Value=$SitecoreSerCommand; Lines=8}
)
Title = "Push sitecore items"
Description = "Enter sitecore (CLI) ser push command"
Width = 800
Height = 300
ShowHints = $true
}
$result = Read-Variable @inputProps
if($result -eq "cancel"){
Write-Host "Not triggering GitHub Action"
exit
}
$Obj = [PSCustomObject]@{
event_type = "push-sitecore-items"
client_payload = [PSCustomObject]@{ SitecoreSerCommand=$SitecoreCliCommand }
}
$Body = $Obj | ConvertTo-JSON
$params = @{
ContentType = 'application/json'
Headers = @{
'authorization' = "token $($GithubToken)"
'accept' = 'application/vnd.github.v3+json'
}
Method = 'Post'
URI = $Uri
Body = $Body
}
Invoke-RestMethod @params -verbose
Write-Host "Triggering GitHub Action"
Let me go through the script.
In order to trigger the workflow, we need the organization, the name of our repo(where the Sitecore items reside), and the Github token(PAT). Notice that they are all environment variables, meaning they will be set in the “CM” K8 spec. Or… If you want to try it out in your local docker environment, just add the variables to your docker-compose file π
$GithubOrganization = $env:GithubOrganization
$GithubRepo = $env:GithubRepo
$GithubToken = $env:GithubToken
Next is to build the endpoint URI:
https://api.github.com/repos/{0}/{1}/dispatches' -f $GithubOrganization, $GithubRepo
Then we create the dialogue popup, I really love this. Sitecore Powershell Extensions is the best π
$SitecoreSerCommand = "dotnet sitecore ser push --force -i ModuleNamespaceA.YYYY ModuleNamespaceB.XXXX Feature.* Foundation.*"
$inputProps = @{
Parameters=@(
@{Name="SitecoreCliCommand"; Value=$SitecoreSerCommand; Lines=8}
)
Title = "Push sitecore items"
Description = "Enter sitecore (CLI) ser push command"
Width = 800
Height = 300
ShowHints = $true
}
Building the Body… The JSON object will have the name of the event and the client_payload, which will hold the Sitecore CLI command:
$Obj = [PSCustomObject]@{
event_type = "push-sitecore-items"
client_payload = [PSCustomObject]@{ SitecoreSerCommand=$SitecoreCliCommand }
}
$Body = $Obj | ConvertTo-JSON
Putting it all together and calling the rest method π
$params = @{
ContentType = 'application/json'
Headers = @{
'authorization' = "token $($GithubToken)"
'accept' = 'application/vnd.github.v3+json'
}
Method = 'Post'
URI = $Uri
Body = $Body
}
Invoke-RestMethod @params -verbose
Write-Host "Triggering GitHub Action"
Now it’s all connected, we can trigger a GitHub Action within Sitecore. And with the client_payload we can dynamically set the Sitecore CLI command in the workflow.
With GitHub Actions running in your Kubernetes cluster gives you a lot of interesting possibilities. You can of course set up a self-hosted runner on a VM. But… running within your Kubernetes cluster, that’s a higher coolness factor π
That’s all for now folks π
One thought on “A powerful combination – Sitecore CLI, GitHub Actions, and Sitecore PowerShell Extensions”