How to Create Post Request Api Call to Upload Artifact to Nexus Repo

In this guide, we will utilise Jenkins every bit a Continuous Integration server and Nexus equally a build repository.

The end goal of this guide is to create a flow, where we can build, store, organize and monitor the compiled artifacts by Maven through our CI server.

Before we begin, make sure that you already have Jenkins upwardly and running. In example you lot haven't prepare upward Jenkins even so, and then copy the below command and run information technology on your Docker enabled host/machine.

          $ docker run -d --name jenkins-ci -p 8080:8080 jenkins/jenkins:lts                  

Once, Jenkins container is configured on your local/remote machine. Get to your preferred browser and open the URL http:///your-ip-addr:8080.

On the very first page, Jenkins will enquire you lot for the admin password, which you can find by running beneath mentioned control in your terminal:

          $  docker exec -i jenkins-ci cat /var/jenkins_home/secrets/initialAdminPassword  b5102c8d9fa245dbb0b8da03f504d3a5                  

Follow the guided steps to finish the configuration. Save the username and countersign securely for futurity use.

Installing Nexus:

Nexus is a repository manager that allows yous to store and retrieve artifacts. Information technology enables yous to host your built artifacts in a private and secure repo. You can always pull the Nexus Docker image using the following command:

          $ docker pull sonatype/nexus3   Using default tag: latest latest: Pulling from sonatype/nexus3 cb3c77f9bdd8: Pull complete  fd8daf2668d1: Pull complete  fd1ff82b00e8: Pull complete  2a05f7b573af: Pull complete  Digest: sha256:6570855dfbc3eb094fe5cbbacec87aa8b91d16394dab627177e1deeebb5ac8ee Condition: Downloaded newer image for sonatype/nexus3:latest docker.io/sonatype/nexus3:latest                  

At present it's time to run downloaded sonatype/nexus on the default port 8081. Follow the below-mentioned commands:

          $ docker run -d --proper noun nexus_repo -p 8081:8081 sonatype/nexus3                  

Ordinarily it takes 1 to 2 minutes for the Nexus service to launch in your newly created Docker container. If you wish to follow the log to see if Nexus is up and prepare, and then run the following control:

          $ docker logs nexus_repo -f                  

In logs you will meet a message stating Started Sonatype Nexus OSS 3.20.1-01. Information technology means your Nexus Repository Manager is fix to use. Now get to your browser and open up http://your-ip-addr:8081. Notice the Sign In option equally shown below:

The default username is admin, whereas to retrieve the countersign you need to run the following control:

          $ docker exec -i nexus_repo cat /nexus-data/admin.password 502ace93-5450-4f0d-97d2-9b3b3a88d149                  

And that'due south it. Your Nexus Repository Manager is set up-to-use. The side by side step is to create a new repository.

Create a Repository in Nexus:

In this step, you are going to create a Maven Hosted repository in Nexus, where your Jenkins is going to upload "build" artifacts.

Step 1:
Follow the below-mentioned steps to create a hosted repository, proper name it as maven-nexus-repo, which yous are going to utilize throughout this guide.

Select maven2 (hosted) recipe from the list as shown in the below-mentioned effigy:

Footstep 2:
On the Create Repository page,

  • Enter the name as maven-nexus-repo
  • In Version Policy, select the Mixed blazon of artifacts.
  • Under the Hosted section, in Deployment policy, select Allow redeploy. It volition let you to deploy an application multiple times.

Footstep 3:
To create a new user, become to Dashboard > Server Administrator and Configuration > User > Create user. Select Local user type which happens to exist the default Realm:

In the Create User folio,

  1. ID: Enter the desired ID; in our case, it is jenkins-user.
  2. First Name: Enter the desired showtime name; in our example, it is Jenkins.
  3. Final Name: Enter the desired second name; in our example, it is User.
  4. Email: Enter your email accost.
  5. Status: Select Active from your drop-down card.
  6. Roles: Brand sure that yous grant the nx-admin role to your user.

In case you want more details for user cosmos, then click here.

With this, nosotros are through with the setup function of Nexus Repository Manager. Let the states move to Jenkins to setup Nexus in that location.

Install and Configure Nexus Plugins in Jenkins

Hither you lot are going to install and configure a few plugins for Nexus in Jenkins. For this, go to Jenkins and and then Dashboard > Manage Jenkins > Manage Plugins > Available and search and install Nexus Artifact Uploader and Pipeline Utility Steps.

Add together Nexus Repository Director's user credentials in Jenkins. Go to Dashboard > Credentials > System > Global credentials (unrestricted), every bit shown beneath:

Next, prepare Maven every bit a managed tool. Go to Dashboard > Manage Jenkins > Global Tool Configuration and find Maven. Under this section, click on the Maven Installations button and add Maven, equally shown below:

Alternatively, you can besides install the Maven binary directly to your container on the /var/jenkins_home directory.

Create a Jenkins Pipeline

It'southward time to create a Jenkins Job. Here you are going to use Pipeline job type, named as JenkinsNexus, as shown below:

In the next page, detect the Pipeline section and copy the beneath-mentioned script in the text area:

          pipeline {     agent {         characterization "master"     }     tools {         maven "Maven"     }     environment {         NEXUS_VERSION = "nexus3"         NEXUS_PROTOCOL = "http"         NEXUS_URL = "you-ip-addr-hither:8081"         NEXUS_REPOSITORY = "maven-nexus-repo"         NEXUS_CREDENTIAL_ID = "nexus-user-credentials"     }     stages {         stage("Clone code from VCS") {             steps {                 script {                     git 'https://github.com/javaee/cargotracker.git';                 }             }         }         stage("Maven Build") {             steps {                 script {                     sh "mvn packet -DskipTests=truthful"                 }             }         }         phase("Publish to Nexus Repository Manager") {             steps {                 script {                     pom = readMavenPom file: "pom.xml";                     filesByGlob = findFiles(glob: "target/*.${pom.packaging}");                     echo "${filesByGlob[0].name} ${filesByGlob[0].path} ${filesByGlob[0].directory} ${filesByGlob[0].length} ${filesByGlob[0].lastModified}"                     artifactPath = filesByGlob[0].path;                     artifactExists = fileExists artifactPath;                     if(artifactExists) {                         echo "*** File: ${artifactPath}, group: ${pom.groupId}, packaging: ${pom.packaging}, version ${pom.version}";                         nexusArtifactUploader(                             nexusVersion: NEXUS_VERSION,                             protocol: NEXUS_PROTOCOL,                             nexusUrl: NEXUS_URL,                             groupId: pom.groupId,                             version: pom.version,                             repository: NEXUS_REPOSITORY,                             credentialsId: NEXUS_CREDENTIAL_ID,                             artifacts: [                                 [artifactId: pom.artifactId,                                 classifier: '',                                 file: artifactPath,                                 type: pom.packaging],                                 [artifactId: pom.artifactId,                                 classifier: '',                                 file: "pom.xml",                                 type: "pom"]                             ]                         );                     } else {                         mistake "*** File: ${artifactPath}, could not be found";                     }                 }             }         }     } }                  

Let'due south break downwardly the to a higher place-mentioned parameters bit by scrap:

  • NEXUS_VERSION: Here, we accept to mention the exact version of Nexus, which tin can be nexus2 or nexus3. In our case, it is latest version of nexus3.
  • NEXUS_PROTOCOL: For this guide we take used HTTP protocol, although, in case of production, you will have to utilise HTTPS.
  • NEXUS_URL: Add together your IP address and port number, where you are running Nexus. Make certain that you add Nexus example details without mentioning protocols, i.e., https or http.
  • NEXUS_CREDENTIAL_ID: Enter the user ID, which you lot previously created in Jenkins, which in our example is nexus-user-credentials.
  • Project Git: Under stages, nosotros used https://github.com/javaee/cargotracker

As you are through with the Pipeline set up, it'south fourth dimension to Build our project. Get to the JenkinsNexus projection task page and click Build Now. As this is your first build, it is going to take some fourth dimension, and so sit tight.

Once build is a success, in your Jenkins Console Output, you will run across something similar this:

Whereas, in your Nexus Repository Manager, you would see something similar to this:

Conclusion:

A systematic way to disturb the projection's antiquity is critical for any system. With the help of Jenkins Pipeline and Nexus Repository Manager, you can centralize artifact repo, which ultimately reduces the efforts of reproducing build time besides every bit switch CI tools without worrying most migrating our artifacts.

Nexus tin be configured with cloud storage services like AWS S3 and Google Cloud Storage as well, which gives you boosted freedom and delivery application without whatsoever hassle of storage.

mcdanielbress1988.blogspot.com

Source: https://appfleet.com/blog/publishing-artifacts-to-nexus-using-jenkins-pipelines/

Related Posts

0 Response to "How to Create Post Request Api Call to Upload Artifact to Nexus Repo"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel