On Wednesday I presented at the Sitecore Virtual User Group and demonstrated the CI/CD solution I blogged about in November. I wanted to add an update, to what I presented and blogged about already, which solves a problem that’s cropped up with the process :)!
One of the key parts to the whole process is moving the baseline Git tag each time a successful build and deploy happens. Originally we were using the “VCS Labelling” options provided by TeamCity, what we didn’t realise was that the tag moved the first time it was run and then never moved subsequent to that initial run! A little look through the documentation of TeamCity we found that TeamCity only supports the moving of tags with CVS, quite frustrating I must say.
The solution seemed simple and that was to create a build step which would call the following two Git commands;
- git tag -f %Tag%
- git push -v -f –tags
%Tag% is replaced with the name of the tag, -v just gives us some more verbose output and finally -f is required because we have to do a force push (just like Star Wars)! So at this point it all seemed quite straight forward and simple, I’m sure you’re seeing what’s coming next. Not it was not simple! In a nutshell we found we actually had to solve the following problems.
Using the VCS checkout mode “Automatically on server” means you can’t actually run Git commands because the .git folder is removed. This meant we had to switch to using the VCS checkout mode “Automatically on agent”, which in turn brought up a new issue!
Switching to checking out on the agent means you lose a number of features regarding checkout rules. Part of the process is to check out the baseline commit and move the serialised Sitecore item files into a separate folder, ready to be compared. Unfortunately when checking out on agent you only have one option and that is to move the entire checked out files into a different directory. Previously we were excluding all the files we didn’t need and just moving the ones we did. So we ended up with the following checkout rule (you can only have one as well by the way ;)).
- +:.=>./%Baseline Folder Path%
At this point we thought we had it nailed, of course we didn’t! For some reason when we were running our new build step it would run the tag command fine but just hang, without any messages, on the push command. We realised it was probably “hanging” because it was waiting for some kind of user input. With a bit of digging around on the internet we found we were right and that it was waiting for a yes/no confirmation to accept the host we were trying to connect to. The reason for this is that the build agent service doesn’t use the same look for the SSH information in the same place as the build server service. To solve this issue you’ll need copy the SSH keys and create a config file in the user profile folder of the user the build agent service is running under. For most the build agent service is probably running as Local System so you’ll need to locate the correct systemprofile folder (this serverfault post was quite useful). If you’re running 64 bit then you may need to look in the SysWOW64 folder instead of System32. You should know you’re in the right place because you’ll probably find an empty .ssh folder. Copy in any SSH keys you need and then create a file named “config” (no extension), then you’ll need to add in the following (replace <hostname>, e.g. bitbucket.org).
I’ll be honest, the solution is not as elegant as the original (although that wasn’t really a solution I suppose) but it does work and at least putting this in place does also mean you can run any kind of Git command. I’d highly recommend looking into the Meta Runner feature TeamCity offers, we’ve created a “Git: Move Tag” runner which we can select in a build step. It means we can just pass in the tag name as a parameter rather than writing out the Git commands in every build configuration :). Meta Runners really are a powerful feature of TeamCity, we have other ones where we’re using a command line runner (Sitecore Courier for example).
Hopefully this will help in case you were trying to use the VCS labelling options to move a tag or even if you just want to run Git commands as a build step.