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

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

[[428249]]

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

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

制品上传

NexusUI页面

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

使用Maven工具

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

  1. <server> 
  2.     <id>mymaven</id> 
  3.     <username>admin</username> 
  4.     <password>admin123</password
  5.   </server> 

使用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云学堂」

 

责任编辑:姜华 来源: DevOps云学堂
相关推荐

2023-05-26 08:31:09

2017-03-02 14:12:13

流水线代码Clojure

2018-10-23 16:35:19

华为云

2017-02-28 15:40:30

Docker流水线Azure

2013-06-06 09:31:52

2021-06-26 14:22:34

Tekton流水线Kubernetes

2022-01-26 08:12:42

Jenkins开源流水线

2017-02-28 16:00:45

DevOpsMarkdownreST

2023-05-10 15:08:00

Pipeline设计模式

2022-07-18 06:05:28

Gitlab流水线

2021-11-08 07:41:16

Go流水线编程

2024-01-07 12:47:35

Golang流水线设计模式

2021-12-24 08:02:48

GitLabCI模板库流水线优化

2023-08-18 10:24:52

GitLabCI 流水线

2021-04-13 06:15:37

开源部署流水线Jenkins

2020-10-25 11:28:12

开源端到端流水线

2021-06-28 06:32:46

Tekton Kubernetes Clone

2023-09-27 08:24:49

2021-06-18 05:48:02

Tekton DevopsKubernetes

2023-12-11 18:35:37

测试流水线自动化
点赞
收藏

51CTO技术栈公众号