13

In the setup that I have, there are two repositories in GitLab, one of which has a version file that the other needs for naming the artifact produced by its CI/CD pipeline.

Right now, I'm just cloning the entire other repository to access that VERSION file. I tried using git archive to pull only the VERSION file but the CI_JOB_TOKEN doesn't work with SSH access remotes (from my testing), and doing a curl to the raw file path doesn't work because its on a private GitLab instance.

Is there a better way to do this?

m_callens
  • 6,100
  • 8
  • 32
  • 54

3 Answers3

19

I had the same Problem and solved it by using an access token. Go to User Settings > Access Tokens and create one:

Generate access token

Using that you then can pull files from all repositories via gitlab-api.

wget --header "PRIVATE-TOKEN: <your_token>" http://mygitlab.com/api/v4/projects/<project_id>/repository/files/path%2Fto%2Ffile/raw?ref=master

To pull that file in the GitLab CI you can set your access token as environment variable. Go to > Settings > CI > Environment variables and add GITLAB_TOKEN with your access token:

add ci environment variable

You now can use that environment variable in your CI script to download that file with wget or curl if you prefer.

wget --header "PRIVATE-TOKEN: ${GITLAB_TOKEN}" http://mygitlab.com/api/v4/projects/<project_id>/repository/files/path%2Fto%2Ffile/raw?ref=master

or

curl --header "PRIVATE-TOKEN: ${GITLAB_TOKEN}" http://mygitlab.com/api/v4/projects/<project_id>/repository/files/path%2Fto%2Ffile/raw?ref=master
Samuel Philipp
  • 10,631
  • 12
  • 36
  • 56
  • 7
    Great answer, thank you. Just wanted to add that you can use the baked-in CI variable `$CI_API_V4_URL` if the upstream project is on the same instance, like this: `curl --header "PRIVATE-TOKEN: ${GITLAB_TOKEN}" ${CI_API_V4_URL}/projects//repository/files/path%2Fto%2Ffile/raw?ref=master` – Nathan Wallace Aug 31 '20 at 19:32
  • 2
    But my git returns me only 404 not found on such type of address :( – Tadeusz Nov 16 '21 at 09:36
  • 1
    I must add that in paths to files all slashes must be escaped with %2F. So, if your file path is 'myfolder/lala.txt", then url is: ${CI_API_V4_URL}/projects//repository/files/myfolder%2Flala.txt/raw?ref=master – Tadeusz Nov 16 '21 at 09:45
1

A "not very nice" way I do it is putting a registered in my GitLab user settings SSH key in the container that runs the job which gives me access to all repos I have privileges to.

If you need just one file from the other repo thing that comes to mi mind (although I wouldn't call it a great solution either) is saving this file on GitLab pages in the "other" repo in CI and then just getting it with curl.

Izydorr
  • 1,926
  • 3
  • 23
  • 40
0

For HTTP/s still the option "Allow access to this project with a CI_JOB_TOKEN" under CI/CD>Settings ready in the remote repo allowing source one, and using the header JOB-TOKEN:$CI_JOB_TOKEN seems suitable,

before_script:
  - mkdir -p /from/other/repo
  - curl --output /from/other/repo/file1.txt --header "JOB-TOKEN:$CI_JOB_TOKEN" "${CI_API_V4_URL}/projects/<nnnn>/repository/files/the%2Fpath%2Ffile1.txtyaml/raw?ref=master"
jalbertoa
  • 58
  • 7