Skip to content

Publish from GitLab CI/CD

View as Markdown

A GitLab CI/CD job publishes to CloudRepo the way your laptop does: the build tool sends a repository token with the upload. What changes is where the token lives. Keep it in a masked CI/CD variable, which GitLab gives every job as an environment variable, and let a committed config file name that variable instead of holding the token.

  • A repository, and a repository token that reaches it. A token that publishes is Read + write; a job that only pulls needs Read only.
  • A token you create under Repository Tokens belongs to your account. Deleting a user from your organization deletes that user’s tokens, and any job using one stops authenticating. So create the token as an account that outlives any one person: a user you make for builds (see Create Your First User), signed in to the admin portal when it mints the token. The owner can instead create a service key, which belongs to your organization rather than to a user.
  • Your repository URL, https://your-org.mycloudrepo.io/repositories/your-repo, and the username that Maven, Gradle, Python and Docker clients send: the email address of the account that created the token. npm sends the token alone.
  1. In your GitLab project, click Settings, then CI/CD, expand Variables, and click Add variable.
  2. Set the key to CLOUDREPO_TOKEN and the value to the token. Leave the visibility on Masked, so GitLab hides the value in job logs.

GitLab makes a project variable available to a job as an environment variable named by its key, which is how the files below read it. Three GitLab rules decide whether a job sees the token:

  • Protected variables. A variable marked Protected reaches only pipelines that run on a protected branch or a protected tag. If you protect it, protect the tag your publish job runs on. A pipeline on any other ref gets no CLOUDREPO_TOKEN, and the publish step cannot authenticate.
  • Masking has requirements. GitLab’s include a single line with no spaces, at least 8 characters, and a value that is not the name of another variable. Masking is not encryption: a job that transforms the value before printing it can show it. GitLab’s own page covers the options for a stronger store.
  • Commands print. Keep the token out of command arguments, which logs and process listings can show. The blocks on this page pass it through the environment or a file the tool reads.

See CI/CD variables in GitLab’s documentation.

Pick your client. The choice is remembered on every page of these docs. Each tab has the same parts: a file that names the token’s variable, the repository settings of your build, and the job in .gitlab-ci.yml. Commit them; none holds the token. Each job runs when you push a tag.

Maven reads the credential from a settings file. Commit this one as .mvn/settings.xml; ${env.CLOUDREPO_TOKEN} is replaced by the environment variable when Maven runs. Replace you@example.com with the email address of the account that created the token.

.mvn/settings.xml
<settings>
<servers>
<server>
<id>cloudrepo</id>
<username>you@example.com</username>
<password>${env.CLOUDREPO_TOKEN}</password>
</server>
</servers>
</settings>

The <id> must match the repository’s <id> in your pom.xml. <distributionManagement> is where mvn deploy publishes:

pom.xml
<distributionManagement>
<repository>
<id>cloudrepo</id>
<url>https://your-org.mycloudrepo.io/repositories/your-repo</url>
</repository>
</distributionManagement>

The job runs in a Maven image on a tag pipeline and passes the settings file with --settings:

.gitlab-ci.yml
publish:
image: maven:3.9-eclipse-temurin-17
rules:
- if: $CI_COMMIT_TAG
script:
- mvn --batch-mode --settings .mvn/settings.xml deploy

Expected: the job ends with BUILD SUCCESS, and the artifact is listed in the repository in the admin portal. To resolve dependencies from the same repository in a build, add a <repositories> block with the same URL; see Maven Repositories.

  • 401 Unauthorized. The credential was refused. The username must be the email address of the account that created the token, and the token must not be expired or revoked. Check that the variable is named CLOUDREPO_TOKEN, that this pipeline can see it (a protected variable reaches only protected refs), and that its value is the whole token. The full list is on Repository Tokens.
  • 403 Forbidden. The token does not reach this repository, or it is Read only and the job publishes. Create a Read + write token that includes the repository.
  • 409 Conflict from mvn deploy. The release version you are publishing already exists, and Overwrite Protection refuses to replace it. Publish a new version, or use a -SNAPSHOT version while you iterate. See Maven Overwrite Protection. Python repositories have the same setting: Python Repositories.