1 Nexus基本概念
1.1 Components-组件
1.2 Assets-资产
2 Nexus仓库管理
2.1 仓库格式
2.2 下载机制
2.3 中央仓库
2.4 组件坐标
2.5 release与snapshot
3 LDAP认证
配置完成以后可以用lisi登录,也可以用原来的admin用户登录
4 Pipeline上传制品
4.1 mvn deploy上传制品
4.1.1 共享库配置
/src/org/devops/nexus.groovy
pakcage org.devops
//获取POM中的坐标
def GetGav(){
//上传制品
def jarName = sh returnStdout: true, script: "cd target;ls *.war"
env.jarName = jarName - "\n"
def pom = readMavenPom file: 'pom.xml'
env.pomVersion = "${pom.version}"
env.pomArtifact = "${pom.artifactId}"
env.pomPackaging = "${pom.packaging}"
env.pomGroupId = "${pom.groupId}"
println("${pomGroupId}-${pomArtifact}-${pomVersion}-${pomPackaging}")
return ["${pomGroupId}","${pomArtifact}","${pomVersion}","${pomPackaging}"]
}
//Nexus plugin deploy
def NexusUpload(){
//use nexus plugin
nexusArtifactUploader artifacts: [[artifactId: "${pomArtifact}",
classifier: '',
file: "${filePath}",
type: "${pomPackaging}"]],
credentialsId: 'nexus-admin-user',
groupId: "${pomGroupId}",
nexusUrl: '192.168.137.130:8081',
nexusVersion: 'nexus3',
protocol: 'http',
repository: "${repoName}",
version: "${pomVersion}"
}
//mvn deploy
def MavenUpload(){
def mvnHome = tool "M2"
sh """
cd target/
${mvnHome}/bin/mvn deploy:deploy-file -Dmaven.test.skip=true \
-Dfile=${jarName} -DgroupId=${pomGroupId} \
-DartifactId=${pomArtifact} -Dversion=${pomVersion} \
-Dpackaging=${pomPackaging} -DrepositoryId=maven-snapshots \
-Durl=http://192.168.137.130:8081/repository/maven-snapshots
"""
}
//制品晋级
def ArtifactUpdate(updateType,artifactUrl){
//晋级策略
if ("${updateType}" == "snapshots -> releases"){
println("snapshot -> release")
//下载原始制品
sh " rm -fr updates && mkdir updates && cd updates && wget --http-user=admin --http-passwd=123456 ${artifactUrl} && ls -l "
//获取artifactID
artifactUrl = artifactUrl - "http://192.168.137.130:8081/repository/maven-snapshots/"
artifactUrl = artifactUrl.split("/").toList()
println(artifactUrl.size())
env.jarName = artifactUrl[-1]
//env.pomVersion = artifactUrl[-2].replace("SNAPSHOT","RELEASE")
env.pomVersion = artifactUrl[-2].split("-").toList()[0]
env.pomArtifact = artifactUrl[-3]
pomPackaging = artifactUrl[-1]
pomPackaging = pomPackaging.split("\\.").toList()[-1]
env.pomPackaging = pomPackaging[-1]
env.pomGroupId = artifactUrl[0..-4].join(".")
println("${pomGroupId}##${pomArtifact}##${pomVersion}##${pomPackaging}")
env.newJarName = "${pomArtifact}-${pomVersion}.${pomPackaging}"
//更改名称
sh " cd updates && mv ${jarName} ${newJarName} "
//上传制品
env.repoName = "maven-releases"
env.filePath = "updates/${newJarName}"
NexusUpload()
}
}
def main(uploadType){
GetGav()
if ("${uploadType}" == "maven"){
MavenUpload()
} else if ("${uploadType}" == "nexus") {
env.repoName = "maven-snapshots"
env.filePath = "target/${jarName}"
NexusUpload()
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
- 64.
- 65.
- 66.
- 67.
- 68.
- 69.
- 70.
- 71.
- 72.
- 73.
- 74.
- 75.
- 76.
- 77.
- 78.
- 79.
- 80.
- 81.
- 82.
4.1.2 Maven配置
maven配置文件里面已经配置好了和repositoryId对应的认证信息
4.1.3 jenkins配置
4.2 nexus插件上传制品
4.2.1 共享库配置
4.2.2 安装插件
jenkins安装Nexus Artifact Uploader插件
4.2.3 创建nexus凭据
4.2.4 片段生成器
4.2.5 jenkins配置
4.2.6 构建输出
4.3 jenkinsfile
#!groovy
@Library('jenkinslib') _
def tools = new org.devops.tools()
def build = new org.devops.build()
def gitlab = new org.devops.gitlab()
def toemail = new org.devops.toemail()
def sonar = new org.devops.sonarqube()
def sonarapi = new org.devops.sonarapi()
def nexus = new org.devops.nexus()
def WebHookData = readJSON text : allData
String branch=WebHookData.ref
String userName=WebHookData.user_username
String projectId=WebHookData.project.id
String commitSha=WebHookData.checkout_sha
pipeline {
agent {
node {
label "built-in"
}
}
environment {
String GsrcUrl="${env.srcUrl}"
String GbranchName="${env.branchName}"
String GbuildType="${env.buildType}"
String GbuildShell="${env.buildShell}"
}
stages {
stage('CheckOut') {
steps {
script {
if ("$runOpts" == "GitlabPush"){
GbranchName = branch - 'refs/heads/'
currentBuild.description = "Trigger by user ${userName} \n branch: ${GbranchName}"
gitlab.ChangeCommitStatus(projectId,commitSha,"running")
}
tools.PrintMes("获取代码",'green')
checkout scmGit(branches: [[name: "${GbranchName}"]],
extensions: [cleanBeforeCheckout()],
userRemoteConfigs: [[credentialsId: '8c8a0774-7d4c-48a9-8f52-dd1a04f71320',
url: "${GsrcUrl}"]])
}
}
}
stage("Build") {
steps {
script {
tools.PrintMes("执行打包",'green')
build.Build(GbuildType,GbuildShell)
//上传制品
nexus.main("maven")
}
}
}
stage('QA') {
steps {
script {
tools.PrintMes("搜索项目",'green')
result = sonarapi.SerarchProject("${JOB_NAME}")
println(result)
if ( result == 'false' ){
println("${JOB_NAME}---项目不存在,准备创建项目---> ${JOB_NAME}!")
sonarapi.CreateProject("${JOB_NAME}")
} else {
println("${JOB_NAME}---项目已存在")
}
tools.PrintMes("配置项目质量规则","green")
qpName="${JOB_NAME}".split("-")[0] //Sonar%20way
sonarapi.ConfigQualityProfiles("${JOB_NAME}","java",qpName)
tools.PrintMes("配置质量阈","green")
sonarapi.ConfigQualityGates("${JOB_NAME}",qpName)
tools.PrintMes("代码扫描",'green')
sonar.SonarScan("dev","${JOB_NAME}","${JOB_NAME}","src")
tools.PrintMes("获取扫描结果",'green')
result = sonarapi.GetProjectStatus("${JOB_NAME}")
println(result)
if ( result.toString() == "ERROR" ){
toemail.Email("代码质量阈错误,请及时修复!",userEmail)
error "代码质量阈错误,请及时修复!"
}else {
println(result)
}
}
}
}
}
post {
always {
script {
println("always")
}
}
success {
script {
println("success")
gitlab.ChangeCommitStatus(projectId,commitSha,"success")
toemail.Email("流水线成功了",userEmail)
}
}
failure {
script {
println("failure")
gitlab.ChangeCommitStatus(projectId,commitSha,"failed")
toemail.Email("流水线失败了",userEmail)
}
}
aborted {
script {
println("aborted")
gitlab.ChangeCommitStatus(projectId,commitSha,"canceled")
toemail.Email("流水线被取消了",userEmail)
}
}
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
- 64.
- 65.
- 66.
- 67.
- 68.
- 69.
- 70.
- 71.
- 72.
- 73.
- 74.
- 75.
- 76.
- 77.
- 78.
- 79.
- 80.
- 81.
- 82.
- 83.
- 84.
- 85.
- 86.
- 87.
- 88.
- 89.
- 90.
- 91.
- 92.
- 93.
- 94.
- 95.
- 96.
- 97.
- 98.
- 99.
- 100.
- 101.
- 102.
- 103.
- 104.
- 105.
- 106.
- 107.
- 108.
- 109.
- 110.
- 111.
- 112.
- 113.
- 114.
5 发布制品
5.1 安装插件
jenkins安装Maven Artifact ChoiceListProvider (Nexus)插件
5.2 jenkins参数化构建
默认最新的在上面
5.3 点击构建
6 制品晋级
从maven-snapshots发布稳定版本到maven-releases
6.1 共享库配置
6.2 参数化构建
发布制品已经配置过
新增字符参数
6.3 Pipeline流水线配置
#!groovy
@Library('jenkinslib') _
def tools = new org.devops.tools()
def nexus = new org.devops.nexus()
def nexusapi = new org.devops.nexusapi()
pipeline {
agent {
node {
label "built-in"
}
}
stages {
stage("UpdateArtifact") {
steps {
script {
tools.PrintMes("制品晋级",'green')
nexus.ArtifactUpdate(updateType,artifactUrl)
}
}
}
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
6.4 构建输出
6.5 nexus仓库
7 Nexus reset api
7.1 获取仓库制品
7.1.1 共享库配置
/src/org/devops/nexusapi.groovy
package org.devops
//封装HTTP
def HttpReq(reqType,reqUrl,reqBody){
def sonarServer = "http://192.168.137.130:8081/service/rest"
result = httpRequest authentication: 'nexus-admin-user',
httpMode: reqType,
contentType: "APPLICATION_JSON",
consoleLogResponseBody: true,
ignoreSslErrors: true,
requestBody: reqBody,
url: "${sonarServer}/${reqUrl}",
quiet: true
return result
}
//获取仓库中所有组件
def GetRepoComponents(repoName){
apiUrl = "/v1/components?repository=${repoName}"
response = HttpReq("GET",apiUrl,'')
response = readJSON text: """${response.content}"""
println(response["items"].size())
return response["items"]
}
//获取单件组件
def GetComponentsId(repoName,groupId,artifactId,version){
println("获取单件组件ID")
result = GetRepoComponents(repoName)
for (component in result){
if (component["group"] == groupId && component["name"] == artifactId && component["version"] == version ){
componentId = component["id"]
return componentId
}
}
println(componentId)
}
//获取组件信息
def GetSingleComponents(repoName,groupId,artifactId,version){
println("获取单件组件信息")
componentId = GetComponentsId(repoName,groupId,artifactId,version)
apiUrl = "/v1/components/${componentId}"
response = HttpReq("GET",apiUrl,'')
response = readJSON text: """${response.content}"""
println(response["assets"]["downloadUrl"])
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
7.1.2 pipeline配置
7.1.3 构建输出
7.1.4 Nexus仓库信息
7.2 获取单件制品
7.2.1 共享库配置
7.2.2 参数化构建
7.2.3 Pipeline配置
#!groovy
@Library('jenkinslib') _
def tools = new org.devops.tools()
def nexus = new org.devops.nexus()
def nexusapi = new org.devops.nexusapi()
String updateType = "${env.updateType}"
String artifactUrl = "${env.artifactUrl}"
String pkgVersion = "${env.pkgVersion}"
pipeline {
agent {
node {
label "built-in"
}
}
stages {
stage("UpdateArtifact") {
steps {
script {
//tools.PrintMes("制品晋级",'green')
//nexus.ArtifactUpdate(updateType,artifactUrl)
//tools.PrintMes("获取仓库中所有组件",'green')
//nexusapi.GetRepoComponents("maven-snapshots")
tools.PrintMes("获取单件组件ID",'green')
nexusapi.GetComponentsId("maven-snapshots","com.itheima","web_demo",pkgVersion)
tools.PrintMes("获取单件组件信息",'red')
nexusapi.GetSingleComponents("maven-snapshots","com.itheima","web_demo",pkgVersion)
}
}
}
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.