Search code examples
gitlabincludegitlab-ci

How to include a script.py on my gitlab-ci.yml?


I am implementing a gitlab-ci.yml for my project . in this yml file I will need to execute a script.py file . this script.py is located on a differnet project , Is there anyway to include this python script without uploading it to my project?

Something like:

include: 'https://gitlab.com/khalilazennoud3/<project-name>/-/blob/main/script.py

Solution

  • There's no way to 'include' a file that isn't a Pipeline definition template, but you can still grab that file. The way I'd do it is to add a second Pipeline job in a prior stage to clone that other repository, then upload the file you need as an artifact. Then in the job where you need the file, it will have the artifact available.

    Here's an example pipeline with just these two Jobs:

    stages:
      - "Setup other Project Files" # or whatever
      - Build
    
    Grab Python Script from Other Repo:
      stage: "Setup other Project Files"
      image: gitscm/git
      variables:
        GIT_STRATEGY: none
      script:
        - git clone [email protected]:user/project.git
      artifacts:
        paths:
          - path/to/script.py.
        when: on_success # since if the clone fails, there's nothing to upload
        expire_in: 1 week # or whatever makes sense
    
    Build Job:
      stage: Build
      image: python
      dependencies: ['Grab Python Script from Other Repo']
      script:
        - ls -la # this will show `script.py` from the first step along with the contents of "this" project where the pipeline is running
        - ./do_something_with_the_file.sh
    

    Let's go through these line by line. For the first job:

    1. We're using the Git image since all we need here is git
    2. The GIT_STRATEGY: none variable tells the Gitlab Runner not to clone/fetch the project the pipeline is running for. This is super useful if the job is doing things like sending notifications to Slack, hitting another API, etc.
    3. For the script, all we do is clone the other project so that we can upload the file as an artifact.

    For the second job:

    1. Use whatever image you're using for this job as normal
    2. The dependencies keyword controls which artifacts from previous stages will be 1) required and 2) downloaded for this specific job. By default, all available artifacts are downloaded for all jobs. This keyword controls that since we only need the script.py file.
    3. In the script we just make sure that the file is present, which is just a temporary thing anyway, then you can use it however you need to.