Use Nexus in Pipelines

Nexus is a repository manager that stores, organizes, and distributes artifacts. With Nexus, developers can have better control over the artifacts needed in a development process.

This tutorial demonstrates how to use Nexus in pipelines on KubeSphere.

Prerequisites

Hands-on Lab

Step 1: Get a Repository URL on Nexus

  1. Log in to the Nexus console as admin and click Use Nexus in Pipelines - 图1 on the top navigation bar.

  2. Go to the Repositories page and you can see that Nexus provides three types of repository.

    • proxy: the proxy for a remote repository to download and store resources on Nexus as cache.
    • hosted: the repository storing artifacts on Nexus.
    • group: a group of configured Nexus repositories.
  3. You can click a repository to view its details. For example, click maven-public to go to its details page, and you can see its URL.

Step 2: Modify pom.xml in your GitHub repository

  1. Log in to GitHub. Fork the example repository to your own GitHub account.

  2. In your own GitHub repository of learn-pipeline-java, click the file pom.xml in the root directory.

  3. Click Use Nexus in Pipelines - 图2 to modify the code segment of <distributionManagement> in the file. Set the <id> and use the URLs of your own Nexus repositories.

    modify-pom

  4. When you finish, click Commit changes at the bottom of the page.

Step 3: Modify the ConfigMap

  1. Log in to the KubeSphere web console as admin, click Platform in the upper-left corner, and select Cluster Management.

  2. Select ConfigMaps under Configuration. On the ConfigMaps page, select kubesphere-devops-worker from the drop-down list and click ks-devops-agent.

  3. On the details page, click Edit YAML from the More drop-down menu.

  4. In the displayed dialog box, scroll down, find the code segment of <servers>, and enter the following code:

    1. <servers>
    2. <server>
    3. <id>nexus</id>
    4. <username>admin</username>
    5. <password>admin</password>
    6. </server>
    7. </servers>

    enter-server-code

    Note

    <id> is the unique identifier you set for your Nexus in step 2. <username> is your Nexus username. <password> is your Nexus password. You can also configure a NuGet API Key on Nexus and use it here for better security.

  5. Continue to find the code segment of <mirrors> and enter the following code:

    1. <mirrors>
    2. <mirror>
    3. <id>nexus</id>
    4. <name>maven-public</name>
    5. <url>http://135.68.37.85:8081/repository/maven-public/</url>
    6. <mirrorOf>*</mirrorOf>
    7. </mirror>
    8. </mirrors>

    enter-mirror-code

    Note

    <id> is the unique identifier you set for your Nexus in step 2. <name> is the Nexus repository name. <url> is the URL of your Nexus repository. <mirrorOf> is the Maven repository to be mirrored. In this tutorial, enter * to mirror all Maven repositories. For more information, refer to Using Mirrors for Repositories.

  6. When you finish, click OK.

Step 4: Create a pipeline

  1. Log out of the KubeSphere web console and log back in as project-regular. Go to your DevOps project and click Create on the Pipelines page.

  2. On the Basic Information tab, set a name for the pipeline (for example, nexus-pipeline) and click Next.

  3. On the Advanced Settings tab, click Create to use the default settings.

  4. Click the pipeline name to go to its details page and click Edit Jenkinsfile.

  5. In the displayed dialog box, enter the Jenkinsfile as follows. When you finish, click OK.

    1. pipeline {
    2. agent {
    3. node {
    4. label 'maven'
    5. }
    6. }
    7. stages {
    8. stage ('clone') {
    9. steps {
    10. git 'https://github.com/Felixnoo/learn-pipeline-java.git'
    11. }
    12. }
    13. stage ('build') {
    14. steps {
    15. container ('maven') {
    16. sh 'mvn clean package'
    17. }
    18. }
    19. }
    20. stage ('deploy to Nexus') {
    21. steps {
    22. container ('maven') {
    23. sh 'mvn deploy -DaltDeploymentRepository=nexus::default::http://135.68.37.85:8081/repository/maven-snapshots/'
    24. }
    25. }
    26. }
    27. stage ('upload') {
    28. steps {
    29. archiveArtifacts artifacts: 'target/*.jar', followSymlinks: false
    30. }
    31. }
    32. }
    33. }

    Note

    You need to replace the GitHub repository address with your own. In the command from the step in the stage deploy to Nexus, nexus is the name you set in <id> in the ConfigMap and http://135.68.37.85:8081/repository/maven-snapshots/ is the URL of your Nexus repository.

Step 5: Run the pipeline and check results

  1. You can see all the stages and steps shown on the graphical editing panels. Click Run to run the pipeline.

  2. After a while, you can see the pipeline status shown as Successful. Click the Successful record to see its details.

  3. You can click View Logs to view the detailed logs.

  4. Log in to Nexus and click Browse. Click maven-public and you can see all the dependencies have been downloaded.

    maven-public

  5. Go back to the Browse page and click maven-snapshots. You can see the JAR package has been uploaded to the repository.

    maven-snapshots