Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GitLab CI Read File from Other Repository

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?

like image 408
m_callens Avatar asked Dec 24 '22 00:12

m_callens


1 Answers

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
like image 161
Samuel Philipp Avatar answered Dec 29 '22 02:12

Samuel Philipp