Integrations

TeamCity

How to integrate TeamCity with Cloudsmith

Integrating TeamCity with Cloudsmith enables seamless management of your software packages within your CI/CD pipelines. This integration allows you to push build artifacts directly to Cloudsmith repositories from TeamCity, leveraging TeamCity’s build pipelines for efficient artifact publishing.

Note

The Cloudsmith CLI gives you full control when connecting to any CI/CD process; allowing you to upload any of our support formats or query your repositories. Just configure your API Key, install the CLI, and you'll be all set.

Prerequisites

Before proceeding, ensure you have the following:

  1. TeamCity Server: A functioning installation of TeamCity.
  2. Cloudsmith Account: An active Cloudsmith account with the necessary repository access.
  3. Cloudsmith Credentials: A Cloudsmith API key for authentication, obtainable from your Cloudsmith account under Account > API Settings. Alternatively, use OpenID Connect (OIDC): Set the CLOUDSMITH_OIDC_TOKEN, CLOUDSMITH_ORG, and CLOUDSMITH_SERVICE_SLUG environment variables and the CLI automatically exchanges the OIDC token for a short-lived Cloudsmith token. See OpenID Connect for provider setup.
  4. Build Agent: A configured build agent to execute TeamCity builds.

Integration Steps

Go to Project Build Configuration in TeamCity and add the following build steps:

Step 1: Install Cloudsmith CLI

The Cloudsmith CLI ships as a standalone binary, so no Python or other runtime is required on the build agent. The install script downloads the binary and installs it into a versioned directory, printing the installation result as key=value lines. Parse the executable path from that output and link it in the build checkout directory so later build steps can invoke it without administrator access. This procedure is for Linux agents and requires curl (or GNU wget), tar, and gzip; Windows agents use the install.ps1 script instead.

  • Runner Type: Command Line

  • Custom Script:

    bash
    set -eu
    install_output="$(curl -fsSL https://install.cloudsmith.com/raw/versions/latest/cli.sh | sh -s -- --version 1.20.1)"
    executable="$(printf '%s\n' "$install_output" | sed -n 's/^executable=//p')"
    mkdir -p .cloudsmith-bin
    ln -sf "$executable" .cloudsmith-bin/cloudsmith
  • Execution Policy: If all previous steps finished successfully.

Note

📘 The install script reuses an existing verified installation, so re-running this step on a consistent build agent is fast.

Step 2: Add Cloudsmith API Key Parameter

  1. Navigate to Project Settings > Parameters.
  2. Add the following parameter:
    • Name: env.CLOUDSMITH_API_KEY
    • Kind: Environment variable(env.)
    • Value Type: Password (to ensure security).
    • Value: Your Cloudsmith API key.

Step 3: Push Your Artifacts

  • Runner Type: Command Line

  • Custom Script:

    bash
    ./.cloudsmith-bin/cloudsmith push raw <workspace>/<your-repository> <artifact-path>
  • Replace <workspace>/<your-repository> and <artifact-path> with:

    • Replace <workspace> with your Cloudsmith Workspace slug.
    • Your Cloudsmith Repository Name: For example, my-repository.
    • Artifact Path: Artifact path, such as ./example-artifact.tar.gz.
  • Execution Policy: If all previous steps finished successfully.

Enable Versioned Settings in TeamCity

Optional step

Optional: execute the next step only if you want to track build configuration in your VCS.

  1. In Project Settings, navigate to Versioned Settings.
  2. Configure the following options:
    1. Synchronization: Enable.
    2. Project settings VCS root: Set as per your preference.
    3. Settings format: Choose either from Kotlin or XML.
    4. Settings path in VCS: .teamcity
    5. Allow editing project settings via UI: Enable.
    6. Store passwords and API tokens outside of VCS: Enable.
  3. Apply the changes to ensure your build configuration is tracked in the version control system (VCS).
  4. Allow it to complete and then hit Commit current project settings button and TeamCity will commit the build configuration to your VCS.

Here's a Kotlin Snip for You Reference

kotlin
import jetbrains.buildServer.configs.kotlin.*
import jetbrains.buildServer.configs.kotlin.buildFeatures.perfmon
import jetbrains.buildServer.configs.kotlin.buildSteps.script
import jetbrains.buildServer.configs.kotlin.triggers.vcs

version = "2024.03"

project {

    buildType(TestCloudsmith)
}

object TestCloudsmith : BuildType({
    name = "TestCloudsmith"

    artifactRules = "./"

    params {
        password("env.CLOUDSMITH_API_KEY", "credentialsJSON:4ad9ee86-2dd4-4acf-8070-123e96c647fc")
    }

    vcs {
        root(DslContext.settingsRoot)
    }

    steps {
        script {
            name = "InstallCloudsmith"
            id = "InstallCloudsmith"
            scriptContent = """
                set -eu
                install_output="${'$'}(curl -fsSL https://install.cloudsmith.com/raw/versions/latest/cli.sh | sh -s -- --version 1.20.1)"
                executable="${'$'}(printf '%s\n' "${'$'}install_output" | sed -n 's/^executable=//p')"
                mkdir -p .cloudsmith-bin
                ln -sf "${'$'}executable" .cloudsmith-bin/cloudsmith
            """.trimIndent()
        }
        script {
            name = "CheckCloudsmithAuth"
            id = "CheckCloudsmithAuth"
            scriptContent = "./.cloudsmith-bin/cloudsmith whoami"
        }
        script {
            name = "Cloudsmith Push"
            id = "Cloudsmith_Push"
            scriptContent = "./.cloudsmith-bin/cloudsmith push raw testenv/rawrepo raw-example.tar.gz"
        }
    }

    triggers {
        vcs {
        }
    }

    features {
        perfmon {
        }
    }
})

Best Practices

  • Use Entitlement token Authentication: If you only need to pull packages, use Entitlement tokens for authentication to avoid using long-lived API keys.
  • Secure Secrets: Store sensitive information like API keys/Entitlement tokens as Passwords instead of any other Value type.

Troubleshooting

  • Authentication Issues: Verify the API key is correctly configured as a hidden parameter.
  • Artifact Upload Errors: Ensure the Workspace, repository, and artifact path are correctly specified in the push command.
  • Build Failures: Review the TeamCity build logs for errors in specific steps.