获取 Windows Docker 容器 运行 .gitlab-ci.yml

Getting a Windows Docker Container running with .gitlab-ci.yml

我正在尝试使用我的 Gitlab 管道脚本获取 Windows docker 容器 运行 Python3。但是好像下面的yaml配置只启动了一个Linuxdocker容器。如何配置我的 .yml 文件以使用最新版本的 python 启动 Windows 图像?

.gitlab-ci.yml:

image: python:latest

您将获得 linux 版本的 python 容器,因为 GitLab 的共享 运行 用户使用 linux。由于容器的工作方式,它们共享主机的内核,因此 linux 运行ner 不能“托管” windows 容器 - 它根本没有内核说明运行吧。

如果您想要 运行 windows docker 图片,您需要有一个 windows 服务器 with a supported version 您自己托管.您还需要确保您使用的 windows docker 容器正常工作。

所有这些都已经说过了 - 如果您尝试使用 python,只需 运行 在 Linux 中使用它。似乎几乎没有什么理由需要 python 成为 运行 专门针对 windows 的 CI/CD,但如果您让我们知道它们是什么,我们可能能够提供帮助。

还有一个答案,那就是Pywine。它为 python.

模拟 linux 内部的 windows

因此是:

A docker 运行ner 比打开另一个 docker 运行ner 模拟 windows 可以用来解决这个问题。在下方,您会找到我的设置:

到目前为止,这不是最好的设置,但对我来说很管用。作为 docker 图像,我使用 tobix/pywine:3.9。如果您找到更好的方法,请告诉我。我很乐意改进设置。

image: python:3.9

# Change pip's cache directory to be inside the project directory since we can
# only cache local items.
variables:
  PIP_CACHE_DIR: "$CI_PROJECT_DIR/.cache/pip"
  
stages:
  - "Static Code Analysis"
  - "test"
  - "deploy"

cache:
  paths:
    - .cache/pip
    - venv/

before_script:
  - python3.9 -V  # Print out python version for debugging
  - python3.9 -m pip install virtualenv
  - virtualenv venv
  - source venv/bin/activate

Black Linter:
  when: always
  stage: "Static Code Analysis"
  tags:
    - pi
  script:
    - pip install black
    - black  --check --diff ./
  allow_failure: true

Flake Linter:
  when: always
  stage: "Static Code Analysis"
  tags:
    - pi
  script:
    - pip install flake8
    - flake8 --statistics
  allow_failure: true

Type-test:
  when: always
  image: tobix/pywine:3.9
  tags:
    - win-docker
  stage: "Static Code Analysis"
  before_script:
    - . /opt/mkuserwineprefix
    - wine /opt/wineprefix/drive_c/Python39/Python.exe -v
    - wine /opt/wineprefix/drive_c/Python39/Python.exe -m pip install --upgrade pip setuptools
    - wine /opt/wineprefix/drive_c/Python39/Python.exe -m pip install wheel --no-warn-script-location
    - wine /opt/wineprefix/drive_c/Python39/Python.exe -m pip install -Ur requirements/test.txt --no-warn-script-location
  script:
    - wine /opt/wineprefix/drive_c/Python39/Python.exe -m mypy . --warn-redundant-casts --warn-unused-ignores --show-column-numbers --pretty --install-types --non-interactive
  allow_failure: true

test:
  needs: []
  tags:
    - win-docker
  image: tobix/pywine:3.9
  before_script:
    - . /opt/mkuserwineprefix
    - wine /opt/wineprefix/drive_c/Python39/Python.exe -v
    - wine /opt/wineprefix/drive_c/Python39/Python.exe -m pip install --upgrade pip setuptools --no-warn-script-location
    - wine /opt/wineprefix/drive_c/Python39/Python.exe -m pip install wheel --no-warn-script-location
    - wine /opt/wineprefix/drive_c/Python39/Python.exe -m pip install -Ur requirements/test.txt --no-warn-script-location
  script:
    - wine /opt/wineprefix/drive_c/Python39/Python.exe -m pytest test/ --junitxml=/report.xml --cov=./
    - wine /opt/wineprefix/drive_c/Python39/Python.exe -m coverage report
    - wine /opt/wineprefix/drive_c/Python39/Python.exe -m coverage xml
  artifacts:
    when: always
    reports:
      junit: report.xml
      cobertura: coverage.xml

pyinstall:
  stage: deploy
  image: tobix/pywine:3.9
  retry: 2
  tags:
    - win-docker
  before_script:
      - . /opt/mkuserwineprefix
      - wine /opt/wineprefix/drive_c/Python39/Python.exe -v
      - wine /opt/wineprefix/drive_c/Python39/Python.exe -m pip install --upgrade pip setuptools pyinstaller
      - wine /opt/wineprefix/drive_c/Python39/Python.exe -m pip install wheel --no-warn-script-location
      - wine /opt/wineprefix/drive_c/Python39/Python.exe -m pip install -Ur requirements/base.txt --no-warn-script-location

  script:
    - wine /opt/wineprefix/drive_c/Python39/Scripts/pyinstaller.exe main.spec --clean
  artifacts:
    paths:
      - "dist/*.exe"
  rules:
  - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH

请注意,我不需要 运行 在 windows 运行 上的所有东西都放在普通的 docker 容器中以提高效率。