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
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 git@gitlab.example.com: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:
git
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.For the second job:
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.