Nexus-REST API-[制品上传|制品下载|制品晋级]

root
233
文章
0
评论
2021年10月25日18:15:54 评论 5621字阅读18分44秒

Nexus-REST API-[制品上传|制品下载|制品晋级]

http://192.168.1.110:8081/nexus/#admin/system/api

NexusAPI调试方法

进入设置页面, 找到System > API , 即可进入API调试页面。

调试API /v1/components, 点击Try it out才能填写信息。

填写参数信息

点击执行操作, 204表示成功。 我们可以复用这里的CURL指令, 最后封装到Jenkins流水线当中

 

上传制品

curl -u admin:admin123 如果Nexus开启了认证需要配置认证信息才能正常访问。

##PNG
curl -X POST "http://192.168.1.200:8081/nexus/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/nexus/service/rest/v1/components?repository=myrepo" \
-H "accept: application/json" \
-H "Content-Type: multipart/form-data" \
-F "raw.directory=/tmp" \
-F "raw.asset1=@nexus-3.30.0-01-unix.tar.gz;type=application/x-gzip" \
-F "raw.asset1.filename=aaa.tar.gz"


curl -X POST "http://192.168.1.200:8081/nexus/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.110:8081/nexus/service/rest/v1/components?repository=myrepo" \
-H "accept: application/json" \
-H "Content-Type: multipart/form-data" \
-F "raw.directory=/tmp" \
-F "raw.asset1=@aopalliance-1.0.jar;type=application/java-archive" \
-F "raw.asset1.filename=aopalliance-1.0.jar"

上传制品(maven类型的制品):

curl -X 'POST' \
'http://192.168.1.110:8081/nexus/service/rest/v1/components?repository=hwf-maven-release' \
-H 'accept: application/json' \
-H 'Content-Type: multipart/form-data' \
-F 'maven2.groupId=hwf-app' \
-F 'maven2.artifactId=app-test' \
-F 'maven2.version=1.11' \
-F 'maven2.packaging=jar' \
-F 'maven2.asset1=@/data/sonarqube8.9.0/sonarqube_extensions/plugins/sonar-l10n-zh-plugin-8.9.jar' \
-F 'maven2.asset1.extension=sonar-l10n-zh-plugin-8.9.jar' \
-u admin:admin123

 

下载制品

cURL

curl -u admin:admin123 http://192.168.1.110: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.110:8081/repos
itory/anyops/com/anyops/anyops-devops-service/1.1.1/anyops-devops-service-1.1.1.jar

 

制品晋级实践

【raw库】--以jar包为例,其他类型参考

准备仓库: 按照环境不同,创建四个仓库分别存储不同环境的包。raw仓库

用户选择:

  • 晋级策略
  • 包信息

Jenkinsfile内容

currentBuild.description = "trigger  by  ${params.updatePolicy}"

pipeline {
    agent {
        label "build"
    }

    stages{

        stage("PullArtifact"){
            steps{
                script{

                    env.version = "${params.artifactUrl}".split('/')[-2]
                    env.appName = "${params.artifactUrl}".split('/')[-3]    // Day5/acmp-myapp-service
                    env.buName =  "${params.artifactUrl}".split('/')[-4] 
                    env.pkgName = "${params.artifactUrl}".split('/')[-1]

                    sh """
                        curl ${params.artifactUrl} -o ${env.pkgName} -u admin:admin123
                        ls -l
                        pwd
                    """
                }
            }
        }


        stage("UpdateArtifact"){
            steps{
                script{

                    switch("${params.updatePolicy}") {
                        case "dev>uat":
                            repoName = "hwf-uat"
                            targetDir = "${env.buName}/${env.appName}/${env.version}/"
                            PushRawArtifacts(repoName,targetDir, "./", "${env.pkgName}" )
                            break;

                        case "uat>stag":
                            repoName = "hwf-stag"
                            targetDir = "${env.buName}/${env.appName}/${env.version}/"
                            PushRawArtifacts(repoName,targetDir, "./", "${env.pkgName}" )
                            break;

                        case "stag>prod":
                            repoName = "hwf-prod"
                            targetDir = "${env.buName}/${env.appName}/${env.version}/"
                            PushRawArtifacts(repoName,targetDir, "./", "${env.pkgName}" )
                            break;

                        default:
                            println("xxxxxxxxx")
                    }
                }
            }
        }
    }
}

//上传制品
def PushRawArtifacts(repoName,targetDir, filePath, pkgName ){
     withCredentials([usernamePassword(credentialsId: '8a3c99b1-a165-4ac6-a972-2355e6cc14a7', passwordVariable: 'TOKEN', usernameVariable: 'USER')]) {
        sh """
            curl -X POST "http://192.168.1.110:8081/nexus/service/rest/v1/components?repository=${repoName}" \
            -H "accept: application/json" \
            -H "Content-Type: multipart/form-data" \
            -F "raw.directory=${targetDir}" \
            -F "raw.asset1=@${filePath}/${pkgName};type=application/java-archive" \
            -F "raw.asset1.filename=${pkgName}" \
            -u "${USER}":"${TOKEN}"
        """
    }

}

【maven库】

准备仓库: 按照环境不同,创建四个仓库分别存储不同环境的包。

用户选择:

  • 晋级策略
  • 包信息
  • type

Jenkinsfile内容

currentBuild.description = "trigger  by  ${params.updatePolicy}"

pipeline {
    agent {
        label "build"
    }

    stages{

        stage("PullArtifact"){
            steps{
                script{

                    env.version = "${params.artifactUrl}".split('/')[-2]
                    env.appName = "${params.artifactUrl}".split('/')[-3]    // Day5/acmp-myapp-service
                    env.buName =  "${params.artifactUrl}".split('/')[-4] 
                    env.pkgName = "${params.artifactUrl}".split('/')[-1]
                    env.groName = "${params.artifactUrl}".split('/')[-5]
                    sh """
                        curl ${params.artifactUrl} -o ${env.pkgName} -u admin:admin123

                    """
                }
            }
        }


        stage("UpdateArtifact"){
            steps{
                script{

                    switch("${params.updatePolicy}") {
                        case "dev>uat":
                            repoName = "test"
                            targetDir = "${env.buName}/${env.appName}/${env.version}/"
                            PushRawArtifacts(repoName,targetDir, "./", "${env.pkgName}","${params.type}" )
                            break;

                        case "uat>stag":
                            repoName = "hwf-stag"
                            targetDir = "${env.buName}/${env.appName}/${env.version}/"
                            PushRawArtifacts(repoName,targetDir, "./", "${env.pkgName}" )
                            break;

                        case "stag>prod":
                            repoName = "hwf-prod"
                            targetDir = "${env.buName}/${env.appName}/${env.version}/"
                            PushRawArtifacts(repoName,targetDir, "./", "${env.pkgName}" )
                            break;

                        default:
                            println("xxxxxxxxx")
                    }
                }
            }
        }
    }
}

//上传制品
def PushRawArtifacts(repoName,targetDir, filePath, pkgName,type){
     withCredentials([usernamePassword(credentialsId: '8a3c99b1-a165-4ac6-a972-2355e6cc14a7', passwordVariable: 'TOKEN', usernameVariable: 'USER')]) {
	env.groupName="${env.groName}.${env.buName}"
	sh """
    curl -X 'POST' \
      "http://192.168.1.110:8081/nexus/service/rest/v1/components?repository=${repoName}" \
      -H 'accept: application/json' \
      -H 'Content-Type: multipart/form-data' \
      -F "maven2.groupId=${groupName}" \
      -F "maven2.artifactId=${appName}" \
      -F "maven2.version=${version}" \
      -F "maven2.packaging=${type}" \
      -F "maven2.asset1=@${filePath}/${pkgName}" \
      -F "maven2.asset1.extension=${pkgName}" \
      -u "${USER}":"${TOKEN}"
        """
    }

}

 

继续阅读
weinxin
我的微信
这是我的微信扫一扫
  • 文本由 发表于 2021年10月25日18:15:54
  • 除非特殊声明,本站文章均为原创,转载请务必保留本文链接
Nexus-[功能简介|容器方式安装|使用方法] Nexus

Nexus-[功能简介|容器方式安装|使用方法]

Nexus- 工作流定义: 集成流水线: 提交代码,构建,单测,代码扫描,上传制品【生成制品】 制品晋级流水线:输入版本,选择晋级策略。(将制品复制到对应环境的仓库) 发布流水线: 输入版本号, 输入...
匿名

发表评论

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: