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
- Visual
- YAML
- In your CI pipeline's Build stage, add a Plugin step.
- Enter a Name and optional Description.
- For Container Registry, select a Docker connector.
- In the Image field, enter
harnesscommunity/publish-nexus-repository:1.1.1
. - Under Optional Configuration, add Settings to configure the Nexus Publisher plugin's properties as described in the following table.
Keys | Type | Description | Value example |
---|---|---|---|
username | String | A username for accessing Nexus Repository Manager. |
|
password | String | An expression referencing a secret containing the password for the specified username. | <+secrets.getValue("nexus_password")> |
server_url | Public URL | The URL of your Nexus Repository Manager instance. | http://34.235.128.201:8081/ |
filename | String | The path to the target artifact that you want to upload. | ./target/example-1.0.jar |
format | String | The repository format. |
|
repository | String | The name of the repository where you want to upload the artifact. | maven-releases |
attributes | String of key-value pairs | Component and asset attributes providing additional artifact metadata. Can be optional. Relevant fields vary by use case. For example, use -CgroupID to map group info. | -CgroupId=org.dronetest -CartifactId=example -Cversion=1.0 -Aextension=jar -Aclassifier=bin |
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.
Keys | Type | Description | Value example |
---|---|---|---|
username | String | A username for accessing Nexus Repository Manager. |
|
password | String | An expression referencing a secret containing the password for the specified username. | <+secrets.getValue("nexus_password")> |
server_url | Public URL | The URL of your Nexus Repository Manager instance. | http://11.222.333.444:8000/ |
filename | String | The path to the target artifact that you want to upload. | ./target/example-1.0.jar |
format | String | The repository format. |
|
repository | String | The name of the repository where you want to upload the artifact. | maven-releases |
attributes | String of key-value pairs | Component and asset attributes providing additional artifact metadata. "-CgroupId=org.dronetest -CartifactId=example -Cversion=1.0 -Aextension=jar -Aclassifier=bin" |
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.
- Visual
- YAML
Configure the Plugin step to use the Artifact Metadata Publisher plugin:
- Name: Enter a name.
- Container Registry: Select a Docker connector.
- Image: Enter
plugins/artifact-metadata-publisher
. - Settings: Add the following two settings as key-value pairs.
file_urls
: 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 asartifact.txt
orurl.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.
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 beplugins/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 asartifact.txt
orurl.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.
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: