Publish from CircleCI
A CircleCI 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 CircleCI context, which gives every job that names the context an environment variable, and let a committed config file name that variable instead of holding the token.
Before you start
Section titled “Before you start”- A repository, and a repository token that reaches it. A token that publishes is Download and publish packages; a job that only pulls needs Download packages.
- 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 at an email address of its own, not yours (see Create Your First User), signed in to the admin portal when it mints the token. The owner can instead create an organization token, 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.
Store the token in a context
Section titled “Store the token in a context”- In CircleCI, open Org, then Contexts, and click Create Context. Name it
cloudrepo. - Open the context, click Add Environment Variable, name it
CLOUDREPO_TOKEN, and paste the token as the value.
A job that lists context: cloudrepo in the workflow receives CLOUDREPO_TOKEN as an environment
variable, which is how the files below read it. CircleCI can restrict which members may use a
context. See Contexts in CircleCI’s
documentation.
To keep the token with one project instead, open Project Settings, then Environment
Variables, and add CLOUDREPO_TOKEN there. CircleCI hides the value once it is saved, and the
job then needs no context: line. See
Set an environment variable.
Keep the token off the command line, which logs and process listings can show. The blocks on this page pass it through the environment or a file the tool reads.
Publish from a workflow
Section titled “Publish from a workflow”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
.circleci/config.yml. Commit them; none holds the token. Each workflow runs when you push a tag
that starts with release-.
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.
<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:
<distributionManagement> <repository> <id>cloudrepo</id> <url>https://your-org.mycloudrepo.io/repositories/your-repo</url> </repository></distributionManagement>The job runs in CircleCI’s OpenJDK image, which carries Maven, and passes the settings file with
--settings. The workflow gives the job the context and a tag filter:
version: 2.1jobs: publish: docker: - image: cimg/openjdk:17.0 steps: - checkout - run: mvn --batch-mode --settings .mvn/settings.xml deployworkflows: publish: jobs: - publish: context: - cloudrepo filters: tags: only: /^release-.*/ branches: ignore: /.*/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.
Gradle reads the token from the environment in the build script. Replace you@example.com with the
email address of the account that created the token.
publishing { publications { create<MavenPublication>("library") { from(components["java"]) } } repositories { maven { url = uri("https://your-org.mycloudrepo.io/repositories/your-repo") credentials { username = "you@example.com" password = providers.environmentVariable("CLOUDREPO_TOKEN").orNull } } }}.orNull keeps every other Gradle command working on a machine where the variable is not set; only
publish needs it.
Your build also needs the maven-publish plugin. In a Kotlin script, add it to the plugins block at
the top of the file, beside the plugins you have:
plugins { `java-library` `maven-publish`}In a Groovy script, add this line above the publishing block instead:
apply plugin: "maven-publish"Then add this publishing block below that line. Use your own email address in place of you@example.com:
publishing { publications { library(MavenPublication) { from components.java } } repositories { maven { url = "https://your-org.mycloudrepo.io/repositories/your-repo" credentials { username = "you@example.com" password = System.getenv("CLOUDREPO_TOKEN") } } }}
tasks.withType(PublishToMavenRepository).configureEach { doFirst { if (!System.getenv("CLOUDREPO_TOKEN")) { throw new GradleException("Set CLOUDREPO_TOKEN to a CloudRepo repository token, then run publish again.") } }}The tasks block at the end stops publish with a message when CLOUDREPO_TOKEN isn’t set. Every other Gradle command still runs without it.
version: 2.1jobs: publish: docker: - image: cimg/openjdk:17.0 steps: - checkout - run: gradle publishworkflows: publish: jobs: - publish: context: - cloudrepo filters: tags: only: /^release-.*/ branches: ignore: /.*/If your project has the Gradle wrapper, run ./gradlew publish instead.
Send your scope’s packages to this repository; every other package still comes from the public npm registry. Replace @yourscope with your scope.
npm replaces ${NPM_TOKEN} in .npmrc with the environment variable of that name, so this
file can be committed. For npm, add the token as a second variable named NPM_TOKEN, the same way
as CLOUDREPO_TOKEN above. Keep the trailing / on both lines, and keep the second line identical to
the registry URL without https:.
@yourscope:registry=https://your-org.mycloudrepo.io/repositories/your-repo///your-org.mycloudrepo.io/repositories/your-repo/:_authToken=${NPM_TOKEN}npm sends the token on its own, so there is no username to set.
version: 2.1jobs: publish: docker: - image: cimg/node:lts steps: - checkout - run: npm publish --registry https://your-org.mycloudrepo.io/repositories/your-repo/workflows: publish: jobs: - publish: context: - cloudrepo filters: tags: only: /^release-.*/ branches: ignore: /.*/The publish names the repository, so a package without your scope won’t go to the public registry.
The same .npmrc and context serve npm ci and npm install in a job that only pulls. A hosted
repository holds only what was published to it. To install public packages through CloudRepo as well,
create an npm proxy repository and use that as the registry.
twine uploads a built package. It reads its username and password from TWINE_USERNAME and
TWINE_PASSWORD, which the step sets from the context’s variable, and takes the repository URL
without /simple/. Replace you@example.com with the email address of the account that created the
token.
version: 2.1jobs: publish: docker: - image: cimg/python:3.13 steps: - checkout - run: | pip wheel . --no-deps -w dist pip install twine export TWINE_USERNAME=you@example.com export TWINE_PASSWORD="$CLOUDREPO_TOKEN" twine upload --repository-url https://your-org.mycloudrepo.io/repositories/your-repo dist/*workflows: publish: jobs: - publish: context: - cloudrepo filters: tags: only: /^release-.*/ branches: ignore: /.*/To install from the repository in a job, write the ~/.netrc entry from the context’s variable first.
pip finds the login for the host in that file, so the index URL carries none. See
Repository Tokens for the URL. The login is the email
address of the account that created the token.
- run: | (umask 077 && printf 'machine your-org.mycloudrepo.io\nlogin you@example.com\npassword %s\n' \ "$CLOUDREPO_TOKEN" >> ~/.netrc) pip install --index-url https://your-org.mycloudrepo.io/repositories/your-repo/simple/ my-packageLog in to the host alone, with your email address and the token on standard input, then build and
push. The image reference carries the repository after the host. CircleCI’s setup_remote_docker
step is what allows Docker commands to run in the job; it works with the docker executor used
below and not with the machine executor.
version: 2.1jobs: publish-image: docker: - image: cimg/base:stable steps: - checkout - setup_remote_docker - run: | echo "$CLOUDREPO_TOKEN" | docker login your-org.mycloudrepo.io --username you@example.com --password-stdin docker build -t your-org.mycloudrepo.io/repositories/your-repo/my-app:1.0.0 . docker push your-org.mycloudrepo.io/repositories/your-repo/my-app:1.0.0workflows: publish: jobs: - publish-image: context: - cloudrepo filters: tags: only: /^release-.*/ branches: ignore: /.*/Use your own image name and tag. The password is the repository token, not a portal password. More: Docker Repositories.
When publishing fails
Section titled “When publishing fails”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 namedCLOUDREPO_TOKEN, that the job lists the context that holds it, 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 has Download packages access and the job publishes. Create a Download and publish packages token that includes the repository.409 Conflictfrommvn deploy. The release version you are publishing already exists, and Overwrite Protection refuses to replace it. Publish a new version, or use a-SNAPSHOTversion while you iterate. See Maven Overwrite Protection. Python repositories have the same setting: Python Repositories.