使用 TravisCI 在 Anaconda 上部署 Python 包

Deploying Python package on Anaconda with TravisCI

我正在尝试使用 TravisCI 在 Anaconda 上部署我的 Python 包。我已经设置了运行良好的 PyPi 部署。这是 travis.yml 文件的相关部分:

deploy:
  provider: pypi
  username: "__token__"
  password:
    secure: ......
  on:
    tags: true
  distributions: "sdist bdist_wheel"
  skip_existing: true

after_deploy:
  - conda install conda-build
  - conda install anaconda-client
  - bash .ci/conda_upload.sh

错误发生在.ci/conda_upload.sh。这里是 conda_upload.sh:

USER=myusername

mkdir ~/conda-bld
conda config --set anaconda_upload no
export CONDA_BLD_PATH=~/conda-bld
ls -l
conda build .

find $CONDA_BLD_PATH/ -name *.tar.bz2 | while read file
do
    echo $file
    anaconda -t $CONDA_UPLOAD_TOKEN upload -u $USER $file --force
done

我收到的错误消息发生在 conda build .,它找不到 setup.py 文件。然而,之前的 ls -l 调用清楚地表明那里有一个 setup.py 文件:

...
-rw-rw-r-- 1 travis travis     1190 Aug 15 09:42 setup.py
...

我也收录了

build:
  script_env:
   - CONDA_BLD_PATH

meta.yaml 文件中。

这是我在 Travis 上获得的完整输出:

WARNING:conda_build.metadata:No numpy version specified in conda_build_config.yaml.  Falling back to default numpy value of 1.11

Adding in variants from internal_defaults

INFO:conda_build.variants:Adding in variants from internal_defaults

Attempting to finalize metadata for pysprint

INFO:conda_build.metadata:Attempting to finalize metadata for pysprint

Collecting package metadata (repodata.json): ...working... done

Solving environment: ...working... done

Collecting package metadata (repodata.json): ...working... done

Solving environment: ...working... done

BUILD START: ['pysprint-0.12.1-py38_0.tar.bz2']

Collecting package metadata (repodata.json): ...working... done

Solving environment: ...working... done

## Package Plan ##
  environment location: /home/travis/conda-bld/pysprint_1597484753718/_h_env_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_pl

The following NEW packages will be INSTALLED:

    # deleted this section to save space

Preparing transaction: ...working... done

Verifying transaction: ...working... done

Executing transaction: ...working... done

Collecting package metadata (repodata.json): ...working... done

Solving environment: ...working... done

/home/travis/conda-bld/pysprint_1597484753718/_h_env_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_pl/bin/python: can't open file 'setup.py': [Errno 2] No such file or directory

source tree in: /home/travis/conda-bld/pysprint_1597484753718/work

export PREFIX=/home/travis/conda-bld/pysprint_1597484753718/_h_env_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_pl

export BUILD_PREFIX=/home/travis/conda-bld/pysprint_1597484753718/_build_env

export SRC_DIR=/home/travis/conda-bld/pysprint_1597484753718/work

Traceback (most recent call last):

  File "/home/travis/miniconda/bin/conda-build", line 11, in <module>

    sys.exit(main())

  File "/home/travis/miniconda/lib/python3.8/site-packages/conda_build/cli/main_build.py", line 474, in main

    execute(sys.argv[1:])

  File "/home/travis/miniconda/lib/python3.8/site-packages/conda_build/cli/main_build.py", line 463, in execute

    outputs = api.build(args.recipe, post=args.post, test_run_post=args.test_run_post,

  File "/home/travis/miniconda/lib/python3.8/site-packages/conda_build/api.py", line 208, in build

    return build_tree(absolute_recipes, config, stats, build_only=build_only, post=post,

  File "/home/travis/miniconda/lib/python3.8/site-packages/conda_build/build.py", line 2859, in build_tree

    packages_from_this = build(metadata, stats,

  File "/home/travis/miniconda/lib/python3.8/site-packages/conda_build/build.py", line 1994, in build

    utils.check_call_env(cmd, env=env, rewrite_stdout_env=rewrite_env,

  File "/home/travis/miniconda/lib/python3.8/site-packages/conda_build/utils.py", line 405, in check_call_env

    return _func_defaulting_env_to_os_environ('call', *popenargs, **kwargs)

  File "/home/travis/miniconda/lib/python3.8/site-packages/conda_build/utils.py", line 385, in _func_defaulting_env_to_os_environ

    raise subprocess.CalledProcessError(proc.returncode, _args)

subprocess.CalledProcessError: Command '['/bin/bash', '-o', 'errexit', '/home/travis/conda-bld/pysprint_1597484753718/work/conda_build.sh']' returned non-zero exit status 2.

问题似乎是环境位置设置为

environment location: /home/travis/conda-bld/pysprint_1597484753718/_h_env_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_pl

但我不知道如何解决这个问题。我对 linux 环境和 shell 脚本编写还很陌生,如果有人能帮助我,我将不胜感激。

编辑: meta.yaml的内容:

{% set name = "pysprint" %}
{% set version = "0.12.1" %}

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

source:
  url: "https://pypi.io/packages/source/{{ name[0] }}/{{ name }}/{{ name }}-{{ version }}.tar.gz"
  sha256: 5fee159c59c81fd31957e23bbd292bcfc1c947583f27eda7f4215594ec898ddd

build:
  script_env:
   - CONDA_BLD_PATH

requirements:
  host:
    - matplotlib
    - numpy >=1.16.6
    - pandas
    - pip
    - python
    - scipy

test:
  imports:
    - pysprint

事实证明,deploy 部分以某种方式扰乱了 conda 部署。我将 after_deploy 部分更改为 before_deploy 并且有效。

before_deploy:
  - conda install conda-build
  - conda install anaconda-client
  - bash .ci/conda_upload.sh

deploy:
  provider: pypi
  username: "__token__"
  password:
    secure: ......
  on:
    tags: true
  distributions: "sdist bdist_wheel"
  skip_existing: true

但是我完全不清楚是什么导致了这个问题。