Devops之制品庫(kù)平臺(tái)實(shí)踐-nexus
目錄
本節(jié)實(shí)踐
實(shí)戰(zhàn)名稱 |
---|
?? 實(shí)踐:配置Maven代理倉(cāng)庫(kù)(測(cè)試成功)-2022.6.7 |
?? 實(shí)踐:創(chuàng)建Maven本地倉(cāng)庫(kù)(依賴)-2022.6.10(測(cè)試成功) |
?? 實(shí)踐:創(chuàng)建Raw本地倉(cāng)庫(kù)(制品)(測(cè)試成功)-2022.6.10 |
?? 實(shí)踐:使用管理頁(yè)面上傳制品(測(cè)試成功)-2023.6.27 |
?? 實(shí)踐:使用mvn命令上傳制品(自定義pom信息(靈活))-2022.6.11 |
?? 實(shí)踐:直接讀取pom文件(擴(kuò)展)-2022.6.11(測(cè)試成功) |
?? 實(shí)踐:使用Jenkins插件上傳制品(把信息寫死)(測(cè)試成功)-2023.6.28 |
?? 實(shí)踐:使用Jenkins插件上傳制品(讀取POM文件上傳制品)(測(cè)試成功)-2023.6.28 |
?? 擴(kuò)展: 可以在Jenkins頁(yè)面添加參數(shù), 讓用戶輸入后進(jìn)行發(fā)布(測(cè)試成功)-2023.6.28 |
?? 實(shí)踐:Jenkins插件獲取包路徑-2023.6.29(測(cè)試成功) |
?? 實(shí)踐:CI綜合實(shí)踐-2023.6.29(測(cè)試成功) |
1、Nexus基本功能簡(jiǎn)介
官網(wǎng):https://www.sonatype.com/products/nexus-repository?topnav=true
官方文檔: https://help.sonatype.com/repomanager3/product-information/release-notes
工作流定義:
- 集成流水線: 提交代碼,構(gòu)建,單測(cè),代碼掃描,上傳制品【生成制品】
- 發(fā)布流水線: 輸入版本號(hào), 輸入部署環(huán)境.(從對(duì)應(yīng)環(huán)境的制品庫(kù)中取制品)
制品類型: 二進(jìn)制制品, docker鏡像
核心技能點(diǎn):
- 制品庫(kù)管理規(guī)范(創(chuàng)建)
- 上傳制品
- 下載制品
從3.x開始,它默認(rèn)支持許多主流的軟件包格式。Docker、Maven、Npm:
倉(cāng)庫(kù)類型:
- proxy 代理倉(cāng)庫(kù)。
- hosted 私有倉(cāng)庫(kù)。
- group 倉(cāng)庫(kù)組,將多個(gè)倉(cāng)庫(kù)組合在一起,通過同一個(gè)URL對(duì)外提供。
代理倉(cāng)庫(kù) : Maven、Npm等。用于存儲(chǔ)外網(wǎng)公共倉(cāng)庫(kù)中的插件和依賴,不可進(jìn)行修改和私自上傳。
2、Nexus3安裝部署配置
- docker部署
## 1、下載鏡像
docker pull sonatype/nexus3:3.53.0
## 2、創(chuàng)建數(shù)據(jù)存儲(chǔ)目錄
mkdir -p /data/devops6/nexus3/data
chmod 777 -R /data/devops6/nexus3/
## 3、啟動(dòng)命令
docker run -itd \
-p 8081:8081 \
-v /data/devops6/nexus3/:/nexus-data \
--restart=always \
--name nexus3 sonatype/nexus3:3.53.0
## 4、訪問
http://172.29.9.101:8081
- 查看日志來確定Nexus是否已啟動(dòng)并準(zhǔn)備就緒
docker logs nexus3 -f
- 在日志中,看到
Started Sonatype Nexus OSS 3.53.0-01
,這意味著Nexus可以使用了?,F(xiàn)在轉(zhuǎn)到瀏覽器并打開:
http://172.29.9.101:8081
- 安裝完成后, 默認(rèn)的admin賬號(hào)密碼存儲(chǔ)在了數(shù)據(jù)目錄,獲取初始化密碼:
docker exec nexus3 cat /nexus-data/admin.password
登錄后需要更新密碼 admin/Admin@123
這里可以啟用匿名用戶登錄,無所謂的:
- 登錄后
3、Nexus制品庫(kù)應(yīng)用實(shí)踐
1.搭建Maven私服(代理倉(cāng)庫(kù))
默認(rèn)開發(fā)同學(xué)在進(jìn)行開發(fā)的時(shí)候會(huì)使用一些包管理工具,例如:maven
、ant
、gradle
這些都是常見項(xiàng)目編譯構(gòu)建工具 。這些工具可以理解為是一個(gè)命令行工具, 本身不會(huì)存儲(chǔ)任何依賴包,而是通過公網(wǎng)官方的倉(cāng)庫(kù)中下載當(dāng)前項(xiàng)目構(gòu)建所需要的包。 (內(nèi)網(wǎng)的速度要比公網(wǎng)快,這會(huì)直接影響管道的構(gòu)建速度)
使用私服,就是在企業(yè)內(nèi)部建立單一的可信源, 例如:我們?cè)诠就ㄟ^nexus創(chuàng)建一個(gè)代理倉(cāng)庫(kù), 將公網(wǎng)倉(cāng)庫(kù)中的maven包代理到內(nèi)網(wǎng)倉(cāng)庫(kù)中。 這樣整個(gè)公司的同學(xué)就可以直接訪問內(nèi)網(wǎng)的私服進(jìn)行下載構(gòu)建依賴包。(減少了引入不信任依賴的風(fēng)險(xiǎn))
代理倉(cāng)庫(kù)不會(huì)一下子把公網(wǎng)倉(cāng)庫(kù)中的所有包下載到本地,而是按需緩存。 例如: 此時(shí)我需要使用aa這個(gè)包, 如果代理倉(cāng)庫(kù)中沒有, 則請(qǐng)求外部服務(wù)器下載這個(gè)包并進(jìn)行緩存。第二次訪問的時(shí)候,就直接訪問代理倉(cāng)庫(kù)了。、
安裝nexus后,默認(rèn)存在以下圖中的倉(cāng)庫(kù), 這些倉(cāng)庫(kù)是官方默認(rèn)配置好的maven私服。(可以直接使用)
進(jìn)入其中一個(gè)倉(cāng)庫(kù), 可以看到默認(rèn)的配置。即: 代理公網(wǎng)repo1中的包到本地;
?? 實(shí)踐:配置Maven代理倉(cāng)庫(kù)(測(cè)試成功)-2022.6.7 |
- 實(shí)驗(yàn)環(huán)境
nexus3.39.0-01
apache-maven-3.8.5
- 實(shí)驗(yàn)軟件(無)
- 創(chuàng)建
proxy-aliyun-maven
倉(cāng)庫(kù):倉(cāng)庫(kù)類型選擇proxy
,制品策略類型選擇Release
,其他默認(rèn)。
- 編輯maven構(gòu)建節(jié)點(diǎn)的
settings.xml
文件,配置阿里云代理倉(cāng)庫(kù)地址
[root@devops conf]#vim /usr/local/apache-maven-3.8.5/conf/settings.xml
https://maven.aliyun.com/repository/public
注意:
- 創(chuàng)建完成后如下:
http://172.29.9.101:8081/repository/proxy-aliyun-maven/
- 配置maven軟件的配置文件
[root@devops conf]#vim /usr/local/apache-maven-3.8.5/conf/settings.xml
<url>http://172.29.9.101:8081/repository/proxy-aliyun-maven/</url>
- 刪除本地緩存:
[root@devops conf]#rm -rf ~/.m2/
[root@devops conf]#rm -rf /data/maven_build_cache/*
[root@devops conf]#vim /usr/local/apache-maven-3.8.5/conf/settings.xml
- 測(cè)試效果:
來到一個(gè)java項(xiàng)目里,使用maven進(jìn)行測(cè)試效果:
[root@devops devops4-maven-service-master]#pwd
/root/devops4-maven-service-master
[root@devops devops4-maven-service-master]#ls
build.sh mvnw mvnw.cmd pom.xml README.md sonar-project.properties src
[root@devops devops4-maven-service-master]#mvn clean package
此時(shí)會(huì)報(bào)一個(gè)錯(cuò)誤的,這里我們要配置下權(quán)限:
這種公網(wǎng)的,我們一般不加權(quán)限:
再次構(gòu)建,觀察效果:
可以看到能夠正常下載。
- 來到這里也是可以看到下載的包:
測(cè)試成功。??
2.搭建制品庫(kù)(本地倉(cāng)庫(kù) 依賴包)
本地倉(cāng)庫(kù):以Maven為例:
- RELEASE類型倉(cāng)庫(kù)(存放制品穩(wěn)定版) 這個(gè)里面的包一般是需要手動(dòng)指定的;
- SNAPSHOT類型倉(cāng)庫(kù)(存放制品開發(fā)版) 這個(gè)里面的包一般是自動(dòng)生成版本號(hào)的;
切記:release類型的倉(cāng)庫(kù)只能存放release版本的包。不能將release類型的包上傳到snapshot倉(cāng)庫(kù),同理snapshot類型的包也不能上傳到release類型的倉(cāng)庫(kù)中。
- 新建raw類型的倉(cāng)庫(kù): raw可以理解為普通的文件存儲(chǔ);
raw格式就是一塊普通的存儲(chǔ)。(制品就傳到這種類型的倉(cāng)庫(kù)里去)
① 創(chuàng)建Maven本地倉(cāng)庫(kù)(依賴)
?? 實(shí)踐:創(chuàng)建Maven本地倉(cāng)庫(kù)(依賴)-2022.6.10(測(cè)試成功) |
- 創(chuàng)建倉(cāng)庫(kù):
選擇hosted類型:
- 定義和配置倉(cāng)庫(kù)的信息: 名稱、存儲(chǔ)、是否允許重新上傳:
最后點(diǎn)擊Create repositry
:
- 觀察效果:
- 同理,我們?cè)賱?chuàng)建一個(gè)
devops4-release
的本地倉(cāng)庫(kù):
測(cè)試完成。??
② 創(chuàng)建Raw本地倉(cāng)庫(kù)(制品)
?? 實(shí)踐:創(chuàng)建Raw本地倉(cāng)庫(kù)(制品)(測(cè)試成功)-2022.6.10 |
- 創(chuàng)建倉(cāng)庫(kù):
選擇raw(hosted)類型:
填寫倉(cāng)庫(kù)名,并創(chuàng)建:
- 觀察效果:
- 模擬上傳一個(gè)圖片:
測(cè)試結(jié)束。??
4、CI流水線中集成制品庫(kù)
1.使用管理頁(yè)面上傳制品
頁(yè)面很方便上傳,但是有時(shí)候不太好用…例如出現(xiàn)上傳失敗等問題(暫時(shí)無法解決,不同版本的nexus有些api不對(duì)應(yīng)的坑)。
?? 實(shí)踐:使用管理頁(yè)面上傳制品(測(cè)試成功)-2023.6.27 |
- 我們來創(chuàng)建
maven-devops6-release
和maven-devops6-snapshot
2個(gè)maven倉(cāng)庫(kù)
- 這次來手動(dòng)上傳下jenkins agent的
agent.jar
包。
- 先來模擬一次報(bào)錯(cuò)提示
我們指定jar包Version
是1.1.1-SNAPSHOT
,但是我們往RELEASE
類型的倉(cāng)庫(kù)上傳,肯定就會(huì)報(bào)錯(cuò)的啦。
- 我們改變下這里的Version字段內(nèi)容,然后再次上傳,觀察下現(xiàn)象
此時(shí),就可以正常上傳成功了。
2.使用maven指令上傳制品
參考:https://support.sonatype.com/hc/en-us/articles/213465818-How-can-I-programmatically-upload-an-artifact-into-Nexus-2-
如果是0.0.1或者0.0.1-RELEASE,就是RELEASE版本。
?? 擴(kuò)展:
如果是maven類型的具有源碼的項(xiàng)目, 可以直接使用mvn命令上傳,更加方便。
//上傳制品使用maven命令
def PushArtifactsByMvn(repoName,filePath ){
sh """
mvn deploy:deploy-file \
-DgeneratePom=false \
-DrepositoryId="maven-hosted" \
-Durl=http://192.168.1.200:8081/repository/"${repoName}" \
-DpomFile=pom.xml \
-Dfile="${filePath}"
"""
}
PushArtifactsByMvn("${params.repoName}","target/${pkg}")
① 方法1:使用mvn命令上傳制品(自定義pom信息(靈活))
?? 實(shí)踐:使用mvn命令上傳制品(自定義pom信息(靈活))-2022.6.11 |
- 實(shí)驗(yàn)環(huán)境
sonatype/nexus3:3.53.0
apache-maven-3.9.2
-
實(shí)驗(yàn)軟件(無)
-
上傳制品之前, 肯定得確定目標(biāo)倉(cāng)庫(kù)是存在的。 如果不存在我們可以新建一個(gè) hosted類型的maven倉(cāng)庫(kù)。
- 倉(cāng)庫(kù)已經(jīng)有了, 需要更新maven的配置文件,在settings.xml中添加倉(cāng)庫(kù)的認(rèn)證信息。如下:
<server>
<id>mymaven</id>
<username>admin</username>
<password>admin123</password>
</server>
[root@devops ~]#vim /usr/local/apache-maven-3.8.5/conf/settings.xml
<server>
<id>mymaven</id>
<username>admin</username>
<password>admin123</password>
</server>
- 上傳制品前記得先要編譯構(gòu)建下的:
[root@devops ~]#cd
[root@devops ~]#cd devops4-maven-service-master
[root@devops devops4-maven-service-master]#ls
build.sh mvnw mvnw.cmd pom.xml README.md sonar-project.properties src target
[root@devops devops4-maven-service-master]#mvn clean package
[root@devops devops4-maven-service-master]#pwd
/root/devops4-maven-service-master
[root@devops devops4-maven-service-master]#ls
build.sh mvnw mvnw.cmd pom.xml README.md sonar-project.properties src target
[root@devops devops4-maven-service-master]#ls target/
classes demo-0.0.1-SNAPSHOT.jar demo-0.0.1-SNAPSHOT.jar.original maven-archiver
- 開始上傳:
注意使用mvn deploy 發(fā)布時(shí),-DrepositoryId
參數(shù)的值要與上面配置文件中的<server>
標(biāo)簽中的<id>
一致。不然會(huì)出現(xiàn)401,用戶認(rèn)證失敗的問題。
mvn deploy:deploy-file
-DgroupId=xxxxxx #pom中的groupId
-DartifactId=xxxxxx #pom中的artifactId
-Dversion=xxxxxx #pom中的版本號(hào)version
-Dpackaging=xxxxxx #pom中打包方式
-Dfile=xxxxxx #本地文件
-Durl=xxxxxx #倉(cāng)庫(kù)url
-DrepositoryId=xxxxxx #對(duì)應(yīng)的是setting.xml(認(rèn)證)
Maven上傳報(bào)錯(cuò), 401 可以確定是認(rèn)證的錯(cuò)誤。 需要檢查認(rèn)證信息。
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-deploy-plugin:2.8.2:deploy-file (default-cli) on project demo: Failed to deploy artifacts: Could not transfer artifact com.devops:zeyang:jar:1.1.1 from/to remote-repository (http://192.168.1.200:8081/repository/devops-maven/): authentication failed for http://192.168.1.200:8081/repository/devops-maven/com/devops/zeyang/1.1.1/zeyang-1.1.1.jar, status: 401 Unauthorized -> [Help 1]
替換參數(shù), 執(zhí)行命令開始上傳制品。
mvn deploy:deploy-file \
-DgroupId=com.devops \
-DartifactId=xyy \
-Dversion=1.1.1-snapshot \
-Dpackaging=jar \
-Dfile=target/demo-0.0.1-SNAPSHOT.jar \
-Durl=http://172.29.9.101:8081/repository/devops4-snapshot/ \
-DrepositoryId=mymaven
- 我們?cè)俅涡薷南聟?shù)觀察下效果:
mvn deploy:deploy-file \
-DgroupId=com.devops \
-DartifactId=xyy \
-Dversion=1.1.1-SNAPSHOT \
-Dpackaging=jar \
-Dfile=target/demo-0.0.1-SNAPSHOT.jar \
-Durl=http://172.29.9.101:8081/repository/devops4-snapshot/ \
-DrepositoryId=mymaven
#注意:默認(rèn)是會(huì)生成pom文件的。
#如果不想生成pom文件,則可以使用如下選項(xiàng)
-DgeneratePom=false
這里的
-Dversion=1.1.1-SNAPSHOT
SNAPSHOT一定要大寫才行的!否則會(huì)被認(rèn)為是release類型的制品,當(dāng)然也就不能上傳到snapshot倉(cāng)庫(kù)了。
可以看到,本次上傳成功了:
備注:Pom文件就是個(gè)坐標(biāo)。
- 驗(yàn)證:制品已經(jīng)上傳成功了。
測(cè)試成功。??
② 方法2:直接讀取pom文件(擴(kuò)展)
?? 實(shí)踐:直接讀取pom文件(擴(kuò)展)-2022.6.11(測(cè)試成功) |
- 實(shí)驗(yàn)環(huán)境
- 默認(rèn)項(xiàng)目下是已經(jīng)有
pom.xml
文件了:
sonatype/nexus3:3.53.0
apache-maven-3.9.2
- 實(shí)驗(yàn)軟件(無)
[root@devops devops4-maven-service-master]#ls
build.sh mvnw mvnw.cmd pom.xml README.md sonar-project.properties src target
[root@devops devops4-maven-service-master]#cat pom.xml
- 這里直接使用命令上傳:
mvn deploy:deploy-file \
-DgeneratePom=true \
-DrepositoryId=mymaven \
-Durl=http://172.29.9.101:8081/repository/devops4-snapshot/ \
-DpomFile=pom.xml \
-Dfile=target/demo-0.0.1-SNAPSHOT.jar
- 驗(yàn)證:
測(cè)試成功。??
?? FAQ:
release類型的倉(cāng)庫(kù)只能上傳release版本的包。如果你嘗試用snapshot包上傳到release類型的倉(cāng)庫(kù)時(shí)會(huì)遇到這些錯(cuò)誤的。
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-deploy-plugin:2.8.2:deploy-file (default-cli) on project demo: Failed to deploy artifacts: Could not transfer artifact com.example:demo:jar:0.0.1 from/to maven-hosted (http://192.168.1.200:8081/repository/maven-zeyang-test/): transfer failed for http://192.168.1.200:8081/repository/maven-zeyang-test/com/example/demo/0.0.1/demo-0.0.1.jar, status: 400 Repository version policy: SNAPSHOT does not allow version: 0.0.1 -> [Help 1]
解決方法: 1. 更新pom中的版本號(hào) 2. 對(duì)號(hào)入座,上傳到對(duì)應(yīng)類型的倉(cāng)庫(kù)。
<groupId>com.example</groupId>
<artifactId>myapp</artifactId>
<version>0.0.2-SNAPSHOT</version> //改成0.0.2-RELEASE
3.使用Jenkins插件上傳制品
① 方法1:使用Jenkins插件上傳制品(把信息寫死)
?? 實(shí)踐:使用Jenkins插件上傳制品(把信息寫死)(測(cè)試成功)-2023.6.28 |
- 安裝插件:
Nexus Aritifact Uploader
- 使用片段生成器生成DSL:
找一個(gè)pipeline項(xiàng)目,生成流水線腳本:
新建一個(gè)nexus憑據(jù):
生成代碼:
nexusArtifactUploader artifacts: [[artifactId: 'demo-app', classifier: '', file: 'target/xxx.jar', type: 'jar']], credentialsId: '3404937d-89e3-4699-88cf-c4bd299094ad', groupId: 'com.devops6', nexusUrl: '172.29.9.101:8081', nexusVersion: 'nexus3', protocol: 'http', repository: 'maven-devops6-release', version: '1.1.1'
調(diào)整下格式:
nexusArtifactUploader artifacts: [[artifactId: 'demo-app',
classifier: '',
file: 'target/xxx.jar',
type: 'jar']],
credentialsId: '3404937d-89e3-4699-88cf-c4bd299094ad',
groupId: 'com.devops6',
nexusUrl: '172.29.9.101:8081',
nexusVersion: 'nexus3',
protocol: 'http',
repository: 'maven-devops6-release',
version: '1.1.1'
- 這邊來到j(luò)enkins的
devops6-maven-service
項(xiàng)目里,先跑一次流水線看下有問題沒:
可以看到流水線是ok的。
- 我們?cè)賮韺懸幌铝魉€代碼:
stage("PushArtifact"){
steps{
script{
PushArtifactByPlugin()
}
}
}
def PushArtifactByPlugin(){
nexusArtifactUploader artifacts: [[artifactId: 'demo-app',
classifier: '',
file: 'target/demo-0.0.1-SNAPSHOT.jar',
type: 'jar']],
credentialsId: '3404937d-89e3-4699-88cf-c4bd299094ad',
groupId: 'com.devops6',
nexusUrl: '172.29.9.101:8081',
nexusVersion: 'nexus3',
protocol: 'http',
repository: 'maven-devops6-release',
version: '1.1.1'
}
然后把這個(gè)代碼放到上面項(xiàng)目的回放里,跑一次:
@Library("devops06@main") _
//import src/org/devops/Build.groovy
def build = new org.devops.Build()
def sonar = new org.devops.Sonar()
currentBuild.displayName = "${env.branchName}-commitID"
currentBuild.description = "Trigger by user jenkins \n branch: ${env.branchName}"
pipeline {
agent {label "build"}
options {
skipDefaultCheckout true
}
stages{
stage("CheckOut"){
steps{
script{
build.CheckOut()
}
}
}
stage("Build"){
steps{
script{
build.Build()
}
}
}
stage("CodeScan"){
when {
environment name: 'skipSonar', value: 'false'
}
steps{
script{
sonar.SonarScannerByPlugin()
}
}
}
stage("PushArtifact"){
steps{
script{
PushArtifactByPlugin()
}
}
}
}
}
def PushArtifactByPlugin(){
nexusArtifactUploader artifacts: [[artifactId: 'demo-app',
classifier: '',
file: 'target/demo-0.0.1-SNAPSHOT.jar',
type: 'jar']],
credentialsId: '3404937d-89e3-4699-88cf-c4bd299094ad',
groupId: 'com.devops6',
nexusUrl: '172.29.9.101:8081',
nexusVersion: 'nexus3',
protocol: 'http',
repository: 'maven-devops6-release',
version: '1.1.1'
}
可以看到,上傳制品成功了:
- 下載制品,點(diǎn)擊鏈接,下載就ok了。
測(cè)試成功。??
② 方法2:使用Jenkins插件上傳制品(讀取POM文件上傳制品)
?? 實(shí)踐:使用Jenkins插件上傳制品(讀取POM文件上傳制品)(測(cè)試成功)-2023.6.28 |
- 在
devops6-maven-service
gitlab項(xiàng)目里編輯下pom.xml
文件,添加packaging
選項(xiàng)并提交。
<packaging>jar</packaging>
- 編寫pipeline代碼
@Library("devops06@main") _
//import src/org/devops/Build.groovy
def build = new org.devops.Build()
def sonar = new org.devops.Sonar()
currentBuild.displayName = "${env.branchName}-commitID"
currentBuild.description = "Trigger by user jenkins \n branch: ${env.branchName}"
pipeline {
agent {label "build"}
options {
skipDefaultCheckout true
}
stages{
stage("CheckOut"){
steps{
script{
build.CheckOut()
}
}
}
stage("Build"){
steps{
script{
build.Build()
}
}
}
stage("CodeScan"){
when {
environment name: 'skipSonar', value: 'false'
}
steps{
script{
sonar.SonarScannerByPlugin()
}
}
}
stage("PushArtifact"){
steps{
script{
//PushArtifactByPlugin()
PushArtifactByPluginPOM()
}
}
}
}
}
def PushArtifactByPlugin(){
nexusArtifactUploader artifacts: [[artifactId: 'demo-app',
classifier: '',
file: 'target/demo-0.0.1-SNAPSHOT.jar',
type: 'jar']],
credentialsId: '3404937d-89e3-4699-88cf-c4bd299094ad',
groupId: 'com.devops6',
nexusUrl: '172.29.9.101:8081',
nexusVersion: 'nexus3',
protocol: 'http',
repository: 'maven-devops6-release',
version: '1.1.1'
}
def PushArtifactByPluginPOM(){
POM = readMavenPom file: 'pom.xml'
println(POM)
println("GroupID: ${POM.groupId}")
println("ArtifactID: ${POM.artifactId}")
println("Version: ${POM.version}")
println("Packaging: ${POM.packaging}")
env.artifactId = "${POM.artifactId}"
env.packaging = "${POM.packaging}"
env.groupId = "${POM.groupId}"
env.art_version = "${POM.version}"
env.art_name = "${env.artifactId}-${env.art_version}.${env.packaging}"
nexusArtifactUploader artifacts: [[artifactId: "${env.artifactId}",
classifier: '',
file: "target/${env.art_name}",
type: "${env.packaging}"]],
credentialsId: '3404937d-89e3-4699-88cf-c4bd299094ad',
groupId: "${env.groupId}",
nexusUrl: '172.29.9.101:8081',
nexusVersion: 'nexus3',
protocol: 'http',
repository: 'maven-devops6-release',
version: "${env.art_version}"
}
- 在回放里運(yùn)行
發(fā)現(xiàn)報(bào)錯(cuò):
將項(xiàng)目里的pom文件改成RELEASE版本。
將
改成
然后再次運(yùn)行:
又一次報(bào)錯(cuò),提示:status: 400 Repository does not allow updating assets: maven-devops6-release
應(yīng)該是nexus3倉(cāng)庫(kù)里之前已經(jīng)存在了這個(gè)包。
我們這里再改下version:
再次運(yùn)行:
這次就運(yùn)行成功了。
- 然后寫到共享庫(kù)里,再次運(yùn)行測(cè)試
構(gòu)建成功。
- 也可以修改上面這個(gè)版本,利用函數(shù)傳參來演示
pipeline代碼:
@Library("devops06@main") _
//import src/org/devops/Build.groovy
def build = new org.devops.Build()
def sonar = new org.devops.Sonar()
currentBuild.displayName = "${env.branchName}-commitID"
currentBuild.description = "Trigger by user jenkins \n branch: ${env.branchName}"
pipeline {
agent {label "build"}
options {
skipDefaultCheckout true
}
stages{
stage("CheckOut"){
steps{
script{
build.CheckOut()
}
}
}
stage("Build"){
steps{
script{
build.Build()
}
}
}
stage("CodeScan"){
when {
environment name: 'skipSonar', value: 'false'
}
steps{
script{
sonar.SonarScannerByPlugin()
}
}
}
stage("PushArtifact"){
steps{
script{
//PushArtifactByPlugin()
// PushArtifactByPluginPOM()
pomData = readMavenPom file: 'pom.xml'
buName = "${JOB_NAME}".split("-")[0]
repoName = "maven-${buName}-release"
file = "target/${pomData.artifactId}-${pomData.version}.${pomData.packaging}"
PushArtifactByPluginPOM(pomData.artifactId, file, pomData.packaging, pomData.groupId, repoName, pomData.version)
}
}
}
}
}
def PushArtifactByPlugin(){
nexusArtifactUploader artifacts: [[artifactId: 'demo-app',
classifier: '',
file: 'target/demo-0.0.1-SNAPSHOT.jar',
type: 'jar']],
credentialsId: '3404937d-89e3-4699-88cf-c4bd299094ad',
groupId: 'com.devops6',
nexusUrl: '172.29.9.101:8081',
nexusVersion: 'nexus3',
protocol: 'http',
repository: 'maven-devops6-release',
version: '1.1.1'
}
// def PushArtifactByPluginPOM(){
// POM = readMavenPom file: 'pom.xml'
// println(POM)
// println("GroupID: ${POM.groupId}")
// println("ArtifactID: ${POM.artifactId}")
// println("Version: ${POM.version}")
// println("Packaging: ${POM.packaging}")
// env.artifactId = "${POM.artifactId}"
// env.packaging = "${POM.packaging}"
// env.groupId = "${POM.groupId}"
// env.art_version = "${POM.version}"
// env.art_name = "${env.artifactId}-${env.art_version}.${env.packaging}"
// nexusArtifactUploader artifacts: [[artifactId: "${env.artifactId}",
// classifier: '',
// file: "target/${env.art_name}",
// type: "${env.packaging}"]],
// credentialsId: '3404937d-89e3-4699-88cf-c4bd299094ad',
// groupId: "${env.groupId}",
// nexusUrl: '172.29.9.101:8081',
// nexusVersion: 'nexus3',
// protocol: 'http',
// repository: 'maven-devops6-release',
// version: "${env.art_version}"
// }
//函數(shù)方式
def PushArtifactByPluginPOM(artifactId, file, type, groupId, repoName, version){
println(artifactId)
//demo
println("${file}, ${type}, ${groupId}, ${repoName}, ${version}")
//target/demo-0.0.1-SNAPSHOT.jar, jar, com.example, devops4-release, 0.0.1-SNAPSHOT
nexusArtifactUploader artifacts: [[artifactId: artifactId,
classifier: '',
file: file,
type: type]],
credentialsId: '3404937d-89e3-4699-88cf-c4bd299094ad',
groupId: groupId,
nexusUrl: '172.29.9.101:8081',
nexusVersion: 'nexus3',
protocol: 'http',
repository: repoName,
version: version
}
再次運(yùn)行:
發(fā)現(xiàn)這個(gè)方法也是ok的。(haha,函數(shù)方式感覺nb一點(diǎn),那這里就采用函數(shù)方式了哦。??)
然后把這部分代碼放到共享庫(kù)里,再次測(cè)試下:
符合預(yù)期。
此時(shí)共享庫(kù)里玩轉(zhuǎn)代碼如下:PushArtifact.groovy
文件:
package org.devops
def PushArtifactByPlugin(){
nexusArtifactUploader artifacts: [[artifactId: 'demo-app',
classifier: '',
file: 'target/demo-0.0.1-SNAPSHOT.jar',
type: 'jar']],
credentialsId: '3404937d-89e3-4699-88cf-c4bd299094ad',
groupId: 'com.devops6',
nexusUrl: '172.29.9.101:8081',
nexusVersion: 'nexus3',
protocol: 'http',
repository: 'maven-devops6-release',
version: '1.1.1'
}
// def PushArtifactByPluginPOM(){
// POM = readMavenPom file: 'pom.xml'
// println(POM)
// println("GroupID: ${POM.groupId}")
// println("ArtifactID: ${POM.artifactId}")
// println("Version: ${POM.version}")
// println("Packaging: ${POM.packaging}")
// env.artifactId = "${POM.artifactId}"
// env.packaging = "${POM.packaging}"
// env.groupId = "${POM.groupId}"
// env.art_version = "${POM.version}"
// env.art_name = "${env.artifactId}-${env.art_version}.${env.packaging}"
// nexusArtifactUploader artifacts: [[artifactId: "${env.artifactId}",
// classifier: '',
// file: "target/${env.art_name}",
// type: "${env.packaging}"]],
// credentialsId: '3404937d-89e3-4699-88cf-c4bd299094ad',
// groupId: "${env.groupId}",
// nexusUrl: '172.29.9.101:8081',
// nexusVersion: 'nexus3',
// protocol: 'http',
// repository: 'maven-devops6-release',
// version: "${env.art_version}"
// }
//函數(shù)方式
def PushArtifactByPluginPOM(artifactId, file, type, groupId, repoName, version){
println(artifactId)
//demo
println("${file}, ${type}, ${groupId}, ${repoName}, ${version}")
//target/demo-0.0.1-SNAPSHOT.jar, jar, com.example, devops4-release, 0.0.1-SNAPSHOT
nexusArtifactUploader artifacts: [[artifactId: artifactId,
classifier: '',
file: file,
type: type]],
credentialsId: '3404937d-89e3-4699-88cf-c4bd299094ad',
groupId: groupId,
nexusUrl: '172.29.9.101:8081',
nexusVersion: 'nexus3',
protocol: 'http',
repository: repoName,
version: version
}
Jenkinsfile
文件:
@Library("devops06@main") _
//import src/org/devops/Build.groovy
def build = new org.devops.Build()
def sonar = new org.devops.Sonar()
def pushartifact = new org.devops.PushArtifact()
currentBuild.displayName = "${env.branchName}-commitID"
currentBuild.description = "Trigger by user jenkins \n branch: ${env.branchName}"
pipeline {
agent {label "build"}
options {
skipDefaultCheckout true
}
stages{
stage("CheckOut"){
steps{
script{
build.CheckOut()
}
}
}
stage("Build"){
steps{
script{
build.Build()
}
}
}
stage("CodeScan"){
when {
environment name: 'skipSonar', value: 'false'
}
steps{
script{
sonar.SonarScannerByPlugin()
}
}
}
stage("PushArtifact"){
steps{
script{
//PushArtifactByPlugin()
// PushArtifactByPluginPOM()
pomData = readMavenPom file: 'pom.xml'
buName = "${JOB_NAME}".split("-")[0]
repoName = "maven-${buName}-release"
file = "target/${pomData.artifactId}-${pomData.version}.${pomData.packaging}"
pushartifact.PushArtifactByPluginPOM(pomData.artifactId, file, pomData.packaging, pomData.groupId, repoName, pomData.version)
}
}
}
}
}
測(cè)試完成。??
擴(kuò)展: 可以在Jenkins頁(yè)面添加參數(shù), 讓用戶輸入后進(jìn)行發(fā)布
?? 擴(kuò)展: 可以在Jenkins頁(yè)面添加參數(shù), 讓用戶輸入后進(jìn)行發(fā)布(測(cè)試成功)-2023.6.28 |
測(cè)試過程如下:
- 改寫代碼:
@Library("mylib@main") _ //加載共享庫(kù)
import org.devops.* // 導(dǎo)入庫(kù)
def checkout = new Checkout() //New實(shí)例化
def build = new Build()
def unittest = new UnitTest()
def sonar = new Sonar()
def gitcli = new GitLab()
//env.buildType = "${JOB_NAME}".split("-")[1]
//流水線
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")
//build.CodeBuild("${env.buildType}")
sh "${env.buildShell}"
}
}
}
/*stage("UnitTest"){
steps{
script{
unittest.CodeTest("${env.buildType}")
}
}
}*/
stage("CodeScan"){
when {
environment name: 'skipSonar', value: 'false'
}
steps{
script{
profileName = "${JOB_NAME}".split("-")[0]
sonar.Init("${JOB_NAME}", "java", profileName)
//commit-status
commitID = gitcli.GetCommitID()
groupName =profileName
projectID = gitcli.GetProjectID("${JOB_NAME}", groupName)
sonar.CodeScan("${env.branchName}", commitID, projectID)
}
}
}
stage("PushArtifact"){
steps{
script{
// 讀取pom文件獲取坐標(biāo)信息
// pomData = readMavenPom file: 'pom.xml'
// println(pomData)
// println(pomData.getClass())
buName = "${JOB_NAME}".split("-")[0]
repoName = "${buName}-snapshot"
file = "target/${env.artifactId}-${env.version}.${env.packaging}"
// 用戶輸入獲取坐標(biāo)信息
PushArtifactByNexusPlugin(env.artifactId, file, env.packaging ,env.groupId, repoName, env.version)
}
}
}
}
}
def PushArtifactByNexusPlugin(artifactId, file, type, groupId, repoName, version){
println(artifactId)
//demo
println("${file}, ${type}, ${groupId}, ${repoName}, ${version}")
//target/demo-0.0.1-SNAPSHOT.jar, jar, com.example, devops4-release, 0.0.1-SNAPSHOT
nexusArtifactUploader artifacts: [[artifactId: artifactId,
classifier: '',
file: file,
type: type]],
credentialsId: '2b44f51a-2374-4275-aeed-b720f4fbf937',
groupId: groupId,
nexusUrl: '172.29.9.101:8081',
nexusVersion: 'nexus3',
protocol: 'http',
repository: repoName,
version: version
}
- 在流水線里添加字符參數(shù)和選項(xiàng)參數(shù):
- 開始構(gòu)建:
觀察效果:
可以看到制品被成功上傳到制品庫(kù)了:
測(cè)試結(jié)束。??
Jenkins插件獲取包路徑
發(fā)布其實(shí)就是下載制品,然后將制品發(fā)送到目標(biāo)主機(jī),最后通過腳本或者指令啟動(dòng)程序。
這個(gè)其實(shí)沒多大意義。
?? 實(shí)踐:Jenkins插件獲取包路徑-2023.6.29(測(cè)試成功) |
- 實(shí)驗(yàn)環(huán)境
gitlab-ce:15.0.3-ce.0
jenkins:2.346.3-2-lts-jdk11
sonarqube:9.9.0-community
nexus3:3.53.0
- 實(shí)驗(yàn)軟件(無)
下面是下載制品的示例:
curl http://192.168.1.200:8081/repository/devops-maven/com/example/demo/1.1.10/demo-1.1.10.jar -o app.jar -uadmin:admin123
或者:
- 安裝插件
Maven Artifact ChoiceListProvider (Nexus)
- 創(chuàng)建一個(gè)流水線
創(chuàng)建選項(xiàng)參數(shù),選擇擴(kuò)展選項(xiàng)參數(shù)
,并配置:
- 點(diǎn)擊構(gòu)建,就出現(xiàn)效果了
用戶選擇制品后, 點(diǎn)擊構(gòu)建。此時(shí)可以想象,Jenkins下載這個(gè)包, 然后通過salt、ansible進(jìn)行發(fā)布部署。
測(cè)試結(jié)束。??
5、Nexus REST API
http://172.29.9.101:8081/#admin/system/api
NexusAPI調(diào)試方法
進(jìn)入設(shè)置頁(yè)面, 找到System
> API
, 即可進(jìn)入API調(diào)試頁(yè)面。
調(diào)試API /v1/components
, 點(diǎn)擊Try it out
才能填寫信息。
Maven格式倉(cāng)庫(kù)
- 填寫信息
- 點(diǎn)擊
Execute
此時(shí),包就上傳到nexus倉(cāng)庫(kù)了,然后可以拷貝代碼,放到流水線里集成。
curl -X 'POST' \
'http://172.29.9.101:8081/service/rest/v1/components?repository=maven-devops6-release' \
-H 'accept: application/json' \
-H 'Content-Type: multipart/form-data' \
-H 'NX-ANTI-CSRF-TOKEN: 0.6366498537413283' \
-H 'X-Nexus-UI: true' \
-F 'maven2.groupId=com.devops6' \
-F 'maven2.artifactId=demo-app' \
-F 'maven2.version=1.20.1' \
-F 'maven2.generate-pom=true' \
-F 'maven2.packaging=jar' \
-F 'maven2.asset1=@agent.jar' \
-F 'maven2.asset1.extension=jar'
Raw格式倉(cāng)庫(kù)
上傳jar包
- 填寫參數(shù)信息
- 點(diǎn)擊
執(zhí)行操作, 204表示成功。 我們可以復(fù)用這里的CURL指令, 最后封裝到Jenkins流水線當(dāng)中。
上傳圖片
- 填寫參數(shù):
- 點(diǎn)擊
Excute
上傳:
curl -X 'POST' \
'http://172.29.9.101:8081/service/rest/v1/components?repository=devops6' \
-H 'accept: application/json' \
-H 'Content-Type: multipart/form-data' \
-H 'NX-ANTI-CSRF-TOKEN: 0.6366498537413283' \
-H 'X-Nexus-UI: true' \
-F 'raw.directory=/devops/demo' \
-F 'raw.asset1=@xyyhg.png;type=image/png'
- 觀察效果:
上傳制品成功。
上傳制品
- curl -u admin:admin123 如果Nexus開啟了認(rèn)證需要配置認(rèn)證信息才能正常訪問。(不同的包的類型不同)
##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=@默認(rèn)標(biāo)題_自定義px_2020-10-01-0.png;type=image/png" \
-F "raw.asset1.filename=默認(rèn)標(biāo)題_自定義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"
- 上傳制品(maven類型的制品):
curl -X POST "http://192.168.1.200:8081/service/rest/v1/components?repository=devops-maven" \
-H "accept: application/json" \
-H "Content-Type: multipart/form-data" \
-F "maven2.groupId=com.newdevops" \
-F "maven2.artifactId=devopsapp" \
-F "maven2.version=1.1.5" \
-F "maven2.packaging=jar" \
-F "maven2.asset1=@demo-0.0.1-SNAPSHOT.jar;type=application/java-archive" \
-F "maven2.asset1.extension=demo-0.0.1-SNAPSHOT.jar" \
-u admin:admin123
下載制品
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
6、CI綜合實(shí)踐
?? 實(shí)踐:CI綜合實(shí)踐-2023.6.29(測(cè)試成功) |
- 實(shí)驗(yàn)環(huán)境
gitlab-ce:15.0.3-ce.0
jenkins:2.346.3-2-lts-jdk11
sonarqube:9.9.0-community
nexus3:3.53.0
- 實(shí)驗(yàn)軟件
鏈接:https://pan.baidu.com/s/1lxP-jO5gHKsvoNaJi7PE6A?pwd=0820
提取碼:08202023.6.29-實(shí)踐:CI綜合實(shí)踐(測(cè)試成功)
制品倉(cāng)庫(kù)規(guī)范
在開始引入制品的時(shí)候,就應(yīng)該制定制品庫(kù)的管理和使用規(guī)范。 有了標(biāo)準(zhǔn)化的規(guī)范之后, 就很容易實(shí)現(xiàn)自動(dòng)化。(為什么有些工作無法做成自動(dòng)化? -無標(biāo)準(zhǔn))
-
版本號(hào): 1.2.3 主次修
主版本號(hào):表示項(xiàng)目的重大架構(gòu)變更。 次版本號(hào):表示較大范圍的功能增加和變化。 修訂版本號(hào):表示重大Bug的修復(fù)。 里程碑版本:表示某一個(gè)版本的里程碑。
-
倉(cāng)庫(kù)名稱: 每個(gè)項(xiàng)目組一個(gè)倉(cāng)庫(kù) devops6
-
應(yīng)用:
- 應(yīng)用1: devops6-maven-service
- 應(yīng)用2: devops6-gradle-service
-
包名: 應(yīng)用名稱-版本號(hào)-類型
- 應(yīng)用1: devops6-maven-service-1.2.3.jar
- 應(yīng)用2: devops6-gradle-service-1.2.3.jar
-
倉(cāng)庫(kù)目錄結(jié)構(gòu): /應(yīng)用名稱/版本號(hào)/包名
- 應(yīng)用1: /devops6-maven-service/1.2.3/devops6-maven-service-1.2.3.jar
- 應(yīng)用2: /devops6-gradle-service/1.2.3/devops6-gradle-service-1.2.3.jar
創(chuàng)建nexus制品倉(cāng)庫(kù)
- 創(chuàng)建一個(gè)
raw
類型的倉(cāng)庫(kù)存放制品
創(chuàng)建Jenkins作業(yè)
這里復(fù)用之前的項(xiàng)目。
項(xiàng)目倉(cāng)庫(kù)創(chuàng)建版本分支
- 基于main分支創(chuàng)建一個(gè)版本分支
- 然后在Jenkins上修改選項(xiàng)參數(shù)
branchName
,添加RELEASE-1.1.1
內(nèi)容
編寫Jenkins代碼
@Library("devops06@main") _
//import src/org/devops/Build.groovy
def build = new org.devops.Build()
def sonar = new org.devops.Sonar()
currentBuild.displayName = "${env.branchName}-commitID"
currentBuild.description = "Trigger by user jenkins \n branch: ${env.branchName}"
env.branchName = "RELEASE-6.1.1"
println(env.branchName)
pipeline {
agent {label "build"}
options {
skipDefaultCheckout true
}
stages{
stage("CheckOut"){
steps{
script{
build.CheckOut()
}
}
}
stage("Build"){
steps{
script{
build.Build()
}
}
}
stage("CodeScan"){
when {
environment name: 'skipSonar', value: 'false'
}
steps{
script{
sonar.SonarScannerByPlugin()
}
}
}
stage("PushArtifact"){
steps{
script{
//PushArtifactByPlugin()
//PushArtifactByPluginPOM()
// init package info
appName = "${JOB_NAME}" //devops6-maven-service
repoName = appName.split('-')[0] //devops6
appVersion = "${env.branchName}".split("-")[-1] // RELEASE-1.1.1 1.1.1
targetDir="${JOB_NAME}/${appVersion}"
// 通過pom文件獲取包名稱
POM = readMavenPom file: 'pom.xml'
env.artifactId = "${POM.artifactId}"
env.packaging = "${POM.packaging}"
env.groupId = "${POM.groupId}"
env.art_version = "${POM.version}"
sourcePkgName = "${env.artifactId}-${env.art_version}.${env.packaging}"
pkgPath = "target"
targetPkgName = "${appName}-${appVersion}.${env.packaging}"
PushNexusArtifact(repoName, targetDir, pkgPath, sourcePkgName,targetPkgName)
}
}
}
}
}
//通過nexus api上傳制品--綜合實(shí)踐
def PushNexusArtifact(repoId, targetDir, pkgPath, sourcePkgName,targetPkgName){
//nexus api
withCredentials([usernamePassword(credentialsId: '3404937d-89e3-4699-88cf-c4bd299094ad', \
passwordVariable: 'PASSWD',
usernameVariable: 'USERNAME')]) {
sh """
curl -X 'POST' \
"http://172.29.9.101:8081/service/rest/v1/components?repository=${repoId}" \
-H 'accept: application/json' \
-H 'Content-Type: multipart/form-data' \
-F "raw.directory=${targetDir}" \
-F "raw.asset1=@${pkgPath}/${sourcePkgName};type=application/java-archive" \
-F "raw.asset1.filename=${targetPkgName}" \
-u ${USERNAME}:${PASSWD}
"""
}
}
//通過jenkins的nexus插件上傳制品
def PushArtifactByPlugin(){
nexusArtifactUploader artifacts: [[artifactId: 'demo-app',
classifier: '',
file: 'target/demo-0.0.1-SNAPSHOT.jar',
type: 'jar']],
credentialsId: '3404937d-89e3-4699-88cf-c4bd299094ad',
groupId: 'com.devops6',
nexusUrl: '172.29.9.101:8081',
nexusVersion: 'nexus3',
protocol: 'http',
repository: 'maven-devops6-release',
version: '1.1.1'
}
// 通過jenkins的nexus插件上傳制品(讀取pom文件)
// def PushArtifactByPluginPOM(){
// POM = readMavenPom file: 'pom.xml'
// println(POM)
// println("GroupID: ${POM.groupId}")
// println("ArtifactID: ${POM.artifactId}")
// println("Version: ${POM.version}")
// println("Packaging: ${POM.packaging}")
// env.artifactId = "${POM.artifactId}"
// env.packaging = "${POM.packaging}"
// env.groupId = "${POM.groupId}"
// env.art_version = "${POM.version}"
// env.art_name = "${env.artifactId}-${env.art_version}.${env.packaging}"
// nexusArtifactUploader artifacts: [[artifactId: "${env.artifactId}",
// classifier: '',
// file: "target/${env.art_name}",
// type: "${env.packaging}"]],
// credentialsId: '3404937d-89e3-4699-88cf-c4bd299094ad',
// groupId: "${env.groupId}",
// nexusUrl: '172.29.9.101:8081',
// nexusVersion: 'nexus3',
// protocol: 'http',
// repository: 'maven-devops6-release',
// version: "${env.art_version}"
// }
// 通過jenkins的nexus插件上傳制品(讀取pom文件)-函數(shù)方式
def PushArtifactByPluginPOM(artifactId, file, type, groupId, repoName, version){
println(artifactId)
//demo
println("${file}, ${type}, ${groupId}, ${repoName}, ${version}")
//target/demo-0.0.1-SNAPSHOT.jar, jar, com.example, devops4-release, 0.0.1-SNAPSHOT
nexusArtifactUploader artifacts: [[artifactId: artifactId,
classifier: '',
file: file,
type: type]],
credentialsId: '3404937d-89e3-4699-88cf-c4bd299094ad',
groupId: groupId,
nexusUrl: '172.29.9.101:8081',
nexusVersion: 'nexus3',
protocol: 'http',
repository: repoName,
version: version
}
- 運(yùn)行,觀察效果:
測(cè)試是ok的。
- 將代碼集成到共享庫(kù)
Jenkinsfile
文件
@Library("devops06@main") _
//import src/org/devops/Build.groovy
def build = new org.devops.Build()
def sonar = new org.devops.Sonar()
def artifact = new org.devops.Artifact()
currentBuild.displayName = "${env.branchName}-commitID"
currentBuild.description = "Trigger by user jenkins \n branch: ${env.branchName}"
env.branchName = "RELEASE-6.1.1"
println(env.branchName)
pipeline {
agent {label "build"}
options {
skipDefaultCheckout true
}
stages{
stage("CheckOut"){
steps{
script{
build.CheckOut()
}
}
}
stage("Build"){
steps{
script{
build.Build()
}
}
}
stage("CodeScan"){
when {
environment name: 'skipSonar', value: 'false'
}
steps{
script{
sonar.SonarScannerByPlugin()
}
}
}
stage("PushArtifact"){
steps{
script{
//PushArtifactByPlugin()
//PushArtifactByPluginPOM()
// init package info
appName = "${JOB_NAME}" //devops6-maven-service
repoName = appName.split('-')[0] //devops6
appVersion = "${env.branchName}".split("-")[-1] // RELEASE-1.1.1 1.1.1
targetDir="${JOB_NAME}/${appVersion}"
// 通過pom文件獲取包名稱
POM = readMavenPom file: 'pom.xml'
env.artifactId = "${POM.artifactId}"
env.packaging = "${POM.packaging}"
env.groupId = "${POM.groupId}"
env.art_version = "${POM.version}"
sourcePkgName = "${env.artifactId}-${env.art_version}.${env.packaging}"
pkgPath = "target"
targetPkgName = "${appName}-${appVersion}.${env.packaging}"
artifact.PushNexusArtifact(repoName, targetDir, pkgPath, sourcePkgName,targetPkgName)
}
}
}
}
}
Artifact.groovy
文件
package org.devops
//通過nexus api上傳制品--綜合實(shí)踐
def PushNexusArtifact(repoId, targetDir, pkgPath, sourcePkgName,targetPkgName){
//nexus api
withCredentials([usernamePassword(credentialsId: '3404937d-89e3-4699-88cf-c4bd299094ad', \
passwordVariable: 'PASSWD',
usernameVariable: 'USERNAME')]) {
sh """
curl -X 'POST' \
"http://172.29.9.101:8081/service/rest/v1/components?repository=${repoId}" \
-H 'accept: application/json' \
-H 'Content-Type: multipart/form-data' \
-F "raw.directory=${targetDir}" \
-F "raw.asset1=@${pkgPath}/${sourcePkgName};type=application/java-archive" \
-F "raw.asset1.filename=${targetPkgName}" \
-u ${USERNAME}:${PASSWD}
"""
}
}
//通過jenkins的nexus插件上傳制品
def PushArtifactByPlugin(){
nexusArtifactUploader artifacts: [[artifactId: 'demo-app',
classifier: '',
file: 'target/demo-0.0.1-SNAPSHOT.jar',
type: 'jar']],
credentialsId: '3404937d-89e3-4699-88cf-c4bd299094ad',
groupId: 'com.devops6',
nexusUrl: '172.29.9.101:8081',
nexusVersion: 'nexus3',
protocol: 'http',
repository: 'maven-devops6-release',
version: '1.1.1'
}
// 通過jenkins的nexus插件上傳制品(讀取pom文件)
// def PushArtifactByPluginPOM(){
// POM = readMavenPom file: 'pom.xml'
// println(POM)
// println("GroupID: ${POM.groupId}")
// println("ArtifactID: ${POM.artifactId}")
// println("Version: ${POM.version}")
// println("Packaging: ${POM.packaging}")
// env.artifactId = "${POM.artifactId}"
// env.packaging = "${POM.packaging}"
// env.groupId = "${POM.groupId}"
// env.art_version = "${POM.version}"
// env.art_name = "${env.artifactId}-${env.art_version}.${env.packaging}"
// nexusArtifactUploader artifacts: [[artifactId: "${env.artifactId}",
// classifier: '',
// file: "target/${env.art_name}",
// type: "${env.packaging}"]],
// credentialsId: '3404937d-89e3-4699-88cf-c4bd299094ad',
// groupId: "${env.groupId}",
// nexusUrl: '172.29.9.101:8081',
// nexusVersion: 'nexus3',
// protocol: 'http',
// repository: 'maven-devops6-release',
// version: "${env.art_version}"
// }
// 通過jenkins的nexus插件上傳制品(讀取pom文件)-函數(shù)方式
def PushArtifactByPluginPOM(artifactId, file, type, groupId, repoName, version){
println(artifactId)
//demo
println("${file}, ${type}, ${groupId}, ${repoName}, ${version}")
//target/demo-0.0.1-SNAPSHOT.jar, jar, com.example, devops4-release, 0.0.1-SNAPSHOT
nexusArtifactUploader artifacts: [[artifactId: artifactId,
classifier: '',
file: file,
type: type]],
credentialsId: '3404937d-89e3-4699-88cf-c4bd299094ad',
groupId: groupId,
nexusUrl: '172.29.9.101:8081',
nexusVersion: 'nexus3',
protocol: 'http',
repository: repoName,
version: version
}
- 運(yùn)行,觀察結(jié)果
符合預(yù)期。??
如何自動(dòng)識(shí)別項(xiàng)目的分支名選擇進(jìn)行構(gòu)建
- Jenkins安裝
Git Parameters
插件
- 我們?cè)?code>devops6-maven-service上基于main分支創(chuàng)建版本分支
RELEASE-6.2.1
- 編輯Jenkins項(xiàng)目
devops6-maven-service
,添加一個(gè)Git Parameter參數(shù)。
默認(rèn)什么都不過濾的話,保存后,點(diǎn)擊參數(shù)化構(gòu)建,現(xiàn)象如下:
可以看到構(gòu)建分支上回出現(xiàn)所有分支。
這里過濾下,只允許出現(xiàn)版本分支:
- 再次編輯共享庫(kù)代碼,重新修改下分支名
//使用git 參數(shù)需要格式化
env.branchName = "${env.branchName}" - "origin/"
println(env.branchName)
- 再次運(yùn)行流水線,觀察效果
代碼掃描:
CI上傳制品:
測(cè)試結(jié)束。??
完整共享庫(kù)代碼
Jenkinsfile
文件
@Library("devops06@main") _
//import src/org/devops/Build.groovy
def build = new org.devops.Build()
def sonar = new org.devops.Sonar()
def artifact = new org.devops.Artifact()
//使用git 參數(shù)需要格式化
env.branchName = "${env.branchName}" - "origin/"
println(env.branchName)
currentBuild.displayName = "${env.branchName}-commitID"
currentBuild.description = "Trigger by user jenkins \n branch: ${env.branchName}"
pipeline {
agent {label "build"}
options {
skipDefaultCheckout true
}
stages{
stage("CheckOut"){
steps{
script{
build.CheckOut()
}
}
}
stage("Build"){
steps{
script{
build.Build()
}
}
}
stage("CodeScan"){
when {
environment name: 'skipSonar', value: 'false'
}
steps{
script{
sonar.SonarScannerByPlugin()
}
}
}
stage("PushArtifact"){
steps{
script{
//PushArtifactByPlugin()
//PushArtifactByPluginPOM()
// init package info
appName = "${JOB_NAME}" //devops6-maven-service
repoName = appName.split('-')[0] //devops6
appVersion = "${env.branchName}".split("-")[-1] // RELEASE-1.1.1 1.1.1
targetDir="${JOB_NAME}/${appVersion}"
// 通過pom文件獲取包名稱
POM = readMavenPom file: 'pom.xml'
env.artifactId = "${POM.artifactId}"
env.packaging = "${POM.packaging}"
env.groupId = "${POM.groupId}"
env.art_version = "${POM.version}"
sourcePkgName = "${env.artifactId}-${env.art_version}.${env.packaging}"
pkgPath = "target"
targetPkgName = "${appName}-${appVersion}.${env.packaging}"
artifact.PushNexusArtifact(repoName, targetDir, pkgPath, sourcePkgName,targetPkgName)
}
}
}
}
}
Artifact.groovy
文件
package org.devops
//通過nexus api上傳制品--綜合實(shí)踐
def PushNexusArtifact(repoId, targetDir, pkgPath, sourcePkgName,targetPkgName){
//nexus api
withCredentials([usernamePassword(credentialsId: '3404937d-89e3-4699-88cf-c4bd299094ad', \
passwordVariable: 'PASSWD',
usernameVariable: 'USERNAME')]) {
sh """
curl -X 'POST' \
"http://172.29.9.101:8081/service/rest/v1/components?repository=${repoId}" \
-H 'accept: application/json' \
-H 'Content-Type: multipart/form-data' \
-F "raw.directory=${targetDir}" \
-F "raw.asset1=@${pkgPath}/${sourcePkgName};type=application/java-archive" \
-F "raw.asset1.filename=${targetPkgName}" \
-u ${USERNAME}:${PASSWD}
"""
}
}
//通過jenkins的nexus插件上傳制品
def PushArtifactByPlugin(){
nexusArtifactUploader artifacts: [[artifactId: 'demo-app',
classifier: '',
file: 'target/demo-0.0.1-SNAPSHOT.jar',
type: 'jar']],
credentialsId: '3404937d-89e3-4699-88cf-c4bd299094ad',
groupId: 'com.devops6',
nexusUrl: '172.29.9.101:8081',
nexusVersion: 'nexus3',
protocol: 'http',
repository: 'maven-devops6-release',
version: '1.1.1'
}
// 通過jenkins的nexus插件上傳制品(讀取pom文件)
// def PushArtifactByPluginPOM(){
// POM = readMavenPom file: 'pom.xml'
// println(POM)
// println("GroupID: ${POM.groupId}")
// println("ArtifactID: ${POM.artifactId}")
// println("Version: ${POM.version}")
// println("Packaging: ${POM.packaging}")
// env.artifactId = "${POM.artifactId}"
// env.packaging = "${POM.packaging}"
// env.groupId = "${POM.groupId}"
// env.art_version = "${POM.version}"
// env.art_name = "${env.artifactId}-${env.art_version}.${env.packaging}"
// nexusArtifactUploader artifacts: [[artifactId: "${env.artifactId}",
// classifier: '',
// file: "target/${env.art_name}",
// type: "${env.packaging}"]],
// credentialsId: '3404937d-89e3-4699-88cf-c4bd299094ad',
// groupId: "${env.groupId}",
// nexusUrl: '172.29.9.101:8081',
// nexusVersion: 'nexus3',
// protocol: 'http',
// repository: 'maven-devops6-release',
// version: "${env.art_version}"
// }
// 通過jenkins的nexus插件上傳制品(讀取pom文件)-函數(shù)方式
def PushArtifactByPluginPOM(artifactId, file, type, groupId, repoName, version){
println(artifactId)
//demo
println("${file}, ${type}, ${groupId}, ${repoName}, ${version}")
//target/demo-0.0.1-SNAPSHOT.jar, jar, com.example, devops4-release, 0.0.1-SNAPSHOT
nexusArtifactUploader artifacts: [[artifactId: artifactId,
classifier: '',
file: file,
type: type]],
credentialsId: '3404937d-89e3-4699-88cf-c4bd299094ad',
groupId: groupId,
nexusUrl: '172.29.9.101:8081',
nexusVersion: 'nexus3',
protocol: 'http',
repository: repoName,
version: version
}
關(guān)于我
我的博客主旨:
- 排版美觀,語言精煉;
- 文檔即手冊(cè),步驟明細(xì),拒絕埋坑,提供源碼;
- 本人實(shí)戰(zhàn)文檔都是親測(cè)成功的,各位小伙伴在實(shí)際操作過程中如有什么疑問,可隨時(shí)聯(lián)系本人幫您解決問題,讓我們一起進(jìn)步!
?? 微信二維碼
x2675263825 (舍得), qq:2675263825。
?? 微信公眾號(hào)
《云原生架構(gòu)師實(shí)戰(zhàn)》
?? csdn
https://blog.csdn.net/weixin_39246554?spm=1010.2135.3001.5421
?? 博客
www.onlyyou520.com
?? 知乎
https://www.zhihu.com/people/foryouone
?? 語雀
https://www.yuque.com/books/share/34a34d43-b80d-47f7-972e-24a888a8fc5e?# 《不服來懟:宇宙中最好用的云筆記!》
最后
好了,關(guān)于本次就到這里了,感謝大家閱讀,最后祝大家生活快樂,每天都過的有意義哦,我們下期見!文章來源:http://www.zghlxwxcb.cn/news/detail-707088.html
文章來源地址http://www.zghlxwxcb.cn/news/detail-707088.html
到了這里,關(guān)于Devops之制品庫(kù)平臺(tái)實(shí)踐-nexus的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!