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 Found from 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:

yaml
.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_TOKEN

A 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

  1. Add a settings file to your project, for example ci-settings.xml:

    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>
  2. In pom.xml, point distributionManagement at your hosted repository. The id must match the server id in the settings file.

    xml
    <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.

  3. In .gitlab-ci.yml, add a job that extends the hidden job and deploys:

    yaml
    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 ( play ) 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_in to 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:

  1. In your top-level group, create a service account and a personal access token for it with the api scope. The token exchange is a POST request, so read_api is not enough. Because an api token can do anything the service account can do in GitLab, give the service account only the group and project roles it needs.
  2. Assign the service account the Artifact Contributor role on the repositories it publishes to, or Artifact Viewer if the pipeline only installs.
  3. Store the token as a masked and protected CI/CD variable, for example AR_PIPELINE_TOKEN.
  4. In the .artifact-registry hidden job, replace the header line with --header "PRIVATE-TOKEN: $AR_PIPELINE_TOKEN" \.