Extreme CI Part 2: Setting up Git for a Personal CI

How I run a personal CI build every time I build my code in Visual Studio.

Summary

This week we setup Git so it commits your changes and pushes them up to the personal CI server. We’ll also create a batch file to automate this process. In part 3 we’ll create the Visual Studio macro that automatically runs the Personal CI process every time you build your code.

The Series

In this series I show you how to take continuous integration to the extreme by building a personal CI server that runs all your tests and adds a commit to source control every time you build in Visual Studio.

Lets get to it.

Create the Personal CI Git Repository

The CI Server will get code changes from a Git repository. It will monitor this repository, and when changes are committed, it will pull them, build the code, and run the tests. Let’s create the repository.

For this example we’ll keep it simple and create the repository in a new folder our local machine. In reality, you may want to put this on another machine.

C:\Projects>md MyProjectPersonalCI.git

C:\Projects>cd MyProjectPersonalCI.git

Since this repository will only be accessed remotely, it doesn’t need a working directory—so we will use the --bare switch when initalizing it to tell Git not to create one.

C:\Projects\MyProjectPersonalCI.git>git --bare init

Initialized empty Git repository in C:\Projects\MyProjectPersonalCI.git\

Add the Personal CI Repository as a Remote

To push changes to the personal CI repository we need to add it as a remote to our local working repository. To avoid conflicts with developers using “origin”, we’ll give it the name “personalci”. How descriptive.

C:\Projects\MyProjectPersonalCI.git>cd ..

C:\Projects>cd MyProject

C:\Projects\MyProject>git remote add personalci C:\Projects\MyProjectPersonalCI.git

C:\Projects\MyProject>git remote

origin

personalci

All we have to do now is push our changes to the personalci remote and they will be visible to the CI server. Let’s do that now (if there is any code in your project to commit).

C:\Projects\MyProject>git push personalci master

Counting objects: 899, done.

Delta compression using up to 2 threads.

Compressing objects: 100% (554/554), done.

Writing objects: 100% (899/899), 13.93 MiB | 3.15 MiB/s, done.

Total 899 (delta 422), reused 691 (delta 332)

To C:\Projects\MyProjectPersonalCI.git

* [new branch] master –> master

Your output may differ depending on how many files you have uncommitted in your project folde. I am adding this to an existing project of mine, so there are quite a few files.

The command git push personalci master is telling Git to push the changes from the master branch on my local repository to the master branch on the personalci remote repository. The remote master branch was automatically created for us since it didn’t already exist.

If you are having trouble understanding what Git is doing here, I encourage you to read the documentation. I don’t want to dive too deep into Git since it’s beyond the scope of this article.

Now we can push our changes up to the CI Server. Lets automate it.

Creating the Batch File

This batch file will do three things:

  1. Add any untracked files.
  2. Commit all changes with a custom commit message.
  3. Push the changes to the personalci remote repository.

Create a file in the root of your git repository called “CommitAndPushToPersonalCI.bat”. Yeah, I know…I’m a sucker for long descriptive names.

@REM CommitAndPushToPersonalCI.bat
@REM This script adds any untracked files and commits them and any changes to git.
@REM It then pushes the git repository changes up to the "personalci" remote repository.
@REM From there, a CI Server should pick up and run the build.

@ECHO on
@ECHO Auto-committing to Git Repository and push to PersonalCI after Successful Build.

@REM Add files that are outstanding to the local git repository.
call git add .
@if errorlevel 1 goto :error

@REM Commit all files to the repository with an autogenerated commit message.
call git commit -a -m "Auto-commit from successful build on %COMPUTERNAME% by %USERNAME% at %TIME% on %DATE%"
@if errorlevel 1 goto :error

@REM Push to the personalci remote repository.
call git push personalci master
@if errorlevel 1 goto :error

@ECHO.
@ECHO SUCCESS: Pushed to Personal CI
@goto :exit

:error
@ECHO.
@ECHO ERROR! -- ErrorLevel: %errorLevel%

:exit

The code batch file is simple and should pretty much explain itself.

Run the batch file and you should get output similar to–but not exactly the same as the following:

image

We have everything ready, all we need now is to get Visual Studio to fire it off when we build.

Extreme CI Part 3: The Visual Studio Macro

-Chris

This entry was posted in .NET, Agile, Continuous Integration, Testing, Visual Studio and tagged , , , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

2 Trackbacks

Post a Comment

Your email is never published nor shared. Required fields are marked *

You may use these HTML tags and attributes <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*
*