doc/ci/jobs/ci_job_token.md
{{< details >}}
{{< /details >}}
When a CI/CD pipeline job is about to run, GitLab generates a unique token and makes it available
to the job as the CI_JOB_TOKEN predefined variable.
The token is valid only while the job is running. After the job finishes, the token access
is revoked and you cannot use the token anymore.
Use a CI/CD job token to authenticate with certain GitLab features from running jobs. The token receives the same access level as the user that triggered the pipeline, but has access to fewer resources than a personal access token. A user can cause a job to run with an action like pushing a commit, triggering a manual job, or being the owner of a scheduled pipeline. This user must have a role that has the required privileges to access the resources.
You can use a job token to authenticate with GitLab to access another group or project's resources (the target project). By default, the job token's group or project must be added to the target project's allowlist.
If a project is public or internal, you can access some features without being on the allowlist. For example, you can fetch artifacts from the project's public pipelines. This access can also be restricted.
{{< history >}}
{{< /history >}}
CI/CD job tokens can access the following resources:
| Resource | Notes |
|---|---|
| Branches API | Can access the GET /projects/:id/repository/branches endpoint. |
| Commits API | Can access the GET /projects/:id/repository/commits/:sha and GET /projects/:id/repository/commits/:sha/merge_requests endpoints. |
| Container registry | Used as the $CI_REGISTRY_PASSWORD predefined variable to authenticate with the container registry associated with the job's project. |
| Package registry | Used to authenticate with the registry. |
| Terraform module registry | Used to authenticate with the registry. |
| Secure files | Used by the glab securefile command to use secure files in jobs. |
| Container registry API | Can authenticate only with the container registry associated with the job's project. |
| Deployments API | Can access all endpoints in this API. |
| Environments API | Can access all endpoints in this API. |
| Files API | Can access the GET /projects/:id/repository/files/:file_path/raw endpoint. |
| Jobs API | Can access only the GET /job endpoint. |
| Job artifacts API | Can access download endpoints only. |
| Merge requests API | Can access the GET /projects/:id/merge_requests and GET /projects/:id/merge_requests/:merge_request_iid endpoints. |
| Notes API | Can access the GET /projects/:id/merge_requests/:merge_request_iid/notes and GET /projects/:id/merge_requests/:merge_request_iid/notes/:note_id endpoints. |
| Packages API | Can access all endpoints in this API. |
| Pipeline trigger tokens API | Can access only the POST /projects/:id/trigger/pipeline endpoint. |
| Pipelines API | Can access only the PUT /projects/:id/pipelines/:pipeline_id/metadata endpoint. |
| Release links API | Can access all endpoints in this API. |
| Releases API | Can access all endpoints in this API. |
| Repositories API | Can access only the GET /projects/:id/repository/changelog endpoint of public repositories. |
| Tags API | Can access the GET /projects/:id/repository/tags and GET /projects/:id/repository/tags/:tag_name endpoints. |
An open proposal exists to make permissions more granular.
If a job token is leaked, it could potentially be used to access private data accessible to the user that triggered the CI/CD job. To help prevent leaking or misuse of this token, GitLab:
You should also configure your runners to be secure:
privileged mode if the machines are re-used.shell executor when jobs
run on the same machine.An insecure GitLab Runner configuration increases the risk that someone can steal tokens from other jobs.
You can control which groups or projects can use a job token to authenticate and access some of your project's resources.
By default, job token access is restricted to only CI/CD jobs that run in pipelines in your project. To allow another group or project to authenticate with a job token from the other project's pipeline:
If your project is public or internal, some publicly accessible resources can be accessed with a job token from any project. These resources can also be limited to only projects on the allowlist.
GitLab Self-Managed administrators can override and enforce this setting. When the setting is enforced, the CI/CD job token is always restricted to the project's allowlist.
{{< history >}}
:inbound_ci_scoped_job_token feature flag, enabled by default.{{< /history >}}
You can add groups or projects to your job token allowlist to allow access to your project's resources with a job token for authentication. By default, the allowlist of any project only includes itself. Add groups or projects to the allowlist only when cross-project access is needed.
Adding a project to the allowlist does not give additional permissions to the members of the allowlisted project. They must already have permissions to access the resources in your project to use a job token from the allowlisted project to access your project.
For example, project A can add project B to project A's allowlist. CI/CD jobs in project B (the "allowed project") can now use CI/CD job tokens to authenticate API calls to access project A.
Prerequisites:
To add a group or project to the allowlist:
You can also add a group or project to the allowlist with the API.
{{< history >}}
{{< /history >}}
Projects not in the allowlist can use a job token to authenticate with public or internal projects to:
You can limit access to these actions to only the projects on the allowlist by setting each feature to be only visible to project members.
Prerequisites:
To set a feature to be only visible to project members:
{{< details >}}
{{< /details >}}
{{< history >}}
{{< /history >}}
[!warning] It is a security risk to disable the token access limit and allowlist. A malicious user could try to compromise a pipeline created in an unauthorized project. If the pipeline was created by one of your maintainers, the job token could be used in an attempt to access your project.
If you disable the CI/CD job token allowlist, jobs from any project can access your project with a job token. The user that triggers the pipeline must have permission to access your project. You should only disable this setting for testing or a similar reason, and you should enable it again as soon as possible.
This option is only available on GitLab Self-Managed or GitLab Dedicated instances with the Enable and enforce job token allowlist for all projects setting disabled.
Prerequisites:
To disable the job token allowlist:
You can also modify this setting with the GraphQL
(inboundJobTokenScopeEnabled) or REST API.
{{< history >}}
allow_push_repository_for_job_token. Disabled by default.allow_push_repository_for_job_token removed.{{< /history >}}
You can configure your project to allow Git push requests that are authenticated with a CI/CD job token. This setting is turned off by default.
When you turn on this setting, only job tokens generated by CI/CD jobs that run in the project pipelines can push to the project. Job tokens from other projects or groups in the allowlist cannot push to your project.
When you use a job token to push to the project, no CI/CD pipelines are triggered. The job token has the same access permissions as the user who started the job.
If you use the semantic-release tool, this setting might prevent pipeline creation.
[!warning] Do not enable this setting on projects configured as pull mirrors, especially if pipelines are triggered for mirror updates. The upstream repository owner could attempt to use the
CI_JOB_TOKENto push commits to the mirrored project.
Prerequisites:
To grant permission to job tokens generated in your project to push to the project's repository:
You can also control this setting with the ci_push_repository_for_job_token_allowed parameter in
the projects API.
You can use fine-grained permissions to explicitly allow access to a limited set of REST API endpoints.
For more information, see fine-grained permissions for CI/CD job tokens.
You can use the job token to authenticate and clone a repository from a private project
in a CI/CD job. Use gitlab-ci-token as the user, and the value of the job token as the password.
For example:
git clone https://gitlab-ci-token:${CI_JOB_TOKEN}@gitlab.example.com/<namespace>/<project>
You can use this job token to clone a repository even if the HTTPS protocol is disabled by group, project, or instance settings.
You can use a job token to authenticate requests for specific REST API endpoints using these methods:
--header "JOB-TOKEN: $CI_JOB_TOKEN" (recommended)--form "token=$CI_JOB_TOKEN"--data "job_token=$CI_JOB_TOKEN"?job_token=$CI_JOB_TOKEN (not recommended)For example, using the recommended header method:
curl --verbose --request POST --header "JOB-TOKEN: $CI_JOB_TOKEN" --form ref=master "https://gitlab.com/api/v4/projects/1234/trigger/pipeline"
For token security guidance, see security considerations.
You cannot use job tokens to authenticate GraphQL requests.
{{< history >}}
{{< /history >}}
You can track which other projects use a CI/CD job token to authenticate with your project in an authentication log. To check the log:
The authentication log displays a maximum of 100 authentication events. If the number of events is more than 100, download the CSV file to view the log.
New authentications to a project can take up to 5 minutes to appear in the authentication log.
{{< history >}}
{{< /history >}}
Beginning in GitLab 19.0, CI/CD job tokens use the JWT standard by default. Projects can continue to use the legacy format by configuring the top-level group for their project. This setting is only available until the GitLab 20.0 release.
To use the legacy format for your CI/CD tokens:
Your CI/CD tokens now use the legacy format. If you want to use the JWT format again later, you can re-enable this setting.
CI job token failures are usually shown as responses like 404 Not Found or similar:
Unauthorized Git clone:
$ git clone https://gitlab-ci-token:[email protected]/fabiopitino/test2.git
Cloning into 'test2'...
remote: The project you were looking for could not be found or you don't have permission to view it.
fatal: repository 'https://gitlab-ci-token:[MASKED]@gitlab.com/<namespace>/<project>.git/' not found
Unauthorized package download:
$ wget --header="JOB-TOKEN: $CI_JOB_TOKEN" ${CI_API_V4_URL}/projects/1234/packages/generic/my_package/0.0.1/file.txt
--2021-09-23 11:00:13-- https://gitlab.com/api/v4/projects/1234/packages/generic/my_package/0.0.1/file.txt
Resolving gitlab.com (gitlab.com)... 172.65.251.78, 2606:4700:90:0:f22e:fbec:5bed:a9b9
Connecting to gitlab.com (gitlab.com)|172.65.251.78|:443... connected.
HTTP request sent, awaiting response... 404 Not Found
2021-09-23 11:00:13 ERROR 404: Not Found.
Unauthorized API request:
$ curl --verbose --request POST --form "token=$CI_JOB_TOKEN" --form ref=master "https://gitlab.com/api/v4/projects/1234/trigger/pipeline"
< HTTP/2 404
< date: Thu, 23 Sep 2021 11:00:12 GMT
{"message":"404 Not Found"}
< content-type: application/json
While troubleshooting CI/CD job token authentication issues, be aware that:
semantic-release tool and job tokensThere is a known issue if you use the semantic-release tool with the Allow Git push requests to the repository setting.
When enabled:
For more information, see the issue 891.
There are some known issues with the JWT format for CI/CD job tokens.
Error when persisting the task ARN. error with EC2 Fargate Runner custom executorThere is a bug
in version 0.5.0 and earlier of the EC2 Fargate custom executor. This issue causes this error:
Error when persisting the task ARN. Will stop the task for cleanupTo fix this issue, upgrade to version 0.5.1 or later of the Fargate custom executor.
invalid character '\n' in string literal error with base64 encodingIf you use base64 to encode job tokens, you could receive an invalid character '\n' error.
The default behavior of the base64 command wraps strings that are longer than 79 characters.
When base64 encoding JWT format job tokens during job execution, for example with
echo $CI_JOB_TOKEN | base64, the token is rendered invalid.
To fix this issue, use base64 -w0 to disable automatically wrapping the token.
403 Forbidden in long-running jobsWhen using JWT format job tokens in GitLab 18.8 and earlier, a job could fail with
a 403 Forbidden error. This can happen in:
needs.The error typically appeared in runner logs as:
WARNING: Submitting job to coordinator... job failed
code=403 job=<job_id> status=PUT https://gitlab.com/api/v4/jobs/<job_id>: 403 Forbidden
Update to GitLab 18.9 to avoid this issue.