[09] Jenkins: Pipeline supplement

EDITORIAL words

 

We used when using an ordinary task of building a Sonar make code quality management, also used Publish Over SSH plug-in updates on-line, but how do we use them in the Pipeline.

Sonar section if you did not see before, it is recommended to go back and read through special:

https://www.cnblogs.com/Dy1an/p/11198822.html

Otherwise, some concepts can not understand!

 

 

Pipeline use Sonar

 

Prerequisite for our operations and are installed Sonar, specifically how the installation can refer to previous chapters.

1. Token Sonar configuration in which:

Token generation, pay attention to the Token display only once, pay attention to save:

 

2. Jenkins System Management - Configuration Sonar> System Settings, if you do follow the previous section, there should be configured in.

 

3. New Pipeline tasks:

 

 

3. Next is the focus of the operation, configuration Pipeline:

Prior to this, we need to know a few key, Sonar scans of the code at the time will be used:

 

[1] Sonar scanning tool Sonar Scaner: System Management -> Global Configuration Tool

 

[2] Certification of Sonar: System Management -> System Settings

Of course, both the configuration we can automatically generate syntax form, but we need to get to know his principles, we Sonar subsequent use in the Pipeline in fact, note that rely on these two.

In line syntax, we have Mr. Cheng Sonar Home Path:

Sonar generate certified syntax:

By withSonarEnv Token can be generated by a corresponding authentication grammar.

At this point, we can write the entire Pipeline method used in the Sonar:

This is the basic wording of the final Sonar Pipeline:

node {
    stage("拉取代码"){
        echo 'STEP 1:Clone code'
        git credentialsId: 'xxxx', url: 'http://192.168.10.199:8041/xxxx.git'
    }
    stage('代码质量检测') { 
        echo 'STEP 2:Sonar code check'
        // 获取 Sonar Scaner 家目录并赋值给变量
        def SonarScannerHome = tool name: 'xxxx'
        withSonarQubeEnv(credentialsId: 'xxxx') {
            sh "${SonarScannerHome}/bin/sonar-scanner -X "+
            "-Dsonar.host.url=http://192.168.10.202:9000 " +
            "-Dsonar.language=java " + 
            "-Dsonar.projectKey=Pipeline-Sonar-Test " + 
            "-Dsonar.projectName=Pipeline-Sonar-Test " + 
            "-Dsonar.sources=./ " + 
            "-Dsonar.sourceEncoding=UTF-8 " + 
            "-Dsonar.java.binaries=./ "    
        }
    }
}

红色 xxxx 部分需要替换成为自己生成的。

当然,我们其实可以将验证直接写为 withSonarQubeEnv('Sonar') ,Sonar 是我们添加验证时候上图中的 Name 属性。

至于后面 Sonar 扫描的语法,其实就是我们之前普通任务扫描时候的参数。具体含义可以回去看之前的章节。

 

4. 查看构建结果:

Sonar 中查看:

至此,Pipeline 中使用 Sonar 到这里完成。

 

 

Pipeline 中使用 Publish Over SSH


在新版本的 Publish Over SSH 插件中已经支持了 Pipeline,这意味着,我们在语法生成器中可以直接生成:

 

最终生成的语法如下:

配置示例:

node {
    stage("拉取代码"){
        echo 'STEP 1:clone code'
        git credentialsId: 'xxxx', url: 'http://192.168.10.199:8041/xxxx.git'
    }
    stage("打包代码"){
        echo 'STEP 2:code package'
        withEnv(['JAVA_HOME=/data/jdk7']) {
            sh '/data/maven/bin/mvn -e clean package -U -Dmaven.test.skip=true -Ptest'
        }
    }
    stage("上线发布"){
        echo 'STEP 3:deploy package'
        sshPublisher(publishers: xxxx)
    }
}

红色 xxxx 部分为自己环境的配置,构建后在 Blue Ocean 中查看输出:

 

 

Pipeline 中触发另外一个 Job

 

在语法生成器中,我们可以选择 build a job 进行添加:

 

 

小结

 

当然还有一些其他用法,我这里只列举了一下常用的用法,至于其他的用法,后续用到的时候再来增加。 

Guess you like

Origin www.cnblogs.com/Dy1an/p/11225035.html