持续集成流水线中的制品管理(Nexus)

be23aeec95dae1442443fc5da985a45c.png

本内容来自于《第③期DevOps实践训练营》, 了解详情可以点击 2021 如何成为一名优秀的DevOps工程师?|第三期训练营报名中

我们可以在该工作流中通过Maven和CI服务器来构建,存储,管理已编译完成的制品。

Nexus是一个存储库管理器,可存储和检索制品。它使您能够将构建的制品托管在私有且安全的存储库中。默认开发同学在进行开发的时候会使用一些包管理工具,例如:mavenantgradle这些都是常见项目编译构建工具 。这些工具可以理解为是一个命令行工具, 本身不会存储任何依赖包,而是通过公网官方的仓库中下载当前项目构建所需要的包。(内网的速度要比公网快,这会直接影响管道的构建速度)

994da6bcab9eb8d09a539ac9e57c7996.png制品上传

NexusUI页面

Nexus的UI中提供制品上传的功能, 导航Upload, 选择要上传的目标仓库。 最后填写仓库中包的坐标和包信息。

c1cdb7031dcfbb6172902d4de8b79d20.png

使用Maven工具

一般仓库都是需要认证后才能上传的, 所以首先需要在maven的配置文件中(settings.xml)填写仓库的认证信息。

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

使用mvn deploy 命令上传发布制品,命令参数与格式:

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(认证)

2b7524f2b34038e45d63c76ad493ed40.png
如果此时包已经有pom.xml 文件描述, 可以直接通过pom.xml文件进行上传:

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

使用Jenkins插件

安装Nexus Artifact Uploader插件、使用片段生成器生成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'

扩展: 如果需要经常上传制品, 我们最后将其封装在一个函数中,便于复用。

//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
}

使用Nexus API

382b540187f6de887ba7e597490a6309.png
经过调试,整理如下类型文件上传的接口:

##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 "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/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 "raw.asset1=@aopalliance-1.0.jar;type=application/java-archive" \
-F "raw.asset1.filename=aopalliance-1.0.jar"

下载制品

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

案例: 配置制品上传Pipeline

其实我们可以参考Nexus的UI页面, 使用Jenkins来做一个用于上传制品包的流水线作业:

  • srcUrl 指的是源码包的源码/包的仓库;

  • branchName 源码包仓库的分支;

  • groupId、artifactid、 version maven类型仓库的坐标;

  • type 包类型;

c193ab68204e3866479dfd19bffceefa.png
这个Jenkinsfile包含4个阶段, 分别是下载代码、代码编译、单元测试、上传制品。

@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
}

历史与Nexus相关的主题

往期推荐

Jenkins集成

>>> 欢迎投稿,微信:devopsvip。

关于我们

DevOps云学堂,专注于企业级DevOps运维开发技术实践分享,主要以新Linux运维技术、DevOps技术课程为主。丰富的一线实战经验,课程追求实用性获得多数学员认可。课程内容均来源于企业应用,在这里既学习技术又能获取热门技能,欢迎您的到来!

b574bb0d033e9ec9d71abe53a8bfc37c.png

更多DevOps实践,请关注「DevOps云学堂」

ddbd0e217a0a50e3b6c71b3e650e37ea.gif 点击阅读原文,进入企业DevOps学堂

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值