Skip to main content

Upload artifacts to Sonatype Nexus

You can use the Nexus Publish plugin in your CI pipelines to upload artifacts to Sonatype Nexus Repository Manager.

You need:

  • Access to a Sonatype Nexus Repository Manager instance.
  • A CI pipeline with a Build stage.
  • Steps in your pipeline that generate artifacts to upload, such as by running tests or building code. The steps you use depend on what artifacts you ultimately want to upload.

You can also upload artifacts to S3, upload artifacts to GCS, and upload artifacts to JFrog. For other upload locations, you can use a script in a Run step.

Use the Nexus Publish plugin

The following YAML example describes a Plugin step in a CI stage that uploads an artifact to Sonatype Nexus.

              - step:
type: Plugin
name: upload_sonatype
identifier: upload_sonatype
spec:
connectorRef: account.harnessImage ## Docker Hub container registry connector
image: harnesscommunity/publish-nexus-repository:1.1.1
settings:
username: deploy-user ## Nexus Repository Manager username
password: <+secrets.getValue("nexus_password")> ## Nexus Repository Manager password
server_url: http://34.235.128.201:8081/ ## Nexus Repository instance URL
filename: ./target/example-1.0.jar ## Path to the artifact to upload
format: maven2 ## Repository format
repository: maven-releases ## Destination repository name
attributes: "-CgroupId=org.dronetest -CartifactId=example -Cversion=1.0 -Aextension=jar -Aclassifier=bin" ## Key-value pairs providing additional metadata

Plugin step specifications

  • type: Plugin
  • name: Specify a step name.
  • identifier: Specify a unique step ID.
  • connectorRef: Specify a Docker connector.
  • image: harnesscommunity/publish-nexus-repository:1.1.1
  • settings: Configure the Nexus Publisher plugin's properties as described in the following table.
KeysTypeDescriptionValue example
usernameStringA username for accessing Nexus Repository Manager.
  • admin
  • test-user
passwordStringAn expression referencing a secret containing the password for the specified username.<+secrets.getValue("nexus_password")>
server_urlPublic URLThe URL of your Nexus Repository Manager instance.http://11.222.333.444:8000/
filenameStringThe path to the target artifact that you want to upload../target/example-1.0.jar
formatStringThe repository format.
  • maven2
  • raw
repositoryStringThe name of the repository where you want to upload the artifact.maven-releases
attributesString of key-value pairsComponent and asset attributes providing additional artifact metadata. "-CgroupId=org.dronetest -CartifactId=example -Cversion=1.0 -Aextension=jar -Aclassifier=bin"
Tips

You can use variable expressions for Settings values. For example, password: <+stage.variables.nexus_password> uses a stage variable.

Create text secrets for sensitive information, such as passwords.

View artifacts on the Artifacts tab

You can use the Artifact Metadata Publisher plugin to publish artifact URLs on the Artifacts tab. This makes it easier to find artifacts associated with specific builds. To do this, add another Plugin step after the Nexus Publisher plugin step.

Add a Plugin step that uses the artifact-metadata-publisher plugin.

               - step:
type: Plugin
name: publish artifact metadata
identifier: publish_artifact_metadata
spec:
connectorRef: account.harnessImage
image: plugins/artifact-metadata-publisher
settings:
file_urls: https://complete/url/to/artifact/on/nexus
artifact_file: artifact.txt
  • connectorRef: Use the built-in Docker connector (account.harness.Image) or specify your own Docker connector.
  • image: Must be plugins/artifact-metadata-publisher.
  • file_urls: Provide the URL to the artifact that was uploaded by the Nexus Publisher plugin. If you uploaded multiple artifacts, you can provide a list of URLs.
  • artifact_file: Provide any .txt file name, such as artifact.txt or url.txt. This is a required setting that Harness uses to store the artifact URL and display it on the Artifacts tab. This value is not the name of your uploaded artifact, and it has no relationship to the artifact object itself.

Build logs and artifact files

When you run the pipeline, you can observe the step logs on the build details page.

If the Nexus Publisher plugin step succeeds, you can find the artifact in your Sonatype Nexus repo. You can use the Artifact Metadata Publisher plugin to view artifacts on the Artifacts tab.

tip

On the Artifacts tab, select the step name to expand the list of artifact links associated with that step.

If your pipeline has multiple steps that upload artifacts, use the dropdown menu on the Artifacts tab to switch between lists of artifacts uploaded by different steps.

Pipeline YAML example

This example pipeline has steps that build an artifact, upload it to a Sonatype Nexus repo, and then use the Artifact Metadata Publisher plugin to show a link to the artifact on the Artifacts tab.

pipeline:
name: YOUR_PIPELINE_NAME
identifier: YOUR_PIPELINE_ID
projectIdentifier: default
orgIdentifier: default
tags: {}
properties:
ci:
codebase:
connectorRef: YOUR_CODEBASE_CONNECTOR
repoName: YOUR_CODE_REPO
build: <+input>
stages:
- stage:
name: stage1
identifier: stage1
type: CI
spec:
cloneCodebase: true
execution:
steps:
- step:
type: Run
name: Build
identifier: build
spec:
shell: Sh
command: "mvn clean package"
- step:
type: Plugin
name: upload_nexus
identifier: upload_nexus
spec:
connectorRef: account.harnessImage
image: harnesscommunity/publish-nexus-repository:1.1.1
settings:
username: test-user
password: <+secrets.getValue("nexus_password")>
server_url: http://11.222.333.444:8000/
format: maven2
filename: ./target/example-1.0.jar
repository: maven-releases
attributes: "-CgroupId=org.dronetest -CartifactId=example -Cversion=1.0 -Aextension=jar -Aclassifier=bin"
- step:
type: Plugin
name: publish artifact metadata
identifier: publish_artifact_metadata
spec:
connectorRef: account.harnessImage
image: plugins/artifact-metadata-publisher
settings:
file_urls: https://repository.sonatype.org/content/sites/...
artifact_file: artifact.txt
platform:
os: Linux
arch: Amd64
runtime:
type: Cloud
spec: {}

Troubleshoot uploading artifacts

Go to the CI Knowledge Base for questions and issues related uploading artifacts, such as: