Skip to main content

Sonatype Platform Plugin for Jenkins - Nexus Repository

The Sonatype Platform Plugin for Jenkins provides the powerful abilities needed to orchestrate your organization's build and release pipelines while also enabling integration and orchestration of the staging functionality in Nexus Repository Manager Pro. Features include:

  • Publishing tagged build outputs to the repository manager.

  • Creating and associating tags to components.

  • Moving/promoting tagged components through a series of staging repositories (e.g. dev, test, qa).

  • Deleting tagged components as needed during post-release cleanup activities.

This integration also works with Lifecycle.png Lifecycle.

Compatibility

  • For staging and tagging functionality, Nexus Repository Manager Pro 3.13 or newer is required along with the Jenkins Plugin 3.3.20181207-134824.d240aa3 or newer.

  • For component upload functionality, Nexus Repository Manager 3.12.1 or newer is required along with the Jenkins Plugin 3.3.20181207-134824.d240aa3 or newer.

Connecting Jenkins to Repository Manager 3

Connect Jenkins to connect to Nexus Repository Manager 3:

  1. Select Manage Jenkins from the Dashboard’s left-navigation menu.

  2. Select Configure System from the list of configuration options.

  3. In the Sonatype Nexus section, click the Add Nexus Repository Manager Server dropdown menu and then select Nexus Repository Manager 3.x Server. Enter the following:

    1. Display Name: Name of the server to show when selecting Nexus Repository Manager instances for build jobs.

    2. Server ID: A unique ID used to reference Nexus Repository Manager in Build Pipeline scripts. It should be alphanumeric without spaces.

    3. Server URL: Location of your Nexus Repository Manager server.

    4. Credentials: Select the Add button to enter your Nexus Repository Manager username and password using the Jenkins Provider Credentials: Jenkins modal window. Once added, select your Nexus Repository Manager username and password from the Credentials dropdown list.

  4. Click the Test Connection button.

  5. After a successful connection to Nexus Repository Manager, click the Save button.

Repeat the above process for each individual repository manager server instance.

Publish / Deploy Components

The following repository formats are supported by the publisher:

  • Maven 2 release repositories

Publishing and Tagging Maven 2 release versioned components

  1. In a Build step section of a Project configuration screen, select Nexus Repository Manager Publisher from the list. Enter the following parameters:

    1. Nexus Instance: Select the display name of a repository manager 3 instance that was pre-configured in Jenkins global configuration.

    2. Nexus Repository: Select a repository that has a release repository policy and allows for artifact uploads. If a snapshot repository is selected as the target for publishing, the build step will fail.Use the maven-deploy-plugin for snapshot builds.

    3. Tag: Enter the tag name to associate with the uploaded component. If a tag with the specified name does not exist an attempt will be made to create the tag during the upload process.

      Note

      Tagging is only available when using a Nexus Repository Pro instance

    4. Packages: Select packages to publish to Nexus Repository Manager during your freestyle build. For this example, use the Add Package dropdown to select a Maven Package

      126656922.png
  2. Once you have finished configuring the step as desired, click Save.

  3. Launch a build for your project. When the build completes, you should see output similar to the following confirming the upload succeeded:

126656923.png

If at the time of publishing the tag specified does not exist, the plugin will create the tag prior to publishing so the tagging action can complete as intended.

Tagging Components

Note

Only available in Sonatype Nexus Repository Pro. Interested in a free trial? Start here.

Create Tag in Repository Manager

  1. In the Build step section of a Project configuration screen, click the Add Build Step dropdown button and then select Create Tag (Nexus Repository Manager 3.x). Enter the following parameters:

    1. Nexus Instance: Select the display name of a repository manager instance that was pre-configured in Jenkins global configuration.

    2. Tag Name: The name to be given to the created tag (e.g. build-xyz)

    3. Tag Attributes File: ( Optional) Additional attributes to include with the tag. Examples could be details about the build, version, or some other detail relevant to your organization's release process or pipeline.

    4. Tag Attributes (JSON): (Optional) Similar to the Tag Attributes File option, this field provides a means for specifying additional attributes to include with the tag as JSON within the Jenkins UI.

      126656924.png
  2. Once you have finished configuring the step as desired, click Save.

  3. Launch a build for your project. When the build completes, you will see output similar to the following:

126656925.png

Associate Tag with Components

  1. In the Build step section of the project configuration screen, click the Add Build Step dropdown button and then select Associate Tag (Nexus Repository Manager 3.x). Enter the following parameters:

    1. Nexus Instance: Select the display name of a repository manager instance that was pre-configured in Jenkins global configuration.

    2. Tag Name: The name to be given to the created tag (e.g. build-xyz)

    3. Search Criteria: One or more Key / Value pairs to search repository manager for the components to associate with the tag.

      The valid Key names are the supported search parameters listed for the Associate Tags REST API.

      Click the Add button in this section to add a new key-value search criteria pair.

      Click the red X icon within each key-value pair section to delete a pair.

      126656926.png
  2. Once you have finished configuring the step as desired, click Save.

  3. Launch a build for your project. When the build completes, you will see output similar to the following:

126656927.png

Staging Workflow

Note

Only available in Sonatype Nexus Repository Pro. Interested in a free trial? Start here.

Move (Promote) Components

  1. In the Build step section of the configuration screen, click the Add Build Step dropdown button and then select Move Components (Nexus Repository Manager 3.x). Enter the following parameters:

    1. Nexus Instance: Select the display name of a repository manager instance that was pre-configured in Jenkins global configuration.

    2. Destination Repository: Select the hosted repository to which the components associated with the tag should be moved.

    3. Tag Name: The tag to be used to identify the set of components to move. It is assumed a tag with the name specified in this field already exists, if it does not the moveaction will fail.

      126656928.png
  2. Once you have finished configuring the step as desired, click Save.

  3. Launch a build for your project. When the build completes, you will see output similar to the following which details the components that were moved:

    126656929.png

Delete Components

  1. In the Build step section of the configuration screen, click the Add Build Step dropdown button and then select Delete Components (Nexus Repository Manager 3.x). Enter the following parameters:

    1. Nexus Instance: Select the display name of a repository manager instance that was pre-configured in Jenkins global configuration.

    2. Tag Name: The tag to be used to identify the set of components to delete. It is assumed a tag with the name specified in this field already exists, if it does not the deleteaction will fail.

      126656930.png
  2. Once you have finished configuring the step as desired, click Save.

  3. Launch a build for your project. When the build completes, you will see output similar to the following which details the components that were deleted:

    126656931.png

Pipeline Staging Workflow Example

Pipeline builds allow for precise control over your build process. In this section, we will continue the examples from above and demonstrate the usage of Jenkins to orchestrate the Nexus Repository Manager Pro staging features within a pipeline to publish, move and delete components.The pipeline script will:

  • Create a tag named build-125 andpublish (and tag with build-125) fancyWidget version 1.1 to the maven-dev repository.

  • Move fancyWidget componentstagged with build-123 to the maven-test repository.

  • Delete fancyWidget components tagged with build-120.

For the sake of this example, we uploaded oldFancyWidget version 1.0 to the maven-releases repository and tagged the component with build-120. We have also created two additional Maven-hosted repositories for use in this example, maven-dev, and maven-test.

The steps that follow will create the script described above:

  1. To start, create a pipeline project by clicking the New Item link on the Jenkins Dashboard.

  2. Give the project a name and click OK to continue with the configuration.

  3. Pipeline jobs allow you to configure the job via a Groovy script. The script is editable in the Pipeline section.

    In the Pipeline section of the configuration screen, we will add several placeholders to get us started. Modify the script to contain following:

    node {
        echo 'Staging Components...'
        
        stage ('Creating build tag') {
        }
        stage ('Publishing') {
        }
        stage ('Move') {
        }
        stage ('Delete') {
        } 
    }

    When complete the Pipeline section should look similar to the following:

    126656932.png

    At this point, the build script contains placeholders for creating a tag, publishing (and tagging) a component, moving components by tag, and deleting components by tag. In the following steps, each of these placeholders will be populated with the appropriate pipeline script.

  4. First, we'll populate the Creating build tag stage.

  5. Click the Pipeline Syntax link located below the Script textbox.

  6. In the Steps section of the Snippet Generator window, select/enter the following:

    1. Sample Step: Select create Tag: Create Tag (Nexus Repository Manager 3.x).

    2. Nexus Instance: Select the display name set in global configuration.

    3. Tag Name: enter build-125.

    4. Attributes: Click the Advanced... button to add some additional attributes. In the text box enter {"createdBy": "JohnSmith"}.

  7. Click the Generate Pipeline Script button. An example pipeline script is shown below:

    createTag nexusInstanceId: 'nx3', tagAttributesJson: '{"createdBy": "JohnSmith"}', tagName: 'build-125'

  8. Copy the generated script and paste it into the Creating build tag stage of your pipeline script.

  9. Completing the Snippet Generator is very similar to completing the Freestyle Build Step configuration. Repeat Steps 7 and 8 for the Publishing, Move, and Delete stages in your pipeline script using the following inputs:

    1. Publishing stage

      126656933.png

      The generated pipeline script should look similar to the following:

      nexusPublisher nexusInstanceId: 'nx3', nexusRepositoryId: 'maven-releases', packages: [[$class: 'MavenPackage', mavenAssetList: [[classifier: '', extension: '', filePath: '/Users/johnsmith/dev/fancyWidgetProject/fancyWidget.jar']], mavenCoordinate: [artifactId: 'fancyWidget', groupId: 'com.mycompany', packaging: 'jar', version: '1.1']]], tagName: 'build-125'
    2. Move stage

      126656934.png

      The generated pipeline script should look similar to the following:

      moveComponents destination: 'maven-test', nexusInstanceId: 'nx3', tagName: 'build-123'
    3. Delete stage

      126656935.png

      The generated pipeline script should look similar to the following:

      deleteComponents nexusInstanceId: 'nx3', tagName: 'build-120'
  10. When we add all the generated scripts into the pipeline configuration window, the completed script should look similar to the following (depending on any changes you may have made to the inputs):

    node {
        echo 'Staging Components...'
        
        stage ('Creating build tag') {
            createTag nexusInstanceId: 'nx3', tagAttributesJson: '{"createdBy" : "JohnSmith"}', tagName: 'build-125'
        }
        stage ('Publishing') {
            nexusPublisher nexusInstanceId: 'nx3', nexusRepositoryId: 'maven-releases', packages: [[$class: 'MavenPackage', mavenAssetList: [[classifier: '', extension: '', filePath: '/Users/johnsmith/dev/fancyWidgetProject/fancyWidget.jar']], mavenCoordinate: [artifactId: 'fancyWidget', groupId: 'com.mycompany', packaging: 'jar', version: '1.1']]], tagName: 'build-125'
        }
        stage ('Move') {
            moveComponents destination: 'maven-test', nexusInstanceId: 'nx3', tagName: 'build-123'
        }
        stage ('Delete') {
            deleteComponents nexusInstanceId: 'nx3', tagName: 'build-120'
        } 
    }
  11. Launch a build for your project.

  12. Once the job has completed you should see something similar to the following in the Jenkins output console (some additional Jenkins boilerplate logging has been removed for brevity):

    Running on Jenkins in /Users/jbryan/.jenkins/workspace/fullPipeline
    Staging Components...
    Successfully created tag: '{"name":"build-125","attributes":{"createdBy":"JohnSmith"}}'
    
    Uploading Maven asset with groupId: com.mycompany artifactId: fancyWidget version: 1.1 To repository: maven-dev
    Successfully Uploaded Maven Assets
    
    Move successful. Destination: 'maven-test' Components moved:
    (group: com.mycompany, name: fancyWidget, version: 1.0)
    
    Delete successful. Components deleted:
    (group: com.mycompany, name: oldFancyWidget, version: 1.0)
    
    Finished: SUCCESS