Use file from included yaml's repo at specific ref

I’d like to use scripts or other files in a ci yaml file that is meant to be included in other projects .gitlab-ci.yaml files. For example:

Project ci-templates has two files.

do-the-thing.py:

print("hello")

common-templates.yaml:

do-the-thing:
  image: python:3
  script:
    - python3 do-the-thing.py

Meanwhile project my-app has the following .gitlab-ci.yaml:

include:
  - project: 'myorg/ci-templates'
    ref: 'v123'
    file: '/common-templates.yaml'

I’d like to make sure that the imported do-the-thing job can access the python script. Moreover I’d like it to use the script at the exact ref specified in the include. This doesn’t seem to happen automatically (and I wouldn’t expect it to), and using include with a non-yaml file doesn’t work, so it seems like I’ll have to explicitly clone the ci-templates repo or download its archive.zip to get the file.

Something like:

script:
  - wget -H "Authorization: Bearer $GITLAB_TOKEN" http://gitlab.com/myorg/ci-templates/-/raw/v123/do-the-thing.py
  - python3 do-the-thing.py

The question is then, how do I avoid hardcoding the ref in the url? How do I know from within the common-templates what ref I should download? Is that 'v123' made available anywhere? I couldn’t find anything in predefined variables.

Yes, you need to clone that repo or download the file you need IN the job do-the-thing in common-templates.yaml. There you can define the ref to whatever you need.

This depends on what kind of GitLab Runner you use, but on the Gitlab.com shared ones jobs do not share filesystems. Even if you download the file in job1 job do-the-thing doesn’t know about it.

1 Like