16

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?

3 Answers 3

25

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 
Sign up to request clarification or add additional context in comments.

3 Comments

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/<project_id>/repository/files/path%2Fto%2Ffile/raw?ref=master
But my git returns me only 404 not found on such type of address :(
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/<project_id>/repository/files/myfolder%2Flala.txt/raw?ref=master
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.

Comments

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" 

1 Comment

It works probably for public repos, JOB-TOKEN is not supported for accessing private repos: docs.gitlab.com/ee/ci/jobs/ci_job_token.html

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.