IOTXING

记录技术学习之路

0%

Jenkins node踩坑

Jenkins node自动切换

之前在使用pipeline的时候,写法都是下面这种写法

stage("第一个阶段"){
    node{
        xxx
    }
}
stage("第二个阶段"){
    node{
        xxx
    }
}

这种写法是每个阶段都选择一个节点去执行任务,在单节点jenkins的时候这样写是没有任何问题的。但是因为最近在扩展jenkins节点,所以就相当于踩到了坑。

在执行pipeline的时候,遇到了下面的错误

[Pipeline] echo
cdaa19b
[Pipeline] }
[Pipeline] // ws
[Pipeline] }
[Pipeline] // node
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (代码扫描)
[Pipeline] node
Running on 192.168.103.36 in /tmp/workspace/workspace/rocket-pipeline
[Pipeline] {
[Pipeline] ws
Running in /tmp/workspace/springboot-module-demo/test/7754
[Pipeline] {
[Pipeline] withSonarQubeEnv
Injecting SonarQube environment variables using the configuration: sonar
[Pipeline] {
[Pipeline] sh
+ /usr/local/apache-maven-3.6.1/bin/mvn -Dmaven.test.skip=true -Dmaven.compile.fork=true clean package scan:mapper -Dscanmapper.appId=springboot-module-demo -Dscanmapper.xconf.zkAddr=192.168.2.40:2181,192.168.2.40:2182,192.168.2.40:2183 -Dscanmapper.abortWhenFoundDanger=false -Dscanmapper.check.enableHostPortConversion=true -Dscanmapper.intranet=true sonar:sonar -P test -U
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  0.099 s
[INFO] Finished at: 2019-06-14T17:44:19+08:00
[INFO] ------------------------------------------------------------------------

首先提示的是mvn执行错误,然后我去到对应的文件夹,发现并没有任何文件,那么肯定是无法mvn 打包了。至于为什么没有文件,只能往前面查

Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] stage
[Pipeline] { (拉取GIT代码)
[Pipeline] node
Running on Jenkins in /root/.jenkins/workspace/rocket-pipeline
[Pipeline] {
[Pipeline] ws
Running in /tmp/workspace/springboot-module-demo/test/7754
[Pipeline] {
[Pipeline] checkout
using credential deployKey
Cloning the remote Git repository
Cloning repository git@10.24.255.7:devops/springboot-module-demo.git
 > git init /tmp/workspace/springboot-module-demo/test/7754 # timeout=10
Fetching upstream changes from git@10.24.255.7:devops/springboot-module-demo.git
> git --version # timeout=10
using GIT_SSH to set credentials 
> git fetch --tags --progress git@10.24.255.7:devops/springboot-module-demo.git +refs/heads/*:refs/remotes/origin/*
> git config remote.origin.url git@10.24.255.7:devops/springboot-module-demo.git # timeout=10
> git config --add remote.origin.fetch +refs/heads/*:refs/remotes/origin/* # timeout=10
> git config remote.origin.url git@10.24.255.7:devops/springboot-module-demo.git # timeout=10
Fetching upstream changes from git@10.24.255.7:devops/springboot-module-demo.git
using GIT_SSH to set credentials 
> git fetch --tags --progress git@10.24.255.7:devops/springboot-module-demo.git +refs/heads/*:refs/remotes/origin/*
> git rev-parse origin/master^{commit} # timeout=10
Checking out Revision cdaa19ba58bfde0d01079a0667fce5a241b4e0ba (origin/master)
> git config core.sparsecheckout # timeout=10
> git checkout -f cdaa19ba58bfde0d01079a0667fce5a241b4e0ba
Commit message: "Merge remote-tracking branch 'remotes/origin/master' into daily"
> git rev-list --no-walk cdaa19ba58bfde0d01079a0667fce5a241b4e0ba # timeout=10
[Pipeline] sh
+ git log -n 1 --pretty=format:%h
[Pipeline] echo
cdaa19b
[Pipeline] }
[Pipeline] // ws
[Pipeline] }
[Pipeline] // node
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (代码扫描)
[Pipeline] node
Running on 192.168.103.36 in /tmp/workspace/workspace/rocket-pipeline
[Pipeline] {
[Pipeline] ws
Running in /tmp/workspace/springboot-module-demo/test/7754

看到这里,大概就明白了,拉取git代码的时候是在master机器上面拉取的,然后切换到了node节点去进行执行了,因为没有挂NAS,自然是没有这些文件了。

调整一下思路,让pipeline在一开始的时候就切换好节点,这样就避免在不同的节点上面进行文件的操作

node{
    stage(){
        xxx
    }
    stage(){
        xxx
    }
}

针对上面这种问题,也可以有另外一个解决方案:挂NAS,也就是说jenkins的所有机器都共享一个数据盘,这样就能够避免因为node的切换导致文件的找不到。