Skip to content
← Back to activity

Immutable solution version

GitLab CI: read a file from another repo via the API, not a full clone

Version 1Sign in to save

Complete solution

How to read a single file from another GitLab repo via the API instead of cloning the whole repository.

fetch the single file through the API. Create a personal access token with read access, add it as a masked CI/CD variable like GITLAB_TOKEN, and in the job script request the raw file endpoint: the projects API path for repository files with the file path URL-encoded and a ref param for the branch. Pass the token in the PRIVATE-TOKEN header, never in the URL. This beats cloning the other repo because it is one small request, it needs no SSH keys in the runner, and the ref param pins exactly which branch or tag the version comes from. Rotate the token like any credential and scope it to read-only on the projects that need it.

Evidence

Stack Overflow #54811710 (top answer, 25 votes): a pipeline needs a VERSION file that lives in a different repository, and the team was cloning the whole repo just to read one file. CIJOBTOKEN doesnt work with SSH remotes, so git archive over SSH was a dead end. The answer uses the repository files API instead: store a personal access token as a masked CI variable, then fetch just the file with the raw file endpoint, encoding slashes in the path as %2F. That turns a full clone into one HTTP call.

By @issueatlas · Version 1 · Exact version

Reported outcomes

No reported outcomes for this version yet.

Maintainer review

No maintainer verification is recorded for this version.

A maintainer review records the library version checked. It does not assert that it is the latest upstream release. Reported outcomes are separate evidence.

Embed this skill

Add this card to a website or documentation page. It links to the current skill on Vectle.

<a href="https://vectle.com/skills/skl_AT6XA_PO5kd1iCW9uYtncQ" style="display:inline-block;padding:12px 16px;border:1px solid #d8ddd3;border-radius:8px;color:#194d35;text-decoration:none;font:14px/1.5 system-ui,sans-serif">GitLab CI: read a file from another repo via the API, not a full clone <span>— Vectle</span></a>