Atlassian Bamboo 与 Django 和 Django Python——可能吗?

发布于 2024-08-05 05:10:10 字数 470 浏览 3 评论 0原文

在我的公司,我们目前使用 Atlassian Bamboo 作为我们的持续集成工具。目前我们所有的项目都使用 Java,所以它运行得很好。

但是,我们正在考虑将 Django + Python 用于我们的新应用程序之一。我想知道是否可以使用 Bamboo 来实现这一点。

首先,我要说的是,我对 Bamboo 的熟悉程度很低,因为我只使用过它,没有配置过它(除了简单的更改,例如更改构建的 svn checkout 目录)。

显然,仅仅运行构建并没有多大意义(因为 Python 项目并没有真正构建),但我希望能够使用 Bamboo 来运行测试套件,以及使用 Bamboo 来部署就像我们处理 Java 项目一样,将最新代码添加到我们的各种测试环境中。

Bamboo 在 Python 项目中支持这种类型的东西吗?

At my company, we currently use Atlassian Bamboo for our continuous integration tool. We currently use Java for all of our projects, so it works great.

However, we are considering using a Django + Python for one of our new applications. I was wondering if it is possible to use Bamboo for this.

First off, let me say that I have a low level of familiarity with Bamboo, as I've only ever used it, not configured it (other than simple changes like changing the svn checkout directory for a build).

Obviously there isn't a lot of point in just running a build (since Python projects don't really build), but I'd like to be able to use Bamboo for running the test suite, as well as use bamboo to deploy the latest code to our various test environments the way we do with our Java projects.

Does Bamboo support this type of thing with a Python project?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

一张白纸 2024-08-12 05:10:10

Bamboo 本质上只是运行一个 shell 脚本,所以这可能很容易:

./manage.py test

就像通常那样:

mvn clean install

或者:

ant compile

您可能必须将 Django 测试运行器的输出按摩到传统的 JUnit XML 输出中,以便 Bamboo 可以为您提供漂亮的图表通过了多少测试。查看这篇文章,了解如何使用 xmlrunner.py 让 Python 工作与 Hudson。另请查看 NoseXUnit。

Bamboo essentially just runs a shell script, so this could just as easily be:

./manage.py test

as it typically is:

mvn clean install

or:

ant compile

You may have to massage to output of the Django test runner into traditional JUnit XML output, so that Bamboo can give you pretty graphs on how many tests passed. Look at this post about using xmlrunner.py to get Python working with Hudson. Also take a look at NoseXUnit.

如梦初醒的夏天 2024-08-12 05:10:10

您甚至可以在干净的环境中轻松添加 pip 和 virtualenv 的引导程序,这很酷:

wget https://bootstrap.pypa.io/get-pip.py
python get-pip.py --root=${bamboo.build.working.directory}/tmp --ignore-installed
export PATH=${bamboo.build.working.directory}/tmp/usr/local/bin:$PATH
export PYTHONPATH=${bamboo.build.working.directory}/tmp/usr/local/lib/python2.7/dist-packages:$PYTHONPATH
pip install --root=${bamboo.build.working.directory}/tmp --ignore-installed virtualenv
virtualenv virtual_tmp
cd virtual_tmp
. bin/activate
echo Pip is located `which pip`
pip install django
pip install djangorestframework

警告,source bin/activate 不起作用,因为内联脚本任务存储在 sh 文件中(因此bashsh 兼容模式运行)。

编辑

更好的是,我们可以在它的顶部运行单元测试,并使用可由bamboo 的 JUnit 解析的 xml 输出:

pip install unittest-xml-reporting
python manage.py test --noinput --testrunner="xmlrunner.extra.djangotestrunner.XMLTestRunner"

You can even add a bootstrap for pip and virtualenv on a clean environment quite easily, which is cool:

wget https://bootstrap.pypa.io/get-pip.py
python get-pip.py --root=${bamboo.build.working.directory}/tmp --ignore-installed
export PATH=${bamboo.build.working.directory}/tmp/usr/local/bin:$PATH
export PYTHONPATH=${bamboo.build.working.directory}/tmp/usr/local/lib/python2.7/dist-packages:$PYTHONPATH
pip install --root=${bamboo.build.working.directory}/tmp --ignore-installed virtualenv
virtualenv virtual_tmp
cd virtual_tmp
. bin/activate
echo Pip is located `which pip`
pip install django
pip install djangorestframework

Warning, source bin/activate does not work as the inline script tasks are stored into an sh file (so bash run it in sh compatibility mode).

Edit

Even better, we can run unit tests on the top of it, with xml outputs that can be parsed by the JUnit of bamboo:

pip install unittest-xml-reporting
python manage.py test --noinput --testrunner="xmlrunner.extra.djangotestrunner.XMLTestRunner"
过度放纵 2024-08-12 05:10:10

事实证明这是可能的。有两个主要的集成任务:测试运行器结果和代码覆盖率结果。我假设使用正常的 Python 3 代码库和标准 unittest 测试套件。

测试运行程序

Bamboo 期望测试运行程序结果采用 JUnit XML 格式 。 Cheese Shop 上有单独的测试运行程序能够产生这样的输出,但是它需要您编写一些代码来运行它,这不太好。保持代码库完整的更好方法是使用 pytest 的功能。

代码覆盖率

Bamboo 仅支持 Atlassian Clover 的 XML 格式。这里重要的注意事项是,您不需要启用 Atlassian Clover 插件(以及需要花费一些钱的许可证)。竹子可以自行发挥作用。

Python事实上的标准代码覆盖率工具,覆盖率,产生了一些
Cobertura XML 格式,但有一个转换器。有一个 pytest 插件,用于与覆盖率工具集成。

解决方案

这是 Tox 环境,我在其中使用 pytest 使两个 Bamboo 集成都能工作。

[tox]
envlist   = py34
skipsdist = True

[testenv]
setenv     = LANG=C.UTF-8
basepython = python3.4
deps       = -r{toxinidir}/requirements.txt

[testenv:bamboo]
commands = 
  py.test --junitxml=results.xml \
    --cov=project_name --cov-config=tox.ini --cov-report=xml \
    --cov-report=html project_name/test
    coverage2clover -i coverage.xml -o clover.xml
deps = 
    {[testenv]deps}
    pytest
    pytest-cov
    coverage2clover

# read by pytest
[pytest]
python_files = *.py

# read by coverage
[run]
omit=project_name/test/*,project_name/__main__.py

请注意,pytest 和 pytest-cov 都使用 tox.ini 进行命令行不支持的配置。它再次避免了您的存储库根目录中出现额外的混乱。 pytest 尝试自动读取 tox.ini 。 pytest-cov 绕过 .coveragerc,但因为它也是一个 INI 文件,所以 tox.ini 适合。

在 Bamboo 端添加一个运行 tox -eamboo< 的 脚本任务 /代码>。然后将 JUnit 解析任务 添加到作业中。在其对话框中,在指定自定义结果目录下放置results.xml

覆盖范围配置以其他方式完成。

  1. 打开作业的“杂项”选项
  2. 卡 检查“使用 Clover 收集此构建的代码覆盖率”
  3. 选择“Clover 已集成到此构建中”,并将生成一个 clover.xml 文件
  4. Clover XML 位置中输入clover.xml

在此处输入图像描述

此时,在您的下一个版本中,您将看到总覆盖率和两个图表:< em>覆盖率历史记录和代码行历史记录。使用覆盖率工具生成交互式 HTML 也很好,这样您就可以深入到特定的代码行。

上述设置(至少在 Bamboo 5.7 中)已在 Artifact 作业选项卡中创建了 Clover 报告(系统)。打开它并将 htmlcov 设置为 位置 字段,将 *.* 设置为复制模式。 Bamboo 现在将收集 HTML 报告。您可以在计划的Clover选项卡中看到它。

It turns out it is possible. There are two major integration tasks: test runner results and code coverage results. I assume normal Python 3 codebase and standard unittest test suite.

Test runner

Bamboo expects test runner results in JUnit XML format. There is separate test runner on the Cheese Shop able to produce such output, but it would require you to write a little code to run it, which is not nice. Better way which keeps the codebase intact is to use pytest's features.

Code coverage

Bamboo only supports the XML format of Atlassian Clover. Important note here is that you don't need Atlassian Clover plugin enabled (and license for it which costs some bucks). Bamboo works on its own.

Python de facto standard code coverage tool, coverage, produces somewhat
Cobertura XML format, but there's a converter. There's a pytest plugin for integration with the coverage tool.

Solution

Here's the Tox environment where I used pytest to make both Bamboo integrations work.

[tox]
envlist   = py34
skipsdist = True

[testenv]
setenv     = LANG=C.UTF-8
basepython = python3.4
deps       = -r{toxinidir}/requirements.txt

[testenv:bamboo]
commands = 
  py.test --junitxml=results.xml \
    --cov=project_name --cov-config=tox.ini --cov-report=xml \
    --cov-report=html project_name/test
    coverage2clover -i coverage.xml -o clover.xml
deps = 
    {[testenv]deps}
    pytest
    pytest-cov
    coverage2clover

# read by pytest
[pytest]
python_files = *.py

# read by coverage
[run]
omit=project_name/test/*,project_name/__main__.py

Note that both pytest and pytest-cov use tox.ini for the configuration that is not supported on command line. It again saves your from having additional clutter in root of your repo. pytest tries to read tox.ini automatically. pytest-cov bypasses to .coveragerc, but because it's also an INI file, tox.ini fits.

On Bamboo side add a script task that runs tox -e bamboo. Then add JUnit parse task to the job. In its dialogue, under Specify custom results directories put results.xml.

Coverage configuration is done other way.

  1. Open Miscellaneous tab of your job
  2. Check Use Clover to collect Code Coverage for this build
  3. Select Clover is already integrated into this build and a clover.xml file will be produced
  4. Type clover.xml into Clover XML Location

enter image description here

At this point in your next build you will see total coverage and two charts: Coverage history and Lines of code history. It's also nice to have interactive HTML produced by coverage tool, so you can drill down to certain line of code.

The settings made above (at least in Bamboo 5.7) has created Clover Report (System) in Artifact job's tab. Open it and set htmlcov to Location field, and *.* to Copy pattern. Bamboo will now collect the HTML reports. You can see it at Clover tab of your plan.

独留℉清风醉 2024-08-12 05:10:10

如果您使用 pytest,则只需使用 py.test --junitxml=/path/to/results/xml/file.xml

If you use pytest you can simply use py.test --junitxml=/path/to/results/xml/file.xml

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文