Artifact Management in Continuous Integration Pipeline

We can build, store, and manage compiled artifacts through Maven and CI servers in the workflow .

Nexus is a repository manager that stores and retrieves artifacts. It enables you to host your built artifacts in a private and secure repository . By default, developers will use some package management tools when developing, such as: maven, ant, gradleand these are common project compilation and construction tools. These tools can be understood as a command-line tool that does not store any dependent packages, but downloads the packages required for the current project build from the official warehouse on the public network. (The speed of the internal network is faster than that of the public network, which will directly affect the construction speed of the pipeline)

img

Product upload

Nexus UI page

The UI of Nexus provides the function of uploading products, navigating Upload, and selecting the target warehouse to be uploaded. Finally, fill in the coordinates and package information of the package in the warehouse.

img

Use the Maven tool

Generally, warehouses need to be authenticated before they can be uploaded, so you first need to fill in the warehouse's authentication information in the maven configuration file (settings.xml).

<server>
  <id>mymaven</id>
  <username>admin</username>
  <password>admin123</password>
</server>

Use the mvn deploy command to upload release products, command parameters and format:

mvn deploy:deploy-file
-DgroupId=xxxxxx pom中的groupId
-DartifactId=xxxxxx pom中的artifactId
-Dversion=xxxxxx pom中的版本号version
-Dpackaging=xxxxxx pom中打包方式
-Dfile=xxxxxx 本地文件
-Durl=xxxxxx 仓库url
-DrepositoryId=xxxxxx 对应的是setting.xml(认证)

img

If the package already has a pom.xml file description at this time, it can be uploaded directly through the pom.xml file:

mvn deploy:deploy-file \
-DgeneratePom=false \
-DrepositoryId=mymaven \
-Durl=http://192.168.1.200:8081/repository/mymavenrepo \
-DpomFile=pom.xml \
-Dfile=target/demo-0.0.1-SNAPSHOT.jar

Use the Jenkins plugin

Install the Nexus Artifact Uploader plugin and use the snippet generator to generate the DSL.

nexusArtifactUploader   artifacts: [[artifactId: 'devopstest', 
                                    classifier: '', 
                                    file: 'target/demo-0.0.1-SNAPSHOT.jar', 
                                    type: 'jar']], 
                        credentialsId: '1de05a13-197c-4a72-8c6a-cd330fc45559', 
                        groupId: 'com.jenkins', 
                        nexusUrl: '192.168.1.200:8081', 
                        nexusVersion: 'nexus3', 
                        protocol: 'http', 
                        repository: 'mymavenrepo', 
                        version: '1.1.2'

Extension: If you need to upload artifacts frequently, we finally encapsulate them in a function for easy reuse.

//NexusUploadByPlugin('devops-test', 'target/demo-0.0.1-SNAPSHOT.jar', 'jar', 'com.jenkins','1.1.2')

def NexusUploadByPlugin(artifactId, file, type, groupId,version){
    
    
    nexusArtifactUploader   artifacts: [[artifactId: artifactId, 
                                    classifier: '', 
                                    file: file, 
                                    type: type]], 
                        credentialsId: '1de05a13-197c-4a72-8c6a-cd330fc45559', 
                        groupId: groupId, 
                        nexusUrl: '192.168.1.200:8081', 
                        nexusVersion: 'nexus3', 
                        protocol: 'http', 
                        repository: 'mymavenrepo', 
                        version: version
}

Using the Nexus API

img

After debugging, the interface for uploading files of the following types is sorted out:

##PNG
curl -X POST "http://192.168.1.200:8081/service/rest/v1/components?repository=myrepo" \
-H "accept: application/json" \
-H "Content-Type: multipart/form-data" \
-F "raw.directory=/tmp" \
-F "raw.asset1=@默认标题_自定义px_2020-10-01-0.png;type=image/png" \
-F "raw.asset1.filename=默认标题_自定义px_2020-10-01-0.png"


## tar.gz & ZIP
curl -X POST "http://192.168.1.200:8081/service/rest/v1/components?repository=myrepo" \
-H "accept: application/json" \
-H "Content-Type: multipart/form-data" \
-F "raw.directory=/tmp" \
-F "[email protected];type=application/x-gzip" \
-F "raw.asset1.filename=aaa.tar.gz"


curl -X POST "http://192.168.1.200:8081/service/rest/v1/components?repository=myrepo" -H "accept: application/json" -H "Content-Type: multipart/form-data" -F "raw.directory=/tmp" -F "raw.asset1=@waypoint_0.1.5_linux_amd64.zip;type=application/x-gzip" -F "raw.asset1.filename=waypoint_0.1.5_linux_amd64.zip"


## Jar file 
curl -X POST "http://192.168.1.200:8081/service/rest/v1/components?repository=myrepo" \
-H "accept: application/json" \
-H "Content-Type: multipart/form-data" \
-F "raw.directory=/tmp" \
-F "[email protected];type=application/java-archive" \
-F "raw.asset1.filename=aopalliance-1.0.jar"

download product

cURL

curl -u admin:admin123 http://192.168.1.200:8081/repository/anyops/com/anyops/a
nyops-devops-service/1.1.1/anyops-devops-service-1.1.1.jar -o anyops-devops-service-1.1.1.jar

Wget

wget --http-user=admin --http-passwd=admin123 http://192.168.1.200:8081/repos
itory/anyops/com/anyops/anyops-devops-service/1.1.1/anyops-devops-service-1.1.1.jar

Case: Configure the product upload pipeline

In fact, we can refer to the Nexus UI page and use Jenkins to do a pipeline job for uploading product packages:

  • srcUrl refers to the source code/package warehouse of the source code package;
  • branchName The branch of the source package repository;
  • The coordinates of groupId, artifactid, version maven type warehouse;
  • type package type;

img

This Jenkinsfile contains 4 stages, which are downloading code, code compilation, unit testing, and uploading artifacts.

@Library("mylib@main") _
import org.devops.*

def checkout = new Checkout()
def build = new Build()
def unittest = new UnitTest()
def sonar = new Sonar()

pipeline {
    
    
    agent {
    
     label "build" }

    options {
    
    
        skipDefaultCheckout true
    }


    stages{
    
    
        stage("Checkout"){
    
    
            steps{
    
    
                script {
    
    
                    println("GetCode")
                    checkout.GetCode("${env.srcUrl}", "${env.branchName}")
                }
            }
        }

        stage("Build"){
    
    
            steps{
    
    
                script{
    
    
                    println("Build")
                    sh "mvn clean package "
                }
            }
        }

        stage("UnitTest"){
    
    
            steps{
    
    
                script{
    
    
                    unittest.CodeTest("${env.buildTool}")
                }
            }
        }

        stage("Upload"){
    
    
            steps{
    
    
                script{
    
    
                    NexusUploadByPlugin("${env.artifactId}", 
                                        'target/demo-0.0.1-SNAPSHOT.jar', 
                                        "${env.type}", 
                                        "${env.groupId}",
                                        "${env.version}")
                }
            }
        }
    }
}

//NexusUploadByPlugin('devops-test', 'target/demo-0.0.1-SNAPSHOT.jar', 'jar', 'com.jenkins','1.1.2')

def NexusUploadByPlugin(artifactId, file, type, groupId,version){
    
    
    nexusArtifactUploader   artifacts: [[artifactId: artifactId, 
                                    classifier: '', 
                                    file: file, 
                                    type: type]], 
                        credentialsId: '1de05a13-197c-4a72-8c6a-cd330fc45559', 
                        groupId: groupId, 
                        nexusUrl: '192.168.1.200:8081', 
                        nexusVersion: 'nexus3', 
                        protocol: 'http', 
                        repository: 'mymavenrepo', 
                        version: version
}

Guess you like

Origin blog.csdn.net/ximaiyao1984/article/details/132257777