制品仓库集成

本章我们主要讲述Jenkins与制品库nexus、artiifactory集成,上传下载制品。

目录


部署

测试环境使用Docker安装部署

  1. docker run -id \
  2. --privileged=true --name=nexus3 \
  3. -p 8081:8081 \
  4. -v ${LOCALDIR}/nexus3/nexus-data:/nexus-data \
  5. sonatype/nexus3:3.20.1

安装完成后默认的admin账号密码存储在了数据目录,获取初始化密码后更新账号密码。 images

功能

私服仓库: 本地maven私服加快构建速度 代理仓库:将公网等第三方提供的仓库代理到本地

images

基本概念

组件是一种资源,在构建过程中需要依赖。它可以是整个应用程序,也可以是静态资源(例如图片)。 通常,这些组件是各种文件的存档,包括:类文件中的Java字节码、C对象文件、文本文件、二进制文件。 组件的多种格式,例如:Java JAR,WAR,EAR格式;普通ZIP或.tar.gz文件;其他软件包格式,例如NuGet软件包,Ruby gems,NPM软件包;可执行文件格式,例如.exe 或.sh 文件,Android APK文件,各种安装程序格式。 images

组件可以由多个嵌套组件本身组成。组件提供了所有构建块和功能。可以通过组装并添加自己的业务相关组件来创建功能强大的完整应用程序。在不同的工具链中,组件称为工件,程序包,捆绑包,归档和其他术语。概念和想法保持不变,组件用作通用术语。组件由一组特定值(坐标)标识。这些坐标的通用集是组,名称和版本的用法。这些坐标的名称和用法随所使用的工具链而变化。组件也可以成为其他元数据的基础

images

资产 :例如Maven项目中的pom文件算是资产一部分,包含元数据的重要补充。 实际的存档文件(pom.xml)是与组件( jar/war包)关联的资产。但是,更复杂的格式具有与组件(jar包)关联的众多资产(pom)。例如,Maven存储库中的典型JAR组件至少由POM和JAR文件定义-两者均构成属于同一组件的单独资产。其他文件(例如JavaDoc或Sources JAR文件)是属于同一组件的资产。 另一方面,Docker格式为资产提供唯一的标识符,并将其称为Docker层。这些资产可用于不同的组件-Docker映像。例如,一个Docker层可以是多个Docker映像引用的特定操作系统。

images

上传制品

我们需要在nexus中创建一个新的仓库maven-hostd,同时我们需要在maven的配置文件settings.xml中配置maven-hostd认证。如下:

  1. <server>
  2. <id>maven-hostd</id>
  3. <username>admin</username>
  4. <password>admin123</password>
  5. </server>

注意使用mvn deploy 发布时,server.id == respository.id。

images images

集成jenkins上传制品

步骤:获取Jar包名称、读取pom文件、获取坐标信息、使用mvn deploy上传。

  1. def jarName = sh returnStdout: true, script: "cd target;ls *.jar"
  2. jarName = jarName - "\n"
  3. def pom = readMavenPom file: 'pom.xml'
  4. pomVersion = "${pom.version}"
  5. pomArtifact = "${pom.artifactId}"
  6. pomPackaging = "${pom.packaging}"
  7. pomGroupId = "${pom.groupId}"
  8. println("${pomGroupId}-${pomArtifact}-${pomVersion}-${pomPackaging}")
  9. def mvnHome = tool "M2"
  10. sh """
  11. cd target/
  12. ${mvnHome}/bin/mvn deploy:deploy-file -Dmaven.test.skip=true \
  13. -Dfile=${jarName} -DgroupId=${pomGroupId} \
  14. -DartifactId=${pomArtifact} -Dversion=${pomVersion} \
  15. -Dpackaging=${pomPackaging} -DrepositoryId=maven-hostd \
  16. -Durl=http://192.168.1.200:30083/repository/maven-hostd
  17. """

使用nexus插件上传制品

安装nexus artifact uploader插件、使用片段生成器生成DSL。

images

开始编写Jenkinsfile

  1. //use nexus plugin
  2. def repoName = "maven-hostd"
  3. def filePath = "target/${jarName}"
  4. nexusArtifactUploader artifacts: [[artifactId: "${pomArtifact}",
  5. classifier: '',
  6. file: "${filePath}",
  7. type: "${pomPackaging}"]],
  8. credentialsId: 'nexus-admin-user',
  9. groupId: "${pomGroupId}",
  10. nexusUrl: '192.168.1.200:30083',
  11. nexusVersion: 'nexus3',
  12. protocol: 'http',
  13. repository: "${repoName}",
  14. version: "${pomVersion}"

查看上传的日志 images 查看仓库制品 images

发布制品

安装Maven Artifact ChoiceListProvider (Nexus)插件

images

用户选择制品,在应用服务器通过salt、ansible下载制品并部署 images

nexus接口

  1. package org.devops
  2. //封装HTTP
  3. def HttpReq(reqType,reqUrl,reqBody){
  4. def sonarServer = "http://192.168.1.200:30083/service/rest"
  5. result = httpRequest authentication: 'nexus-admin-user',
  6. httpMode: reqType,
  7. contentType: "APPLICATION_JSON",
  8. consoleLogResponseBody: true,
  9. ignoreSslErrors: true,
  10. requestBody: reqBody,
  11. url: "${sonarServer}/${reqUrl}",
  12. quiet: true
  13. return result
  14. }
  15. //获取仓库中所有组件
  16. def GetRepoComponents(repoName){
  17. apiUrl = "/v1/components?repository=${repoName}"
  18. response = HttpReq("GET",apiUrl,'')
  19. response = readJSON text: """${response.content}"""
  20. println(response["items"].size())
  21. return response["items"]
  22. }
  23. //获取单件组件
  24. def GetComponentsId(repoName,groupId,artifactId,version){
  25. println("获取单件组件ID")
  26. result = GetRepoComponents(repoName)
  27. for (component in result){
  28. if (component["group"] == groupId && component["name"] == artifactId && component["version"] == version ){
  29. componentId = component["id"]
  30. return componentId
  31. }
  32. }
  33. println(componentId)
  34. }
  35. //获取组件信息
  36. def GetSingleComponents(repoName,groupId,artifactId,version){
  37. println("获取单件组件信息")
  38. componentId = GetComponentsId(repoName,groupId,artifactId,version)
  39. apiUrl = "/v1/components/${componentId}"
  40. response = HttpReq("GET",apiUrl,'')
  41. response = readJSON text: """${response.content}"""
  42. println(response["assets"]["downloadUrl"])
  43. }

artifactory集成

安装Artifactory插件,配置Artifactory仓库信息(仓库地址、用户认证信息)。 images

构建制品

  1. package org.devops
  2. //Maven打包构建
  3. def MavenBuild(buildShell){
  4. def server = Artifactory.newServer url: "http://192.168.1.200:30082/artifactory"
  5. def rtMaven = Artifactory.newMavenBuild()
  6. def buildInfo
  7. server.connection.timeout = 300
  8. server.credentialsId = 'artifactory-admin-user'
  9. //maven打包
  10. rtMaven.tool = 'M2'
  11. buildInfo = Artifactory.newBuildInfo()
  12. String newBuildShell = "${buildShell}".toString()
  13. println(newBuildShell)
  14. rtMaven.run pom: 'pom.xml', goals: newBuildShell, buildInfo: buildInfo
  15. //上传build信息
  16. server.publishBuildInfo buildInfo
  17. }
  18. def main(buildType,buildShell){
  19. if(buildType == "mvn"){
  20. MavenBuild(buildShell)
  21. }
  22. }

查看构建数据: images

命名规范

  1. 仓库命名规范
  2. 业务/项目-环境类型 例如: demo-dev
  3. 制品命名规范
  4. 应用名称-版本号-构建ID.type
  5. 例如: demo-myapp-service-1.jar
  6. 制品目录规范
  7. 业务/项目
  8. 应用名称
  9. 版本号
  10. 制品

images

上传制品

  1. rtUpload (
  2. serverId: "art1",
  3. spec:
  4. """{
  5. "files": [
  6. {
  7. "pattern": "target/${jarName}",
  8. "target": "${uploadDir}/"
  9. }
  10. ]
  11. }"""
  12. )

images