我正在嘗試使用我的 Gitlab 管道腳本運行 Python3 的 Windows docker 容器。但是好像下面的yaml配置只啟動了一個Linux docker容器。如何配置我的 .yml 檔案以使用最新版本的 python 啟動 Windows 映像?
.gitlab-ci.yml:
image: python:latest
uj5u.com熱心網友回復:
由于 GitLab 的共享運行程式使用 linux,因此您將獲得 linux 版本的 python 容器。由于容器的作業方式,它們共享主機的內核,因此 Linux 運行器不能“托管”Windows 容器——它根本沒有運行它的內核指令。
如果您想運行 Windows docker 映像,您將需要一個 Windows 服務器,該服務器具有您自托管的受支持版本。您還需要確保您使用的 windows docker 容器正常作業。
所有這一切都已經說過了 - 如果您嘗試使用 python,只需在 Linux 中運行它。似乎幾乎沒有什么原因需要 Python 在 Windows 上專門為 CI/CD 運行,但是如果您讓我們知道它們是什么,我們可能會提供幫助。
uj5u.com熱心網友回復:
還有另一個答案,那就是Pywine。它為 python 模擬了 linux 內的 windows。
因此是:
一個 docker runner 然后打開另一個 docker runner 來模擬可以用來解決這個問題的視窗。在下面你會找到我的設定:
到目前為止,這不是最好的設定,但它對我有用。作為 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 容器中運行以提高效率。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/373553.html
