在版本控制时将Git与.NET结合的最佳方法

时间:2022-01-05 13:21:43

I'm currently working on a project (just me), and I already know how to handle versioning on it. I'm using the classic <major>.<minor>.<build or patch>.

我正在研究一个项目(只有我),我已经知道如何处理它的版本控制。我正在使用经典的 或patch>

The problem I have is that I want to have tags in some of my commits pointing to the corresponding versions, but I don't want to do it manually.

我遇到的问题是我希望在我的一些提交中有标记指向相应的版本,但我不想手动执行。

Now I'm currently doing:

现在我正在做:

  • If I'm going to release the v0.2.13, I change the AssemblyInfo.cs and set that version
  • 如果我要发布v0.2.13,我将更改AssemblyInfo.cs并设置该版本
  • Commit the changes on Git
  • 在Git上提交更改
  • Add tag v0.2.13 (manually) on Git
  • 在Git上添加标签v0.2.13(手动)
  • Build the project
  • 建立项目
  • Create a .zip file (not all time time) and name it like ProjectName v0.2.13
  • 创建.zip文件(不是所有时间)并将其命名为ProjectName v0.2.13

Am I doing it wrong?

我做错了吗?

I could easily create an script to do the last step automatically, but I'm wondering if there is a good practice about automating the other part?

我可以轻松地创建一个脚本来自动完成最后一步,但我想知道是否有一个关于自动化其他部分的良好实践?

5 个解决方案

#1


8  

I have a build script in MSBuild that automatically retrieves the git commit currently checked out, and then adds an attribute like this at build time to the assembly:

我在MSBuild中有一个构建脚本,它自动检索当前检出的git提交,然后在构建时将这样的属性添加到程序集:

[assembly: AssemblyInformationalVersion("0.2.13+e3a6f2c1")]
[assembly: AssemblyVersion("0.2")]
[assembly: AssemblyFileVersion("0.2.13")]

The numerical part of the version comes from a version.txt file that my build script reads.

版本的数字部分来自我的构建脚本读取的version.txt文件。

The assembly version lacks the 3rd octet because it avoids binding redirect requirements when you increment the build number. The AssemblyFileVersion includes all the data allowed by that attribute, and AssemblyInformationVersion can be any string you want, so using semantic versioning to include the git commit id allows you to indicate exactly which source version built this project.

程序集版本缺少第3个八位字节,因为它可以在增加构建号时避免绑定重定向要求。 AssemblyFileVersion包含该属性允许的所有数据,AssemblyInformationVersion可以是您想要的任何字符串,因此使用语义版本控制来包含git commit id可以准确指出构建此项目的源版本。

Then after a build is successful, you can add a v0.2.13 tag if you wish. Personally, as soon as I build a final release, I increment the version.txt file so that all subsequent builds have the next higher version number. Each build after that will have that until I release that version, tagging the commit, increment the version.txt file again, and repeat.

然后,在构建成功后,您可以根据需要添加v0.2.13标记。就个人而言,只要我构建最终版本,我就会增加version.txt文件,以便所有后续版本都具有下一个更高版本号。之后的每个构建都将具有该版本,直到我发布该版本,标记提交,再次增加version.txt文件,然后重复。

By the way, the AssemblyInformationalVersion string appears in file properties from Windows Explorer, so this provides a guaranteed way to go from an arbitrary built binary to the matching original source code. Also, unfortunately this approach causes csc.exe to report an AL???? warning on builds because the semantic version format doesn't conform to x.y.z syntax. It doesn't mean anything is broken though, and I just ignore the warning.

顺便说一句,AssemblyInformationalVersion字符串出现在Windows资源管理器的文件属性中,因此这提供了从任意构建的二进制文件到匹配的原始源代码的保证方法。此外,不幸的是,这种方法导致csc.exe报告AL ????构建时发出警告,因为语义版本格式不符合x.y.z语法。这并不意味着任何事情都被打破了,我只是忽略了警告。

I never explicitly zip up sources because that is redundant with source control's responsibility. At least if your source is hosted by some online service (even on your private intranet), most git hosts offer on-the-fly download-as-zip capability for a given commit id already.

我从来没有明确压缩源代码,因为这对源代码管理的责任是多余的。至少如果你的源是由某些在线服务托管的(甚至在你的私人内部网上),大多数git主机已经为给定的提交ID提供了即时的download-as-zip功能。

#2


1  

I use Makefiles for everything after the commit.

提交后我将Makefiles用于所有内容。

Depending on how your build system works, you might be able to add a target "release" which tags the current branch and pushes this tag. You would also add a "package" target that depends on the "build" and "release" targets.

根据构建系统的工作方式,您可以添加标记当前分支的目标“release”并推送此标记。您还可以添加一个“包”目标,该目标取决于“构建”和“发布”目标。

If that doesn't work, just create your own Makefile. You might or might not have to name your "Makefile" differently.

如果这不起作用,只需创建自己的Makefile。您可能会或可能不必以不同方式命名“Makefile”。

#3


1  

If you don't have the capability to use a build server, that's probably the best way to do it manually. However, if you've got the time, I highly recommend getting some type of build server in place. We've had a lot of success with TeamCity. If you have it set up to track your repository, there are ways to do assembly info patching, tagging and post-build deployment (there are a bunch of possible links for those, I recommend just hitting up the documentation).

如果您没有能力使用构建服务器,那么这可能是手动执行此操作的最佳方式。但是,如果您有时间,我强烈建议您使用某种类型的构建服务器。我们在TeamCity上取得了很大的成功。如果你设置它来跟踪你的存储库,有很多方法可以进行汇编信息修补,标记和构建后部署(有很多可能的链接,我建议只需点击文档)。

I found this post pretty helpful when setting up our versioning; you can pretty much exclude the NuGet bits and it should have some decent information. As far as setting up TeamCity goes, just do a quick search for "TeamCity Setup Tutorial" and you should have a ton of resources. Good luck!

我在设置版本时发现这篇文章非常有用;你几乎可以排除NuGet位,它应该有一些不错的信息。就设置TeamCity而言,只需快速搜索“TeamCity设置教程”,您就应该拥有大量资源。祝你好运!

#4


0  

I have successfully used the following steps to accomplish something similar to:

我已成功使用以下步骤来完成类似的操作:

  • Push new commits to a shared git repo
  • 将新提交推送到共享git仓库
  • TeamCity server checks out code, runs tests and builds the project
  • TeamCity服务器检出代码,运行测试并构建项目
  • If successful, TeamCity also moves/deletes/packages files for a deployment package
  • 如果成功,TeamCity还会移动/删除/打包部署包的文件
  • Then it FTPs the zip to a deployment staging area, where it can be manually deployed (this could be fully automated, but our setup was weird)
  • 然后它将zip FTP到部署暂存区域,在那里可以手动部署(这可以完全自动化,但我们的设置很奇怪)

You could use a post-commit hook to tag your branch and push to your TeamCity server.

您可以使用post-commit挂钩标记分支并推送到TeamCity服务器。

#5


0  

Nant fits your needs ! Check this : NAnt tasks reference

Nant符合您的需求!检查一下:NAnt任务参考

It's easy to use and combine with a git repository :-)

它易于使用并与git存储库结合:-)

#1


8  

I have a build script in MSBuild that automatically retrieves the git commit currently checked out, and then adds an attribute like this at build time to the assembly:

我在MSBuild中有一个构建脚本,它自动检索当前检出的git提交,然后在构建时将这样的属性添加到程序集:

[assembly: AssemblyInformationalVersion("0.2.13+e3a6f2c1")]
[assembly: AssemblyVersion("0.2")]
[assembly: AssemblyFileVersion("0.2.13")]

The numerical part of the version comes from a version.txt file that my build script reads.

版本的数字部分来自我的构建脚本读取的version.txt文件。

The assembly version lacks the 3rd octet because it avoids binding redirect requirements when you increment the build number. The AssemblyFileVersion includes all the data allowed by that attribute, and AssemblyInformationVersion can be any string you want, so using semantic versioning to include the git commit id allows you to indicate exactly which source version built this project.

程序集版本缺少第3个八位字节,因为它可以在增加构建号时避免绑定重定向要求。 AssemblyFileVersion包含该属性允许的所有数据,AssemblyInformationVersion可以是您想要的任何字符串,因此使用语义版本控制来包含git commit id可以准确指出构建此项目的源版本。

Then after a build is successful, you can add a v0.2.13 tag if you wish. Personally, as soon as I build a final release, I increment the version.txt file so that all subsequent builds have the next higher version number. Each build after that will have that until I release that version, tagging the commit, increment the version.txt file again, and repeat.

然后,在构建成功后,您可以根据需要添加v0.2.13标记。就个人而言,只要我构建最终版本,我就会增加version.txt文件,以便所有后续版本都具有下一个更高版本号。之后的每个构建都将具有该版本,直到我发布该版本,标记提交,再次增加version.txt文件,然后重复。

By the way, the AssemblyInformationalVersion string appears in file properties from Windows Explorer, so this provides a guaranteed way to go from an arbitrary built binary to the matching original source code. Also, unfortunately this approach causes csc.exe to report an AL???? warning on builds because the semantic version format doesn't conform to x.y.z syntax. It doesn't mean anything is broken though, and I just ignore the warning.

顺便说一句,AssemblyInformationalVersion字符串出现在Windows资源管理器的文件属性中,因此这提供了从任意构建的二进制文件到匹配的原始源代码的保证方法。此外,不幸的是,这种方法导致csc.exe报告AL ????构建时发出警告,因为语义版本格式不符合x.y.z语法。这并不意味着任何事情都被打破了,我只是忽略了警告。

I never explicitly zip up sources because that is redundant with source control's responsibility. At least if your source is hosted by some online service (even on your private intranet), most git hosts offer on-the-fly download-as-zip capability for a given commit id already.

我从来没有明确压缩源代码,因为这对源代码管理的责任是多余的。至少如果你的源是由某些在线服务托管的(甚至在你的私人内部网上),大多数git主机已经为给定的提交ID提供了即时的download-as-zip功能。

#2


1  

I use Makefiles for everything after the commit.

提交后我将Makefiles用于所有内容。

Depending on how your build system works, you might be able to add a target "release" which tags the current branch and pushes this tag. You would also add a "package" target that depends on the "build" and "release" targets.

根据构建系统的工作方式,您可以添加标记当前分支的目标“release”并推送此标记。您还可以添加一个“包”目标,该目标取决于“构建”和“发布”目标。

If that doesn't work, just create your own Makefile. You might or might not have to name your "Makefile" differently.

如果这不起作用,只需创建自己的Makefile。您可能会或可能不必以不同方式命名“Makefile”。

#3


1  

If you don't have the capability to use a build server, that's probably the best way to do it manually. However, if you've got the time, I highly recommend getting some type of build server in place. We've had a lot of success with TeamCity. If you have it set up to track your repository, there are ways to do assembly info patching, tagging and post-build deployment (there are a bunch of possible links for those, I recommend just hitting up the documentation).

如果您没有能力使用构建服务器,那么这可能是手动执行此操作的最佳方式。但是,如果您有时间,我强烈建议您使用某种类型的构建服务器。我们在TeamCity上取得了很大的成功。如果你设置它来跟踪你的存储库,有很多方法可以进行汇编信息修补,标记和构建后部署(有很多可能的链接,我建议只需点击文档)。

I found this post pretty helpful when setting up our versioning; you can pretty much exclude the NuGet bits and it should have some decent information. As far as setting up TeamCity goes, just do a quick search for "TeamCity Setup Tutorial" and you should have a ton of resources. Good luck!

我在设置版本时发现这篇文章非常有用;你几乎可以排除NuGet位,它应该有一些不错的信息。就设置TeamCity而言,只需快速搜索“TeamCity设置教程”,您就应该拥有大量资源。祝你好运!

#4


0  

I have successfully used the following steps to accomplish something similar to:

我已成功使用以下步骤来完成类似的操作:

  • Push new commits to a shared git repo
  • 将新提交推送到共享git仓库
  • TeamCity server checks out code, runs tests and builds the project
  • TeamCity服务器检出代码,运行测试并构建项目
  • If successful, TeamCity also moves/deletes/packages files for a deployment package
  • 如果成功,TeamCity还会移动/删除/打包部署包的文件
  • Then it FTPs the zip to a deployment staging area, where it can be manually deployed (this could be fully automated, but our setup was weird)
  • 然后它将zip FTP到部署暂存区域,在那里可以手动部署(这可以完全自动化,但我们的设置很奇怪)

You could use a post-commit hook to tag your branch and push to your TeamCity server.

您可以使用post-commit挂钩标记分支并推送到TeamCity服务器。

#5


0  

Nant fits your needs ! Check this : NAnt tasks reference

Nant符合您的需求!检查一下:NAnt任务参考

It's easy to use and combine with a git repository :-)

它易于使用并与git存储库结合:-)