CI/CD with TeamCity + Sitecore 9.2 + Unicorn
In this post, I will tell you how to setup CI/CD for Sitecore 9.2 with TeamCity.
You can notice that the target framework for Unicorn packages are .NET Framework 4.7.2 which I use for my Sitecore 9.2 solution.
Assuming that you have already known how to use Unicorn, the only place that you need to change to enable automatic deployment of Sitecore items is in Unicorn.Configs.Default.config
By default, the value is false, you will need to update the above value to true. However I would not want to touch the default config file. I will create a transformed configuration file and update this value. I prefer using profile configuration file but you can use build/custom configuration file if you like.
Here I create a profile called DevProfile in Visual Studio. Note this value as it will be used for the build process later:
Then I create a transformed configuration file Unicorn.Configs.Default.DevProfile.config and update the value to true:
The "Command line parameters" field is where you supply the various parameters for the build. I am going to explain them one by one:
Log into Sitecore admin of your website in the server. You should see your Sitecore items that you committed to source control earlier there.
Solution Update
Prerequisites:
- Sitecore 9.2 project/solution created
Firstly you would want to be able to synchronize Sitecore items between environments. I choose to use Unicorn which can be installed to your solution via Nuget. The version that I am using are 4.1.1. You will need to install both Unicorn & Unicorn Core packages:- Sitecore 9.2 project/solution created
You can notice that the target framework for Unicorn packages are .NET Framework 4.7.2 which I use for my Sitecore 9.2 solution.
Assuming that you have already known how to use Unicorn, the only place that you need to change to enable automatic deployment of Sitecore items is in Unicorn.Configs.Default.config
By default, the value is false, you will need to update the above value to true. However I would not want to touch the default config file. I will create a transformed configuration file and update this value. I prefer using profile configuration file but you can use build/custom configuration file if you like.
Here I create a profile called DevProfile in Visual Studio. Note this value as it will be used for the build process later:
Then I create a transformed configuration file Unicorn.Configs.Default.DevProfile.config and update the value to true:
Now commit the changes to source control, the solution codes are "CI/CD-ready". Try synchronizing a few items from your local Sitecore website and commit them to source control as well for testing purpose.
TeamCity Setup
Prerequisites:
- Setup a new project that connect to a source control hosted the solution above
- Create a new build configuration
- Setup a new project that connect to a source control hosted the solution above
- Create a new build configuration
There are 3 build steps required to be added to the build configuration which are:
- Restore Nuget packages
- Build solution
- Deploy solution
If you want only CI process, ignore the last step.
1. Restore Nuget packages
This step is very straight-forward. Choose the "Nuget Installer" runner type, leave the "Nuget.exe" version to default (mine is 4.8.1 but I believe other versions will work fine) and browse for your solution file in "Path to solution file".
2. Build solution
This step is also straightforward. Choose the "Visual Studio (sln)" runner type, browse for your solution file in "Solution file path", choose your Visual Studio version in "Visual Studio" and enter "Targets" value as "Rebuild" to make sure the solution is rebuilt completely.
3. Delete existing Unicorn files
Because Unicorn files might be moved around or deleted when you commit your changes. They will need to be physically deleted from disk for a fresh deployment. I will add a "Powershell" runner to do that task:
4. Deploy solution
This is the most important step as it will deploy your solution codes as well as Sitecore items to the destination server.
Choose the "MSBuild" runner type. Browse for your solution file in "Build file path". Choose the appropriate MS Build Tools version in your TeamCity server and the platform to run.
The "Command line parameters" field is where you supply the various parameters for the build. I am going to explain them one by one:
/p:DeployOnBuild=True
This is to ensure the deployment happen after build
/p:WebPublishMethod=FileSystem
This is the method to deploy, I choose FileSystem because my TeamCity server is installed in the same server as the web server
/p:publishUrl=path_to_my_website
The physical path to my website
/p:PublishProvider=FileSystem
The publish provider value which is FileSystem
/p:PublishProfile=DevProfile
The profile that I created in Visual Studio earlier. Using this profile, the file Unicorn.Configs.Default.DevProfile.config will be translated back to Unicorn.Configs.Default.config with the automatic synchronization option set to true
Trigger the build
Once the build configuration above is saved, trigger a build and I hope you will see this :)
Log into Sitecore admin of your website in the server. You should see your Sitecore items that you committed to source control earlier there.
Final thoughts
Transparent sync is a really cool feature of Unicorn. It is only available from Unicorn 3. Should we use it for production? Well, check out Kam's article here and decide for yourself ;)
Happy Sitecoring!
This comment has been removed by the author.
ReplyDelete