简体   繁体   English

如何避免在源代码中保留版本号?

[英]How to avoid keeping version number in source code?

Up to now we keep the version number of our python source code in setup.py. 到目前为止,我们在setup.py中保留了python源代码的版本号。

This version gets increased after every successful ci run. 每次成功运行ci后,此版本都会增加。

This means the version of central libraries get increased several times per day. 这意味着中央库的版本每天都会增加几次。

Since the version number is stored in a file in the git repo, every increase of the version number is a new commit. 由于版本号存储在git仓库中的文件中,因此版本号的每次增加都是新的提交。

This means roughly 50% of all commits are not made by humans, but by CI. 这意味着大约50%的提交不是由人类提出的,而是由CI提出的。

I have got the feeling, that we are on the wrong track. 我有种感觉,我们走错了路。 Maybe it is no good solution to keep the version number in ci. 也许在ci中保留版本号并不是一个好方法。

How could we avoid the "useless" CI commits which just increase the version number? 我们怎样才能避免仅增加版本号的“无用”CI提交?

How to avoid keeping version number in source code? 如何避免在源代码中保留版本号?

Update 更新

We live without manual release since several years. 几年以来,我们没有手动释放。 We do not have a versioning scheme like MAJOR.MINOR. 我们没有像MAJOR.MINOR这样的版本控制方案。 And we have not missed this in the past. 我们过去并没有错过这个。 I know that this does not work for all environments. 我知道这不适用于所有环境。 But it works for my current environment. 但它适用于我目前的环境。

We have a version number which looks like this: YEAR.MONTH.X 我们有一个版本号,如下所示:YEAR.MONTH.X

This means every commit which passes CI is a new release. 这意味着每次通过CI的提交都是新版本。

After reading the answers I realize: I need to asks myself: Do I have a version number at all? 读完答案后,我意识到:我需要问自己:我有版本号吗? I think no. 我想不是。 I have a build number . 我有一个内部版本号 More is not needed in this context. 在这种情况下不需要更多。

(thank you for the up-votes. Before asking this question I was sure that this question will get closed because people will think it is "unclear" or "too broad") (谢谢你的选票。在提出这个问题之前,我确信这个问题会被关闭,因为人们会认为它“不清楚”或“过于宽泛”)

It is a common practice to keep a version number in the source code, there is nothing wrong in that. 在源代码中保留版本号是一种常见做法,没有任何错误。

You need to separate CI procedures to regular builds, release publishing and release deployment. 您需要将CI过程分离为常规构建,发布发布和发布部署。

Regular builds: run daily or even after each commit, can include static code analysis and automatic tests, check if the code can be built at all. 常规构建:每天运行或甚至在每次提交后运行,可以包括静态代码分析和自动测试,检查代码是否可以构建。 Regular builds should not change the version number. 常规版本不应更改版本号。

Release publishing: can only be triggered by explicit manual action by release manager. 发布发布:只能由发布经理明确的手动操作触发。
The trigger action could be tagging a commit with a new version number, new merge into the release branch, or just a commit that changes version number kept in a special file (eg pom.xml ). 触发器操作可以是使用新版本号标记提交,将新合并标记到发布分支,或者仅更改保存在特殊文件(例如pom.xml )中的版本号的提交。 Refer to git flow for example. 例如,请参阅git flow。
Release publishing assigns a new version number (either automatically or manually), commits it into the source code if necessary, builds a binary package with a new version and uploads it to the binary package repository (eg Nexus, devpi, local APT repository, Docker registry and so on). 发布发布分配新版本号(自动或手动),必要时将其提交到源代码中,构建具有新版本的二进制包并将其上载到二进制包存储库(例如Nexus,devpi,本地APT存储库,Docker注册表等)。

Release deployment: another manually triggered action that takes a ready binary package from a package repository and installs it to the target environment (dev, QA / UAT / staging, part of production for canary deployments or to the whole production environment). 发布部署:另一个手动触发的操作,从包存储库中获取现成的二进制包,并将其安装到目标环境(dev,QA / UAT / staging,部分生产用于canary部署或整个生产环境)。

I think you should use git flow. 我认为你应该使用git flow。 And create a master branch and a develop branch. 并创建一个主分支和一个开发分支。 Every time the CI checks the develop the version number remains the same. CI每次检查开发时版本号都保持不变。 Everytime you create a release eg merge develop into master, you can increase the version number by CI. 每次创建发布版本时,例如merge develop into master,您都可以通过CI增加版本号。

Or have i missing something, but in my Opinion there is no reason that the version number is increased everytime ci runs. 或者我错过了什么,但在我的意见中没有理由每次ci运行时版本号都会增加。

So all in all you better should think about when to "release" changes to a new version!! 所以最重要的是你应该考虑何时“发布”新版本的更改!!

If the project's kept in a git repo for production use, just use whichever variant of git describe floats your boat, no need to store it in a tracked file because the result identifies the particular history, and you've got that history right there. 如果项目保存在git仓库中供生产使用,只需使用git describe任何变体浮动你的船,不需要将它存储在跟踪文件中,因为结果标识了特定的历史,并且你已经有了那个历史。

If the source is shipped separately, you can use git archive and the export-subst attribute to embed pretty much anything you want in the exported source. 如果源是单独提供的,您可以使用git archiveexport-subst属性在导出的源中嵌入您想要的任何内容。

PS : Being a new user cannot add comment. PS:作为新用户无法添加评论。

support and expand on this answer by @VibrantVivek. @VibrantVivek支持并扩展这个答案。

For Continuous-Integration , tagging of repository back is very important and whether you keep it in your code or simply by any other git way , after every successful CI there must be corresponding tag/version. 对于持续集成 ,对存储库的标记非常重要,无论是将其保存在代码中还是仅通过任何其他git方式,在每次成功的CI之后都必须有相应的标记/版本。

And if you're having CI tags/version which are not against commit , then something really wrong is at work here. 如果你有CI标签/版本不反对提交,那么真正错误的是在这里工作。

And +1 for Martin Fowler , here another link for a more detailed article (more or less by same person) https://www.thoughtworks.com/continuous-integration (recommend to read please) 和Martin Fowler的+1,这里是另一个更详细的文章链接(或多或少由同一个人) https://www.thoughtworks.com/continuous-integration (推荐阅读请)

Premises: 物业:

I assume these are the premises under which the solution is discussed. 我认为这些是讨论解决方案的前提。

  1. Currently version number is kept in a git-tracked source file, but you are OK to get rid of it. 目前版本号保存在git跟踪的源文件中,但您可以将其删除。
  2. No one manually manages version number, nor triggers a release procedure, which includes: (a) increase version number, (b) build from source and (c) store the built result somewhere. 没有人手动管理版本号,也没有触发发布过程,其中包括:(a)增加版本号,(b)从源构建和(c)将构建的结果存储在某处。 These are taken cared by CI, and SHOULD remain that way. 这些都是由CI照顾的,应该保持这种状态。

Solution: 解:

  1. Instead of writing to a source file and create new commit, CI simply tag the specific commit that passed CI check, then push the tag to remote repo. CI只是标记通过CI检查的特定提交,而不是写入源文件并创建新提交,然后将标记推送到远程仓库。
  2. The build script should read the tag of current HEAD commit, and use it as the version number for publishing a release. 构建脚本应该读取当前HEAD提交的标记,并将其用作发布版本的版本号。
  3. Optionally, you might want to use git filter-branch to rewrite your existing git repo history, tag previous release commits for consistency, remove and stop tracking the version number source cile, then get rid of those CI commits. 或者,您可能希望使用git filter-branch来重写现有的git repo历史记录,标记先前版本提交的一致性,删除并停止跟踪版本号源cile,然后删除那些CI提交。

On your first question : 关于你的第一个问题:

How could we avoid the "useless" CI commits which just increase the version number? 我们怎样才能避免仅增加版本号的“无用”CI提交?

Please se Continuous Integration (CI) is a development practice which verifies each check-in by an automated build , allowing teams to detect problems early. 继续集成(CI)是一种开发实践,它通过自动构建验证每个check-in ,允许团队尽早发现问题。

Having said that, would like to articulate here : 话虽如此,想在此明确表达:

  • From practice : Every commit should build on an integration machine 从实践中: 每个提交应该建立在集成机器上

  • Under how to do it : The CI server monitors the repository and checks out changes when they occur 根据如何操作: CI服务器监视存储库并在发生更改时检出更改

In simple words, the CI server should enhance the version only and only when there is a new commit and thus making sure every code commit is releasable. 简单来说, CI服务器 应该 增强版本, 并且只有在有新的提交时才会 增强从而确保每个代码提交都是可释放的。

Looks like from OP , that in your area there more (as you said) "useless" commits from CI server . 看起来像OP,在您所在的地区,更多(如您所说) “无用” commits来自CI server

Based on your CI mechanism, I hope you should/must be able to control it , almost there are ways to handle in every tool we use. 根据您的CI机制,我希望您应该/必须能够控制它,几乎有办法处理我们使用的每个工具。 (Eg: webhooks in bitbucket, version plugin etc). (例如:bitbucket中的webhooks,版本插件等)。

So, making sure only after a new commit we have a new version. 因此,只有在新提交之后我们才能确定新版本。

Now if you're thinking about those regular nightly integration builds , then read below : 现在,如果您正在考虑那些常规的夜间集成构建,请阅读以下内容:

Many organizations do regular builds on a timed schedule, such as every night. 许多组织按照定时计划定期构建,例如每晚。 This is not the same thing as a continuous build and isn't enough for continuous integration . 这与连续构建不同,并不足以进行持续集成 The whole point of continuous integration is to find problems as soon as you can. 持续整合的重点是尽快发现问题。 Nightly builds mean that bugs lie undetected for a whole day before anyone discovers them. 每夜构建意味着在任何人发现它们之前一整天都没有发现错误。 Once they are in the system that long, it takes a long time to find and remove them. 一旦他们在系统中长时间,找到并删除它们需要很长时间。

Also you have mentioned : Every commit which passes CI is a new release , thus in a way you're already on true CI. 你也提到过: 每次通过CI的提交都是一个新版本 ,因此你已经在真正的CI上。

Despite this, if you're still unable to figure out how you can avoid "useless" commits of version number, then I would suggest to add another question with detail on how your CI mecahnism works and why it is difficult with given conditions. 尽管如此,如果您仍然无法弄清楚如何避免版本号的"useless" commits ,那么我建议添加另一个问题,详细说明您的CI mecahnism如何工作以及为什么在给定条件下很难。 I bet there must be a solution. 我打赌必须有一个解决方案。 Also have look on GithubFlowVsGitFlow . 还可以看一下GithubFlowVsGitFlow

source : Martin fowler's white paper on CI 来源: Martin fowler关于CI的白皮书

How to avoid keeping version number in source code? 如何避免在源代码中保留版本号?

On this, would like to expand on @void answer as it is said there there: 在此,想扩展@void答案,因为它在那里说:

It is a common practice to keep a version number in the source code, there is nothing wrong in that.

There are projects which have to know the exact version deployed (for some important xy reasons) in such scenarios they keep version in source and HTTP GET API to fetch from deployed code (one way of doing it) to know the version currently deployed on X server . 有些项目必须知道部署的确切version (出于一些重要的xy原因),在这种情况下,他们将版本保存在源和HTTP GET API中以从部署的代码中获取(一种方式)以了解当前部署在X上的版本服务器

However it is more on the requirement, suppose for another project there is no such situation then recommended way to keep version is using commit hash / tagging each successful CI build. 然而,更多的是要求,假设另一个项目没有这种情况,然后建议的方式来保持version使用commit哈希/ tagging每个成功的CI构建。

You can have more details here : 您可以在此处获得更多详细信息

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在保持.py 源代码可编辑的同时将 python 打包为 exe? - How to pack a python to exe while keeping .py source code editable? setup.py(setuptools)和源代码中的自动版本号? - Automatic version number both in setup.py (setuptools) AND source code? 如何在源代码中检查numpy的当前版本? - How to check the current version of numpy in the source code? 如何通过将源代码保留在 Github 中,将我的 python 程序作为 web 应用程序在线共享? - How to share my python program online as a web app by keeping the source code in Github? Python/Django - 避免在源代码中保存密码 - Python/Django - Avoid saving passwords in source code 打标源代码兼容Python版本 - Marking source code with a compatible Python version 如何避免代码重复和冗余 - How to avoid code repetition and redundancy 如何避免此 Python 代码中的错误? - How to avoid an error in this Python code? 如何避免循环中的代码阻塞 - How to avoid code blocking in a loop 如何让setuptools从另一个源安装一个包,该源也可以在pypi上使用相同的版本号? - How can I make setuptools install a package from another source that's also available on pypi with the same version number?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM