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?
I had the same Problem and solved it by using an access token. Go to User Settings > Access Tokens and create one:
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:
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With