如何在 gitlab 作业中安装 python 和 运行 一个 python 文件

How to install python and run a python file in a gitlab job

image : mcr.microsoft.com/dotnet/core/sdk:3.1 

.deploy: &deploy
      before_script:
        - apt-get update -y
      script:
        - cd source/
        - pip install -r requirements.txt
        - python build_file.py > swagger.yml

我想 运行 build_file.py 文件并将输出写入 swagger.yml。所以要 运行 我需要安装的文件 python。我该怎么做?

您可以为每个作业使用不同的 Docker 图像,这样您就可以将部署阶段拆分为多个作业。例如,使用 python:3 图像到 运行 pip 并生成 swagger.yml,然后将其定义为将被下一个作业使用的工件。

示例(未经测试!)片段:

deploy-swagger:
  image: python:3
  stage: deploy
  script:
    - cd source/
    - pip install -r requirements.txt
    - python build_file.py > swagger.yml
  artifacts:
    paths:
      - source/swagger.yml

deploy-dotnet:
  image: mcr.microsoft.com/dotnet/core/sdk:3.1
  stage: deploy
  dependencies:
    - deploy-swagger
  script:
    - ls -l source/swagger.yml
    - ...

您也可以(可能应该)让 swagger 生成成为前一阶段的一部分,并为工件设置过期时间。例如,参见 this blog post