跳到主要内容

使用 conda-smithy 管理您的 CI

conda-forge,特别是 conda-smithy 包含许多工具,用于在各种不同的平台和架构上构建和部署持续集成 (CI) 基础设施。如果您可以重用所有这些辛勤工作,而无需编写或管理自己的 CI 配置,那岂不是很棒吗?

通过添加 recipe/ 目录到您的存储库,conda-smithy 命令 ci-skeleton 让您连接到经过良好测试且稳健的 CI 基础设施。使用 conda-smithy rerender 命令,您可以使您的存储库与任何需要的更改保持同步。

开始

ci-skeleton 命令通过准备一个具有适当结构的存储库来帮助您入门,以便 rerender 命令将正确添加 CI 配置。让我们看一个例子!

假设您有一个名为 myproj 的项目的存储库。在存储库的根级别,您可以运行以下命令

~/repo $ conda smithy ci-skeleton myproj

这将产生如下输出

Generating ~/repo/conda-forge.yml
Generating ~/repo/recipe/meta.yaml
Updating ~/repo/.gitignore
A CI skeleton has been generated! Please use the following steps
to complete the CI setup process:

1. Fill out recipe/meta.yaml with your install and test code
2. Commit all changes to the repo.

$ git add . && git commit -m "ran conda smithy skeleton"

3. Remember to register your repo with the CI providers.
4. Rerender this repo to generate the CI configurations files.
This can be done with:

$ conda smithy rerender -c auto

At any time in the future, you will be able to automatically update your
CI configuration by re-running the rerender command above. Happy testing!

如您所见,这会生成并更新一些重要文件。它创建的第一个文件是 conda-forge.yml 文件。这专门用于告诉 conda smithy rerender 我们没有将 myproj CI 作为常规 feedstock 运行。.gitignore 被修改为不会意外地将不需要的 conda-smithy 临时文件添加到您的存储库中。

此外,ci-skeleton 输出的步骤对于正确连接所有内容非常重要。幸运的是,它们很容易执行!让我们逐一介绍它们!

1. 填写 recipe/meta.yaml

ci-skeleton 命令发出一个示例 meta.yaml 文件,用于构建 myproj,因此得名“skeleton”(骨架)部分。如果您不希望在 recipe/ 目录中生成骨架,您可以使用 -r 选项来提供替代方案。

meta.yaml 看起来像

{% set name = "myproj" %}
{% set version = environ.get('GIT_DESCRIBE_TAG', 'untagged')|string|replace('-','_') %}

package:
name: {{ name|lower }}
version: {{ version }}

source:
git_url: {{ environ.get('FEEDSTOCK_ROOT', '..') }}

build:
# Uncomment the following line if the package is pure Python and the recipe
# is exactly the same for all platforms. It is okay if the dependencies are
# not built for all platforms/versions, although selectors are still not allowed.
# See https://forge.conda.org.cn/docs/maintainer/knowledge_base.html#noarch-python
# for more details.
# noarch: python

number: {{ environ.get('GIT_DESCRIBE_NUMBER', '0') }}
string: {{ [build_number, ('h' + PKG_HASH), environ.get('GIT_DESCRIBE_HASH', '')]|join('_') }}

# If the installation is complex, or different between Unix and Windows,
# use separate bld.bat and build.sh files instead of this key. By default,
# the package will be built for the Python versions supported by conda-forge
# and for all major OSs. Add the line "skip: True # [py<35]" (for example)
# to limit to Python 3.5 and newer, or "skip: True # [not win]" to limit
# to Windows.
script: "{{ PYTHON }} -m pip install . -vv"

requirements:
build:
# If your project compiles code (such as a C extension) then add the required
# compilers as separate entries here. Compilers are named 'c', 'cxx' and 'fortran'.
- {{ compiler('c') }}
host:
- python
- pip
run:
- python

test:
# Some packages might need a `test/commands` key to check CLI.
# List all the packages/modules that `run_test.py` imports.
imports:
- myproj
# Run your test commands here
commands:
- myproj --help
- pytest
# declare any test-only requirements here
requires:
- pytest
# copy over any needed test files here
source_files:
- tests/

# Uncomment and fill in myproj metadata
#about:
# home: https://github.com/conda-forge/conda-smithy
# license: BSD-3-Clause
# license_family: BSD
# license_file: LICENSE

# Uncomment the following if this will be on a forge
# Remove these lines if this is only be used for CI
#extra:
# recipe-maintainers:
# - BobaFett
# - LisaSimpson

此 recipe 配置为正确地从 git 获取源代码和版本信息。它还桩出添加您可能希望 conda-build 在运行测试套件时使用的任何测试文件。

注意

因为您正在使用 conda-forge、conda-build 等作为您的 CI,所以在此处运行完整的测试套件非常重要。

注意

诸如许可证和维护者之类的元数据可能不太重要,因为在默认情况下,此处创建的软件包永远不会上传到频道。请随意删除或忽略这些字段。

2. 提交更改

一旦您编写了您的 recipe,保存修改非常重要!只需运行以下命令

~/repo $ git add . && git commit -m "ran conda smithy skeleton"

3. 向 CI 提供商注册

这很重要!如果您尚未这样做,则需要转到 CI 提供商(Travis、Circle、Azure 等)并为您的存储库启用 CI。您使用的每个 CI 提供商都将提供有关如何设置它们的文档。

4. 重新渲染

最后但同样重要的是,我们需要生成 CI 配置文件!这基于 recipe 的内容以及在 conda-forge.yml 文件中所做的提供商选择。(请参阅这些文档以获取 CI 提供商的完整列表。)

为了生成 CI 配置文件,请运行

~/repo $ conda smithy rerender -c auto

将这些更改推送到存储库现在应该可以在 CI 上构建和测试您的软件包了!

保持最新

使用 ci-skeleton 的一个主要优点是,一旦设置完成,就可以非常轻松地使您的 CI 系统保持最新。如果您修改您的 recipe 以启用新的架构,您想在不同的提供商上运行,或者即使 CI 系统在您不知情的情况下发生更改,恢复并运行也像重新渲染一样容易。您只需要重复上面的步骤 4

~/repo $ conda smithy rerender -c auto

这将为当前时间和 recipe 的状态生成并替换 CI 配置文件。就这么简单!