Search code examples
gitgithubmsbuildcruisecontrol.netmsbuild-task

Getting the Git Long Hash from MSBuild


I am using Git + MSBuild. I need to get the Git Long Hash number from MSBuild so that I can tie a build to the hash number (and find out what files made up that "revision")

Is there a built in task in MSBuild I can use to get the hash number?

Any help is appreciated. Thanks.


Solution

  • I'd recommend passing it as property from CI sever itself whose job it is to deal with VCS, that way devs can use the same script against ordinary folder that isn't a git repo, e.g. a dumb copy/paste for hacking around by providing it from CLI. That said, there might be proper api-based community task but I did it with:

    <Target Name="Foo">
        <Exec Command="git rev-parse HEAD > head" />
        <ReadLinesFromFile File="head">
            <Output TaskParameter="Lines" PropertyName="Head" /> 
        </ReadLinesFromFile>
        <Delete Files="head" />
        <Message Text="Head: $(Head)" />
    </Target>
    
    <Target Name="Bar">
        <PropertyGroup>
            <Head>$([System.IO.File]::ReadAllText(".git\refs\heads\master").Trim())</Head>
        </PropertyGroup>
        <Message Text="Head: $(Head)" />
    </Target>