The power of Sitecore CLI and environment variable in XM Cloud Deploy App
While the earlier blog post was aligned towards local Docker setup, this blog post is about putting option-1 from that post to proper use in XM Cloud environment. If you remember the contents of the earlier blog post, one of the main problems was, although the ITEM_CLONING_ENABLED toggle was present in .env file, changing it doesn't mean automatic reflection on the Sitecore config file. You have to manually ensure to down/clean/publish/up etc. to see the end-result in the local Docker instance. That is what is addressed in this blog post. So, the ITEM_CLONING_ENABLED is an environment variable in .env file. In order to put that to use in XM Cloud, the env variable must be created in the XM Cloud deploy app. Once that is created, the real power of XM Cloud comes into play. You then deploy other artifacts like the patch config file and the docker-compose.yml created in the earlier post. Now, just a restart of the environment will start reflecting the patch config change. Even for the XM Cloud environment restart, there is a handy CLI command:
dotnet sitecore cloud environment restart --environment-id $environmentId
That's it. Run the sync script to pull content from source environment to XM Cloud environment, reset the ITEM_CLONING_ENABLED env var to true and just restart. All these accomplished with the power of PowerShell and Sitecore CLI commands.
**************
After writing this blog post, I realized that if there was a command to build and deploy, it would be more useful because after changing an XM Cloud environment variable, restart of the environment alone isn't enough for the value to take effect in showconfig.aspx, instead I've had to build and deploy the XM Cloud environment to see the end-result. So, technically speaking, either restart environment command must cover the build and deploy functionality or a separate build and deploy command is necessary for XM Cloud.
**************
Now, consolidated steps for better clarity:
1. Go to XM Cloud Deploy App
2. You can find your list of projects:
3. Click the project and you will see the list of environments and from there, select the environment to see the list of deployments and in this page, you can switch over to the env variables tab:
4. Now, in this environment, you can create an env variable and provide the same name and value as the local .env file env var and value as follows:
This variable will be used by the environment as soon as the other parts of the code like, docker-compose.yml, patch config are deployed. Note that you don't even need to create this environment in the current scenario since the upsert command will automatically insert the env var in XM Cloud if not present.
5. Now, for sync content script to work fine, the concerned PS script must be able to modify the above XM Cloud deploy environment variable - ITEM_CLONING_ENABLED. In other words, in your local setup, the env variable will be adjusted as part of the init.ps1 script but in this case, can be done as part of a function in the sync content script as follows:
A function that can be invoked before and after sync:
******
******
Upsert command to update an environment variable if present and insert if not present:
dotnet sitecore cloud environment variable upsert -n 'ITEM_CLONING_ENABLED' -val true --target CM --environment-id <env id>
Note that if you don't pass target in the above command, you will get this error in command line so, target is important:
Secret type change is not allowed
A sample sync script:
******
******
Extra titbit:
Get a specific environment variable value:
Convert the env var list to json and access the value by passing the env var name:
******
******
I love the waiting for restart message while the restart command is in action:
End-result of restart prior to execution of sync script:
Comments
Post a Comment