我写了一个简单的script在 Python 中。
现在我想让 travis 检查我的代码。 travis 成功后,版本号应该会增加。
到目前为止我的脚本还没有版本号。我可以将它存储在任何对自动递增工作流程有意义的地方。
如何为 Python 代码执行此操作?
更新
现在有效:
- 运行测试
- bumpversion
- 推送标签到master
不幸的是,travis 不支持“after-all”。这意味着如果我想运行多个 Python 版本的测试,我无法在所有 Python 版本测试成功后进行 bumpversion。
在我的例子中,我只会检查 Python2.7,直到 travis 解决了这个问题:https://github.com/travis-ci/travis-ci/issues/929
这是我的简单脚本:https://github.com/guettli/compare-with-remote
已解决:-)
现在有效:
- 开发者推送到github
- Travis-CI 运行
- 如果所有测试都成功 bumpversion 增加版本
- setup.py 中的新版本已推送到 github 仓库
- 使用工具
twine
将新版本的 python 包上传到 pypi。
我在这里解释了我使用 github、travis 和 pypi 进行 CI 的方式:https://github.com/guettli/github-travis-bumpversion-pypi
最佳答案
如果您接受为您的版本控制进行额外提交,您可以将此脚本添加到 continuous_integration/increment_version.py
import os
import pkg_resources
if __name__ == "__main__":
version = pkg_resources.get_distribution("compare_with_remote").version
split_version = version.split('.')
try:
split_version[-1] = str(int(split_version[-1]) + 1)
except ValueError:
# do something about the letters in the last field of version
pass
new_version = '.'.join(split_version)
os.system("sed -i \"s/version='[0-9.]\+'/version='{}'/\" setup.py"
.format(new_version))
os.system("git add -u")
os.system("git commit -m '[ci skip] Increase version to {}'"
.format(new_version))
os.system("git push")
并将您的 .travis.yml
更改为
after_success:
- python continuous_integration/increment_version.py
我不确定如何使 git push
部分工作,因为它需要使用 repo 权限进行一些测试,但我认为您可能可以设置一些东西以允许 travis 推送您的 repo 。你可以看看那个 post例如。
另请注意,我使用 python
执行 git
操作,但它们可以作为额外行添加到 after_success
字段中:
after_success:
- python continuous_integration/increment_version.py
- git add -u
- git commit -m "[ci skip] version changed"
- git push
我只是觉得将版本号放在提交消息中很方便。
另外,添加[ci skip]也很重要在提交消息中避免无限递增。也许在特定的提交消息标记上触发版本更改会更安全。
关于python - 如果 github 上的 Travis 成功,则增加版本号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42718373/