CircleCI CLI on Windows

Yesterday I was setting up a continuous integration for my recent project https://github.com/veton/dotScript, and at two of the most popular tools supporting GitHub: Travis CI and CircleCI.
Travis is the popular out there, but I really liked the idea behind CircleCI of running builds witin containers, and I jumped into an opportunity to play with contriners so.
If you want to test your build scripts before pushing to git (which you probably do), CircleCI provides a command line tooling. Installation process is well described at https://circleci.com/docs/2.0/local-jobs/, the problem is that CLI a bash script, and I am a Windows fan (sorry if it hurts somebody's feelings).
The first and the most obvious solution is to run the script from Bash on Windows. I tried that and ended up spending hours on solving different kind of issues:
  • Getting comfortable with vim (which may itsel kill any motivation to proceed)
  • Sorting permission issues
  • Creating aliases for docker and for circleci. To use aliases within script, you need to run it through source, which exits your current session afterwards, so you should start new batch, create aliases there, and you end up with a lot of nested srings and escape chars in your aliases
  • Docker Interactive TTY not working in Bash for Windows
And finally I run into a showstopper: mounting folders from Bash to Docker, which simply refuses to work properly. It will either mount an empty folder or just show few files/folders out of tens. There is a long thread on Docker forums about the issue if you're looking for more details.
While figthing all those issues, I got familiar with circleci sh script a bit, and realized all I need from there is just a last command of running circleci/picard docker image and calling circleci script hosted within the image.
So I completely changed the course and just created a simple cmd script which runs the circleci docker image instead:
@ECHO OFF
SET ProjectDir=//%CD%
SET ProjectDir=%ProjectDir:\=/%
SET ProjectDir=%ProjectDir::=%

docker run -it --rm ^
 -v /var/run/docker.sock:/var/run/docker.sock ^
 -v %ProjectDir%:/root/project ^
 --workdir /root/project ^
 circleci/picard ^
 circleci ^
  -v %ProjectDir%:/tmp/_circleci_local_build_repo ^
   %*

Just save it as circleci.cmd in a folder with your favorite scripts (that is obviously added to PATH), and assuming that you docker is set up properly, you will now be able to use the CLI the same way you would do with the original batch, e.g.
circleci config validate -c .circleci/config.yml
Please notice script only mounts current directory to docker container, so make sure you have circleci config underneath.

Comments