Captain
Captain

Reputation: 153

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

I want to run the build_file.py file and write the output to swagger.yml. So to run the file I need to install python. How can I do that?

Upvotes: 1

Views: 2352

Answers (1)

vaizki
vaizki

Reputation: 1957

You can use a different Docker image for each job, so you can split your deployment stage into multiple jobs. In one use the python:3 image for example to run pip and generate the swagger.yml, then define it as an artifact that will be used by the next jobs.

Example (untested!) snippet:

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
    - ...

You could (probably should) also make the swagger generation be part of previous stage and set an expiration for the artifact. See this blog post for example.

Upvotes: 1

Related Questions