Travis CI提供了很多虚拟机环境,但是没有想象中方便。比如要运行Python,只有Linux和macOS是自带的,Windows需要安装。不同的操作系统配置方式也不同,少不了一番折腾。

Travis CI的Python 3.x环境搭建

Linux自带Python,非常方便,只需要设置Python版本即可。CPU可以选择AMD64和ARM64:

jobs:
  include:
    - name: "Python 3.6.0 on AMD64 Linux"
      python: 3.6
      os: linux
      arch: amd64
    - name: "Python 3.7.0 on AMD64 Linux"
      python: 3.7 
      os: linux
      arch: amd64
    - name: "Python 3.8.0 on AMD64 Linux"
      python: 3.8 
      os: linux
      arch: amd64
    - name: "Python 3.9.0 on AMD64 Linux"
      python: 3.9  
      os: linux
      arch: amd64
    - name: "Python 3.6.0 on ARM64 Linux"
      python: 3.6
      os: linux
      arch: arm64
    - name: "Python 3.7.0 on ARM64 Linux"
      python: 3.7 
      os: linux
      arch: arm64
    - name: "Python 3.8.0 on ARM64 Linux"
      python: 3.8 
      os: linux
      arch: arm64
    - name: "Python 3.9.0 on ARM64 Linux"
      python: 3.9  
      os: linux
      arch: arm64

macOS需要设置Xcode来选择对应的镜像。Xcode自带Python,但不清楚具体的版本。可以用python --version来查看。macOS也支持brew安装,但是非常的慢。Python 3.6不支持。

    - name: "Python 3.7 on macOS"
      os: osx
      osx_image: xcode11.4 
      language: shell  
      before_install:
        - python3 --version  
    - name: "Python 3.8 on macOS"
      os: osx
      osx_image: xcode11.6 
      language: shell  
      before_install:
        - python3 --version
    - name: "Python 3.9 on macOS"
      os: osx
      osx_image: xcode12.2 
      language: shell  
      before_install:
        - python3 --version  

Windows上需要通过choco安装Python,然后配置环境变量:

    - name: "Python 3.6.0 on Windows"
      os: windows       
      language: shell   
      before_install:
        - choco install python --version 3.6.0
        - python -m pip install --upgrade pip
      env: PATH=/c/Python36:/c/Python36/Scripts:$PATH
    - name: "Python 3.7.0 on Windows"
      os: windows       
      language: shell   
      before_install:
        - choco install python --version 3.7.0
        - python -m pip install --upgrade pip
      env: PATH=/c/Python37:/c/Python37/Scripts:$PATH
    - name: "Python 3.8.0 on Windows"
      os: windows       
      language: shell   
      before_install:
        - choco install python --version 3.8.0
        - python -m pip install --upgrade pip
      env: PATH=/c/Python38:/c/Python38/Scripts:$PATH
    - name: "Python 3.9.0 on Windows"
      os: windows       
      language: shell   
      before_install:
        - choco install python --version 3.9.0
        - python -m pip install --upgrade pip
      env: PATH=/c/Python39:/c/Python39/Scripts:$PATH

安装Python依赖库。如果你需要OpenCV,注意Windows上要使用opencv-python-headless。不然会返回DLL找不到的错误:

install:
    - if [[ ${TRAVIS_OS_NAME} == "windows" ]]; then
        pip install dbr opencv-python-headless;
      else
        pip3 install dbr opencv-python;
      fi

最后执行测试脚本:

branches:
  only:
    - main

script:
    - python3 ./test.py ./images/1D-1-OverExposure.jpg || python ./test.py ./images/1D-1-OverExposure.jpg

查看任务状态:

在这里插入图片描述

源码

https://github.com/Dynamsoft/python-gui-barcode-reader/blob/main/.travis.yml

最后修改:2023 年 02 月 27 日
如果觉得我的文章对你有用,请随意赞赏