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

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

作者:Lizeyang 2021-10-12 08:47:01

云计算 Nexus是一个存储库管理器,可存储和检索制品。它使您能够将构建的制品托管在私有且安全的存储库中。默认开发同学在进行开发的时候会使用一些包管理工具,例如:maven、ant、gradle这些都是常见项目编译构建工具 。

在善右等地区,都构建了全面的区域性战略布局,加强发展的系统性、市场前瞻性、产品创新能力,以专注、极致的服务理念,为客户提供成都做网站、成都网站设计 网站设计制作定制网站开发,公司网站建设,企业网站建设,成都品牌网站建设,网络营销推广,外贸营销网站建设,善右网站建设费用合理。

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

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

制品上传

NexusUI页面

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

使用Maven工具

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

  
 
 
 
  1.  
  2.     mymaven 
  3.     admin 
  4.     admin123 
  5.    

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

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

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

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

使用Jenkins插件

安装Nexus Artifact Uploader插件、使用片段生成器生成DSL。

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

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

  
 
 
 
  1. //NexusUploadByPlugin('devops-test', 'target/demo-0.0.1-SNAPSHOT.jar', 'jar', 'com.jenkins','1.1.2') 
  2.  
  3. def NexusUploadByPlugin(artifactId, file, type, groupId,version){ 
  4.     nexusArtifactUploader   artifacts: [[artifactId: artifactId,  
  5.                                     classifier: '',  
  6.                                     file: file,  
  7.                                     type: type]],  
  8.                         credentialsId: '1de05a13-197c-4a72-8c6a-cd330fc45559',  
  9.                         groupId: groupId,  
  10.                         nexusUrl: '192.168.1.200:8081',  
  11.                         nexusVersion: 'nexus3',  
  12.                         protocol: 'http',  
  13.                         repository: 'mymavenrepo',  
  14.                         version: version 

使用Nexus API

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

  
 
 
 
  1. ##PNG 
  2. curl -X POST "http://192.168.1.200:8081/service/rest/v1/components?repository=myrepo" \ 
  3. -H "accept: application/json" \ 
  4. -H "Content-Type: multipart/form-data" \ 
  5. -F "raw.directory=/tmp" \ 
  6. -F "raw.asset1=@默认标题_自定义px_2020-10-01-0.png;type=image/png" \ 
  7. -F "raw.asset1.filename=默认标题_自定义px_2020-10-01-0.png" 
  8.  
  9.  
  10. ## tar.gz & ZIP 
  11. curl -X POST "http://192.168.1.200:8081/service/rest/v1/components?repository=myrepo" \ 
  12. -H "accept: application/json" \ 
  13. -H "Content-Type: multipart/form-data" \ 
  14. -F "raw.directory=/tmp" \ 
  15. -F "raw.asset1=@nexus-3.30.0-01-unix.tar.gz;type=application/x-gzip" \ 
  16. -F "raw.asset1.filename=aaa.tar.gz" 
  17.  
  18.  
  19. 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" 
  20.  
  21.  
  22. ## Jar file  
  23. curl -X POST "http://192.168.1.200:8081/service/rest/v1/components?repository=myrepo" \ 
  24. -H "accept: application/json" \ 
  25. -H "Content-Type: multipart/form-data" \ 
  26. -F "raw.directory=/tmp" \ 
  27. -F "raw.asset1=@aopalliance-1.0.jar;type=application/java-archive" \ 
  28. -F "raw.asset1.filename=aopalliance-1.0.jar" 

下载制品

cURL

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

Wget

  
 
 
 
  1. wget --http-user=admin --http-passwd=admin123 http://192.168.1.200:8081/repos 
  2. 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 包类型;

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

  
 
 
 
  1. @Library("mylib@main") _ 
  2. import org.devops.* 
  3.  
  4. def checkout = new Checkout() 
  5. def build = new Build() 
  6. def unittest = new UnitTest() 
  7. def sonar = new Sonar() 
  8.  
  9. pipeline { 
  10.     agent { label "build" } 
  11.  
  12.     options { 
  13.         skipDefaultCheckout true 
  14.     } 
  15.  
  16.  
  17.     stages{ 
  18.         stage("Checkout"){ 
  19.             steps{ 
  20.                 script { 
  21.                     println("GetCode") 
  22.                     checkout.GetCode("${env.srcUrl}", "${env.branchName}") 
  23.                 } 
  24.             } 
  25.         } 
  26.  
  27.         stage("Build"){ 
  28.             steps{ 
  29.                 script{ 
  30.                     println("Build") 
  31.                     sh "mvn clean package " 
  32.                 } 
  33.             } 
  34.         } 
  35.  
  36.         stage("UnitTest"){ 
  37.             steps{ 
  38.                 script{ 
  39.                     unittest.CodeTest("${env.buildTool}") 
  40.                 } 
  41.             } 
  42.         } 
  43.  
  44.         stage("Upload"){ 
  45.             steps{ 
  46.                 script{ 
  47.                     NexusUploadByPlugin("${env.artifactId}",  
  48.                                         'target/demo-0.0.1-SNAPSHOT.jar',  
  49.                                         "${env.type}",  
  50.                                         "${env.groupId}", 
  51.                                         "${env.version}") 
  52.                 } 
  53.             } 
  54.         } 
  55.     } 
  56.  
  57. //NexusUploadByPlugin('devops-test', 'target/demo-0.0.1-SNAPSHOT.jar', 'jar', 'com.jenkins','1.1.2') 
  58.  
  59. def NexusUploadByPlugin(artifactId, file, type, groupId,version){ 
  60.     nexusArtifactUploader   artifacts: [[artifactId: artifactId,  
  61.                                     classifier: '',  
  62.                                     file: file,  
  63.                                     type: type]],  
  64.                         credentialsId: '1de05a13-197c-4a72-8c6a-cd330fc45559',  
  65.                         groupId: groupId,  
  66.                         nexusUrl: '192.168.1.200:8081',  
  67.                         nexusVersion: 'nexus3',  
  68.                         protocol: 'http',  
  69.                         repository: 'mymavenrepo',  
  70.                         version: version 

历史与Nexus相关的主题

本文转载自微信公众号「DevOps云学堂」

当前题目:持续集成流水线中的制品管理(Nexus)
标题URL:http://www.36103.cn/qtweb/news41/3241.html

网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联