|
| 1 | +# MSBuild Task |
| 2 | + |
| 3 | +Available on [Nuget](https://www.nuget.org) under [GitVersionTask](https://www.nuget.org/packages/GitVersionTask/) |
| 4 | + |
| 5 | + Install-Package GitVersionTask |
| 6 | + |
| 7 | +The MSBuild task will wire GitVersion into the MSBuild pipeline of a project and perform several actions. |
| 8 | + |
| 9 | +## Inject version metadata into the assembly |
| 10 | + |
| 11 | +Task Name: `GitVersionTask.UpdateAssemblyInfo` |
| 12 | + |
| 13 | +### AssemblyInfo Attributes |
| 14 | + |
| 15 | +At build time a temporary AssemblyInfo.cs will be created that contains the appropriate SemVer information. This will will be included in the build pipeline. |
| 16 | + |
| 17 | +Ensure you remove the `Assembly*Version` attributes from your `Properties\AssemblyInfo.cs` file so as to not get duplicate attribute warnings. Sample default: |
| 18 | + |
| 19 | + [assembly: AssemblyVersion("1.0.0.0")] |
| 20 | + [assembly: AssemblyFileVersion("1.0.0.0")] |
| 21 | + [assembly: AssemblyInformationalVersion("1.1.0+Branch.master.Sha.722aad3217bd49a6576b6f82f60884e612f9ba58")] |
| 22 | + |
| 23 | +Now when you build: |
| 24 | + |
| 25 | +* `AssemblyVersion` will be set to the `AssemblySemVer` variable. |
| 26 | +* `AssemblyFileVersion` will be set to the `MajorMinorPatch` variable with a appended `.0`. |
| 27 | +* `AssemblyInformationalVersion` will be set to the `InformationalVersion` variable. |
| 28 | + |
| 29 | +### Other injected Variables |
| 30 | + |
| 31 | +All other [variables](more-info/variables.md) will be injected into a internal static class. |
| 32 | + |
| 33 | +``` |
| 34 | +namespace AssemblyName |
| 35 | +{ |
| 36 | + [CompilerGenerated] |
| 37 | + internal static class GitVersionInformation |
| 38 | + { |
| 39 | + public static string Major = "1"; |
| 40 | + public static string Minor = "1"; |
| 41 | + public static string Patch = "0"; |
| 42 | + ...All other variables |
| 43 | + } |
| 44 | +} |
| 45 | +``` |
| 46 | + |
| 47 | +### Accessing injected Variables |
| 48 | + |
| 49 | +**All variables** |
| 50 | + |
| 51 | +``` |
| 52 | +var assemblyName = assembly.GetName().Name; |
| 53 | +var gitVersionInformationType = assembly.GetType(assemblyName + ".GitVersionInformation"); |
| 54 | +var fields = gitVersionInformationType.GetFields(); |
| 55 | +
|
| 56 | +foreach (var field in fields) |
| 57 | +{ |
| 58 | + Trace.WriteLine(string.Format("{0}: {1}", field.Name, field.GetValue(null))); |
| 59 | +} |
| 60 | +``` |
| 61 | + |
| 62 | +**Specific variable** |
| 63 | + |
| 64 | +``` |
| 65 | +var assemblyName = assembly.GetName().Name; |
| 66 | +var gitVersionInformationType = assembly.GetType(assemblyName + ".GitVersionInformation"); |
| 67 | +var versionField = gitVersionInformationType.GetField("Major"); |
| 68 | +Trace.WriteLine(versionField.GetValue(null)); |
| 69 | +``` |
| 70 | + |
| 71 | +## Populate some MSBuild properties with version metadata |
| 72 | + |
| 73 | +Task Name: `GitVersionTask.GetVersion` |
| 74 | + |
| 75 | +At build time all the derived [variables](more-info/variables.md) will be written to MSBuild properties so the information can be used by other tooling in the build pipeline. |
| 76 | + |
| 77 | +The class for `GitVersionTask.GetVersion` has a property for each variable. However at MSBuild time these properties a mapped to MSBuild properties that are prefixed with `GitVersion_`. This prevents conflicts with other properties in the pipeline. |
| 78 | + |
| 79 | +### Accessing variable in MSBuild |
| 80 | + |
| 81 | +After `GitVersionTask.GetVersion` has executed the properties can be used in the standard way. For example: |
| 82 | + |
| 83 | + <Message Text="GitVersion_InformationalVersion: $(GitVersion_InformationalVersion)"/> |
| 84 | + |
| 85 | +## Communicate variables to current Build Server |
| 86 | + |
| 87 | +Task Name: `GitVersionTask.WriteVersionInfoToBuildLog` |
| 88 | + |
| 89 | +If, at build time, it is detected that the build is occurring inside a Build Server server then the [variables](more-info/variables.md) will be written to the build log in a format that the current Build Server can consume. See [Build Server Support](build-server-support.md). |
| 90 | + |
| 91 | +## My Git repository requires authentication. What do I do? |
| 92 | + |
| 93 | +Set the environmental variables `GITVERSION_REMOTE_USERNAME` and `GITVERSION_REMOTE_PASSWORD` before the build is initiated. |
0 commit comments