Skip to main content

Jenkins (Nexus Repository 2)

Nexus Repository 2

Jenkins is a powerful and widely used open source continuous integration server providing development teams with a reliable way to monitor changes in source control and trigger a variety of builds.

Installation

Nexus Repository Manager for Jenkins is distributed as a Hudson plugin package ( .hpi file) and is available for download from Sonatype Support.

To install Nexus Repository Manager for Jenkins, perform the following steps:

  1. Login to Jenkins as an administrator

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

  3. Select Manage Plugins from the list of configuration options

  4. Click the Advanced tab on the Plugin Manager screen

  5. In the Upload Plugin section, click Choose File , open the .hpi file, and then click Upload

5410367.png

Figure 4.4. Nexus Repository Manager for Jenkins Upload

Global Configuration

Use the following instructions to configure Jenkins to connect to Nexus Repository Manager:

  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 2.x Server. Enter the following:

    1. Display Name : Name of the server you want shown 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

5410366.png

Figure 4.5. Nexus Repository Manager for Jenkins Global Configuration

Tool Configuration (Optional)

Note

Nexus Repository Manager for Jenkins can upload artifacts to a Maven repository without a Maven installation, however the examples provided in this section use Maven to build in a project. A Maven installation must be configured in Jenkins for these examples to work.

To configure a Maven installation:

  1. Select Manage Jenkins from the left-navigation menu

  2. Select Global Tool Configuration from the list of configuration options

  3. In the Maven section, click the Maven Installations… button

  4. Configure a Maven installation. An example configuration is shown in Figure 4.6, “Nexus Jenkins Tool Configuration”:

5410365.png

Figure 4.6. Nexus Jenkins Tool Configuration

Job Configuration

After a completed installation and global configuration of Jenkins, you are ready to configure a build-step invocation as part of a specific job.

Freestyle Build Step Configuration

The freestyle build job is a flexible and configurable option, and can be used for any type of project.

  1. To start, create a freestyle project by clicking the New Item link on the Jenkins Dashboard. Give the project a name and click OK to continue with the configuration.

  2. In the Build section of the configuration screen, click the Add Build Step dropdown button and then select Nexus Repository Manager Publisher. Enter the following parameters:

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

    2. Nexus Repository: Select a repository that has release repository policy and allows for artifact uploads.

    3. 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.

      5410364.png

      Figure 4.7. Nexus Jenkins Plugin Freestyle Build Configuration

  3. Complete your freestyle build as desired and click Save.

  4. Launch a build for your project. When the build completes, you will see output similar to Figure 4.8, “Nexus Jenkins Plugin Freestyle Build Output”:

5410363.png

Figure 4.8. Nexus Jenkins Plugin Freestyle Build Output

In Nexus Repository Manager, any artifact selected in Packages will be available in the selected repository. In Figure 4.9, “Jenkins war in Release Repo”, the Jenkins war is now available in the Releases repository:

5410362.png

Figure 4.9. Jenkins war in Release Repo

Pipeline Build Step Configuration

Note

Pipeline jobs allow you to configure the job via a Groovy script. The script is editable in the Pipeline section. In this example we will use the sample Github + Maven script.

Pipeline builds allow for precise control and configuration over your build process.

  1. To start, create a pipeline project by clicking the New Item link on the Jenkins Dashboard. Give the project a name and click OK to continue with the configuration.

  2. In the Pipeline section of the configuration screen, click the try sample Pipeline dropdown, and then select Github + Maven.

    5410361.png

    Figure 4.10. Sample Pipeline

  3. Below the Results stage, add a new placeholder Nexus Repository Manager Publish stage to the build pipeline script.

    stage('Publish') {
    
    
    }

    Note

    The Nexus Repository Manager Publisher build step should occur after the build so the binaries are available for upload. For best reporting during the build process, the publish step should exist in its own stage below the Results stage.

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

  5. In the Steps section of the Snippet Generator window, select the following:

    1. Sample Step: Select NexusPublisher: Nexus Repository Manager Publisher .

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

    3. Nexus Repository: Select a repository that has release repository policy and allows for artifact uploads.'

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

      5410360.png

      Figure 4.11. Pipeline Sample Script

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

    nexusPublisher nexusInstanceId: 'localNexus', nexusRepositoryId: 'releases', packages: [[$class: 'MavenPackage', mavenAssetList: [[classifier: '', extension: '', filePath: 'war/target/jenkins.war']], mavenCoordinate: [artifactId: 'jenkins-war', groupId: 'org.jenkins-ci.main', packaging: 'war', version: '2.23']]]
  7. Copy the generated script and paste it into the previously added publish stage of your pipeline script.

  8. Complete your pipeline build as desired and click Save.

  9. Launch a build for your project.

In Nexus Repository Manager, any artifact selected in Packages will be available in the selected repository. Same as "Figure 4.9, “Jenkins war in Release Repo” from the Freestyle Build Step Configuration section, the Jenkins war is now available in the Releases repository.