Answered by:
Embedding build information in local and CI builds like in Gradle

Question
-
my question does not target a problem. It is more some kind of "Do you know something that...?". All my applications are built and deployed using CI/CD with Azure DevOps. I like to have all build information handy in the create binary and to read them during runtime. Those applications are mainly .NET Core 2 applications written in C#. I am using the default build system MSBuild supplied with the .NET Core SDK. The project should be buildable on Windows AND Linux.
Information I need:
- GitCommitHash: string
- GitCommitMessage: string
- GitBranch: string
- CiBuildNumber: string (only when built via CI not locally)
- IsCiBuild: bool (Detecting should work by checking for env variables which are only available in CI builds)
Current approach:
In each project in the solution there is a class BuildConfig à lapublic static class BuildConfig { public const string BuildNumber = "#{Build.BuildNumber}#"; // Those are the names of the variables when using CI // and the remaining information... }
Here tokens are used, which get replaced with the corresponding values during the CI build. To achieve this an addon task is used. Sadly this only fills the values for CI builds and not for the local ones. When running locally and requesting the build information it only contains the tokens as they are not replaced during the local build.
It would be cool to either have the BuildConfig.cs generated during the build or have the values of the variables set during the local build (IntelliSense would be very cool and would prevent some "BuildConfig class could not be found" errors). The values could be set by an MSBuild task (?). That would be one (or two) possibilities to solve this. Do you have ideas/experience regarding this? I did not found that much during my internet research. I only stumbled over a question on StackOverflow with id 32670078 (Sorry - I can not include links) which did not really help me as I have zero experience with MSBuild tasks/customization.
Then I decided to have a look at build systems in general. Namly Fake and Cake. Cake has a Git-Addin, but I did not find anything regarding code generation/manipulation. Do you know some resources on that?
So here's the thing...
Short time ago I had to work with Android apps namly Java and the build system gradle. So I wanted to inject the build information there too during the CI build. After a short time I found a (imo) better and more elegant solution to do this. And this was modifying the build script in the following way (Scripting language used is Groovy which is based on Java):def getGitHash = { -> def stdout = new ByteArrayOutputStream() exec { commandLine 'git', 'rev-parse', '--short', 'HEAD' standardOutput = stdout } return stdout.toString().trim().replace("\"", "\\\"") } def getGitBranch = { -> def fromEnv = System.getenv("BUILD_SOURCEBRANCH") if (fromEnv) { return fromEnv.substring("refs/heads/".length()).replace("\"", "\\\""); } else { def stdout = new ByteArrayOutputStream() exec { commandLine 'git', 'rev-parse', '--abbrev-ref', 'HEAD' standardOutput = stdout } return stdout.toString().trim().replace("\"", "\\\"") } } def getIsCI = { -> return System.getenv("BUILD_BUILDNUMBER") != null; } # And the other functions working very similar android { # ... buildConfigField "String", "GitHash", "\"${getGitHash()}\"" buildConfigField "String", "GitBranch", "\"${getGitBranch()}\"" buildConfigField "String", "BuildNumber", "\"${getBuildNumber()}\"" buildConfigField "String", "GitMessage", "\"${getGitCommitMessage()}\"" buildConfigField "boolean", "IsCIBuild", "${getIsCI()}" # ... }
The result after the first build is the following java code:
public final class BuildConfig { // Some other fields generated by default // Fields from default config. public static final String BuildNumber = "Local Build"; public static final String GitBranch = "develop"; public static final String GitHash = "6c87e82"; public static final String GitMessage = "Merge branch 'hotfix/login-failed' into 'develop'"; public static final boolean IsCIBuild = false; }
Getting the required information is done by the build script itself without depending on the CI engine to fulfill this task. This class can be used after the first build its generated and stored in a "hidden" directory which is included in code analysis but exluded from your code in the IDE and also not pushed to the Git. But there is IntelliSense support. In C# project this would be the obj/ folder I guess. It is very easy to access the information as they are a constant and static values (so no reflection or similar required).
So here the summarized question: "Do you know something to achieve this behaviour/mechanism in a .NET environment?"
Happy to discuss some ideas/approaches... :)- Moved by Wendy ZangMicrosoft contingent staff Friday, August 9, 2019 2:29 AM
Thursday, August 8, 2019 5:53 PM
Answers
-
So the question is about C#, libraries and its tools...
Then if it were me I'd try asking again in C# forum but leave out all the other reference to devops, android, java, third party tools, etc. else they'll likley toss it again.
https://social.msdn.microsoft.com/Forums/en-US/home?forum=csharpgeneral
Regards, Dave Patrick ....
Microsoft Certified Professional
Microsoft MVP [Windows Server] Datacenter Management
Disclaimer: This posting is provided "AS IS" with no warranties or guarantees, and confers no rights.- Proposed as answer by Richard MuellerMVP, Banned Friday, August 9, 2019 7:53 PM
- Marked as answer by Richard MuellerMVP, Banned Friday, August 16, 2019 12:22 PM
Friday, August 9, 2019 1:55 PM
All replies
-
Hi hypervtechnics,
Thank you for posting here.
Since this thread is related to azure and java, we do not support it. Therefore, I will move it to where is forum for.. forum to redirect it to the correct forum.
The Visual C# forum discusses and asks questions about the C# programming language, IDE, libraries, samples, and tools.
Best Regards,
Jack
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.Friday, August 9, 2019 2:28 AM -
I'd try asking for help over here.
https://stackoverflow.com/questions/tagged/azure-devops
https://developercommunity.visualstudio.com/spaces/22/index.html
https://forums.asp.net/1255.aspx/1?ASP+NET+Core
Regards, Dave Patrick ....
Microsoft Certified Professional
Microsoft MVP [Windows Server] Datacenter Management
Disclaimer: This posting is provided "AS IS" with no warranties or guarantees, and confers no rights.
- Edited by Dave PatrickMVP Friday, August 9, 2019 2:49 AM
Friday, August 9, 2019 2:43 AM -
You are wrong. Please read the question again. My question uses the Java thing as an example of how it might work and also as kind of sketchy approach. Azure DevOps does not have anything to do directly with this. The question targets the usage of MSBuild/Other build system to generate/dynamically modify code during the build. No more than that. The Java and Azure part are just for the why and a sketchy how...
So the question is about C#, libraries and its tools...
Friday, August 9, 2019 10:58 AM -
I already asked on StackOverflow.
https://stackoverflow.com/questions/57407919/embedding-build-information-in-local-and-ci-builds-like-in-gradle
- Edited by hypervtechnics Friday, August 9, 2019 10:59 AM
Friday, August 9, 2019 10:59 AM -
Sounds good.
Regards, Dave Patrick ....
Microsoft Certified Professional
Microsoft MVP [Windows Server] Datacenter Management
Disclaimer: This posting is provided "AS IS" with no warranties or guarantees, and confers no rights.Friday, August 9, 2019 12:38 PM -
So the question is about C#, libraries and its tools...
Then if it were me I'd try asking again in C# forum but leave out all the other reference to devops, android, java, third party tools, etc. else they'll likley toss it again.
https://social.msdn.microsoft.com/Forums/en-US/home?forum=csharpgeneral
Regards, Dave Patrick ....
Microsoft Certified Professional
Microsoft MVP [Windows Server] Datacenter Management
Disclaimer: This posting is provided "AS IS" with no warranties or guarantees, and confers no rights.- Proposed as answer by Richard MuellerMVP, Banned Friday, August 9, 2019 7:53 PM
- Marked as answer by Richard MuellerMVP, Banned Friday, August 16, 2019 12:22 PM
Friday, August 9, 2019 1:55 PM