美文网首页
Jenkins常用插件

Jenkins常用插件

作者: heichong | 来源:发表于2023-03-07 16:06 被阅读0次

我们通过Jenkins做自动化部署的过程中,建议安装以下插件

Simple Theme 插件

使用此插件可以隐藏pipeline打印的命令。比如下面的echo:

image.png
  • 安装


    image.png
  • 配置
    在 Manage Jenkins / Configure System / Theme 中,Add / Extra CSS,设置如下样式以隐藏:
.pipeline-new-node {
    display: none;
}

Docker Pipeline

如果我们想在pipeline中使用docker作为agent,则必须安装此插件
Error: Invalid agent type "docker" specified.
You have to install 2 plugins: Docker plugin and Docker Pipeline plugin.

image.png image.png

Pipeline Groovy Libraries

Pipeline Groovy Libraries是用来配置Jenkins Shared Library的。
Jenkins Shared Library可以把Pipeline中的片段抽象成通用的脚本,为不同的pipeline(比如maven\vue)等共用。

  • 安装


    image.png
  • 配置
    Manage Jenkins » Configure System » Global Pipeline Libraries

image.png image.png image.png

Pipeline utility steps

如果想在pipeline的脚本中读取yaml配置文件,需要此插件
n: groovy.lang.MissingMethodException: No signature of method: readYaml.call()
要使用readYaml,jenkins必须安装插件:pipeline utility steps

  • 安装


    image.png
  • 用法
#!groovy
def call() {
    Map cfg = readYaml file: "pipeline.yaml"
    return cfg
}

Active Choices插件

Jenkins Params默认情况下,只能输入一些简单的select或input,相互之间无法做联动。这时就需要Active Choices插件

image.png
  • 例如:
    发布时,我有两个参数:发布的环境(test/pre/prod)、发布的分支
    需求:当环境为prod时,分支只能为master,不能修改;当环境为test/pre时,分支默认是dev,可以修改
    pipeline 如下:

    pipeline {
        agent any

        stages {
            stage('Params') {
                steps {
                    script {
                   properties([parameters([
                        [$class: 'DynamicReferenceParameter',
                         choiceType: 'ET_FORMATTED_HTML',
                         description: '请选择要发布的环境(test:测试、pre:预生产、prod:生产)',
                         name: 'envType',
                         omitValueField: true,
                         script: [
                                 $class: 'GroovyScript',
                                 fallbackScript: [
                                         classpath: [],
                                         sandbox: true,
                                         script:
                                                 "return['Could not get The environemnts']"
                                 ],
                                 script: [
                                         classpath: [],
                                         sandbox: true,
                                         script:'''
                                            return "<select name='value' class='setting-input' style='width:50%'> <option value='test' selected='true'>test</option><option value='pre'>pre</option><option value='prod'>prod</option></select>"
                                        '''
                                 ]
                         ]
                        ],
                        [$class: 'DynamicReferenceParameter',
                         choiceType: 'ET_FORMATTED_HTML',
                         description: '请选择要发布的分支(prod环境下只能选择master分支)',
                         name: 'branch',
                         referencedParameters: 'envType',
                         // 删除原始的input,否则后端获取到的值会两个input用逗号拼接出来的(即多出一个,号)
                         omitValueField: true,
                         script: [
                                 $class: 'GroovyScript',
                                 fallbackScript: [
                                         classpath: [],
                                         sandbox: true,
                                         script:
                                                 "return['Could not get The environemnts']"
                                 ],
                                 script: [
                                         classpath: [],
                                         sandbox: true,
                                         script:'''
                                                    if (envType.equals("prod")){
                                                        return "<input name='value' class='setting-input' type='text' style='width:50%;background-color: lightgray;' value='master' disabled>"
                                                    }
                                                    else{
                                                        return "<input name='value' class='setting-input' type='text' style='width:50%' value='dev'>"
                                                    }
                                                    '''
                                 ]
                         ]
                        ]
                ])
        ])
                    }
                }
            }
            stage('Checkout') {
                steps {
                echo '...'
                }
            }
            stage('Test') {
                steps {
                echo '...'
            }
            stage('Build') {
                steps {
                echo '...'
                }
            }
            stage('Deploy') {
                steps {
                echo '...'
                }
            }
        }

        post {
            always {
                echo '...'
            }
        }
    }

相关文章

网友评论

      本文标题:Jenkins常用插件

      本文链接:https://www.haomeiwen.com/subject/filjldtx.html