Use Artifact Central in CI/CD
- Tier: Premium, Ultimate
- Offering: GitLab.com
- Status: Beta
The availability of this feature is controlled by a feature flag. For more information, see the history.
A CI/CD job can publish to and install from Artifact Central with no stored credential.
The job exchanges its CI_JOB_TOKEN for a short-lived Artifact Central token, then passes that
token to the package manager.
Prerequisites:
- The Artifact Contributor role or higher for the user who runs the job. For more information, see CI/CD job identity.
- A project in your Artifact Central organization. A job in a project outside it gets
404 Organization Not Foundfrom the token exchange.
Exchange the job token
Exchange the job token in a hidden job, then use extends: in each job that needs the token.
In .gitlab-ci.yml, add the hidden job:
.artifact-registry:
before_script:
- |
# Assign before you export, so a failed exchange stops the job.
AR_TOKEN=$(curl -sSf -X POST \
--header "JOB-TOKEN: $CI_JOB_TOKEN" \
"$CI_API_V4_URL/token_exchange?audience=gitlab-artifact-registry&expires_in=3600" \
| sed -E 's/.*"token":"([^"]+)".*/\1/')
# Stops the job in shells without pipefail, such as dash.
test -n "$AR_TOKEN"
export AR_TOKENA job’s own before_script: replaces the one from extends:.
To keep both, use !reference.
Do not put the exchange under default:.
Do not put AR_TOKEN or Maven settings, such as MAVEN_CLI_OPTS, in global variables:.
Jobs that scan execution policies
add to your pipeline inherit both, and fail when they run the exchange in images without curl.
The token cannot outlive the job’s timeout. If the job has less time left than expires_in, the
token expires when the job times out.
Publish a Maven package
Add a settings file to your project, for example
ci-settings.xml:<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"> <servers> <server> <id>artifact-central</id> <username>gitlab-ci-token</username> <password>${env.AR_TOKEN}</password> </server> </servers> </settings>In
pom.xml, pointdistributionManagementat your hosted repository. Theidmust match the serveridin the settings file.<distributionManagement> <repository> <id>artifact-central</id> <url>https://<registry_host>/<registry_handle>/maven/<repository_name></url> </repository> </distributionManagement>For the placeholders, see Configure your client.
In
.gitlab-ci.yml, add a job that extends the hidden job and deploys:publish: extends: .artifact-registry image: maven:3.9-eclipse-temurin-17 script: - mvn --batch-mode -s ci-settings.xml deploy
To install dependencies in the same job, add the repository URL to the repositories section
of pom.xml or to a mirror in the settings file. The same server id and token apply.
CI/CD job identity
The token from the exchange acts as the user who runs the job, not as the project. Artifact Central checks that user’s role on every request.
This user can be different from the one you expect:
- Scheduled pipelines run as the schedule owner. If you select Run scheduled pipeline ( ) to start a schedule manually, it runs as you.
- Retried and manual jobs run as the user who retried or started them.
- Merge request pipelines run as the user who triggered the pipeline.
If that user has no role, publishing returns 404. For more information, see
404 Not Found for a repository you know exists.
With only the Artifact Viewer role, publishing returns 403. For more information, see
403 Forbidden when you publish or delete.
This can happen even if the same pipeline worked for someone else.
Service accounts for pipelines
If many people run your pipelines, the pipeline can have its own identity instead.
A service account is a bot user.
When a job exchanges a service account token instead of CI_JOB_TOKEN, the Artifact Central
token acts as the service account, whoever runs the job.
You assign one role to the service account instead of one role to each person.
A project access token or a group access token works the same way. Each one belongs to a bot user, scoped to its project or group.
A service account token has trade-offs compared to the job token:
- The token is a stored credential, and it expires. The job token stores nothing. Plan to rotate the token.
- Any job that can read the CI/CD variable acts as the service account. A protected variable is available only in pipelines on protected branches and tags, so merge request pipelines on other branches can’t publish with it.
- The job timeout does not cap the Artifact Central token. Only job tokens have that cap.
Set
expires_into the time the job needs.
Set up a service account for a pipeline
Set up a service account so that a pipeline publishes and installs as one identity.
Prerequisites:
- The Owner role for the top-level group.
- The Artifact Admin role.
To set up a service account for a pipeline:
- In your top-level group, create a service account
and a personal access token for it with the
apiscope. The token exchange is aPOSTrequest, soread_apiis not enough. Because anapitoken can do anything the service account can do in GitLab, give the service account only the group and project roles it needs. - Assign the service account the Artifact Contributor role on the repositories it publishes to, or Artifact Viewer if the pipeline only installs.
- Store the token as a masked and protected CI/CD variable,
for example
AR_PIPELINE_TOKEN. - In the
.artifact-registryhidden job, replace the header line with--header "PRIVATE-TOKEN: $AR_PIPELINE_TOKEN" \.