Jenkins Git Parameter 插件

Git Parameter

官网地址:Git Parameter

该插件允许您在构建中分配 git 分支、标签、拉取请求或修订号作为参数。

示例管道脚本

分支类型 - 基本用法

声明式管道

// Using git without checkout
pipeline {
  agent any
  parameters {
    gitParameter branchFilter: 'origin/(.*)', defaultValue: 'master', name: 'BRANCH', type: 'PT_BRANCH'
  }
  stages {
    stage('Example') {
      steps {
        git branch: "${params.BRANCH}", url: 'https://github.com/jenkinsci/git-parameter-plugin.git'
      }
    }
  }
}

脚本流水线

properties([
    parameters([
        gitParameter(branch: '',
                     branchFilter: 'origin/(.*)',
                     defaultValue: 'master',
                     description: '',
                     name: 'BRANCH',
                     quickFilterEnabled: false,
                     selectedValue: 'NONE',
                     sortMode: 'NONE',
                     tagFilter: '*',
                     type: 'PT_BRANCH')
    ])
])
node {
    git branch: "${params.BRANCH}", url: 'https://github.com/jenkinsci/git-parameter-plugin.git'
}

重要设置:

  • 应该设置一个 default 值,因为初始构建必须获取此信息
  • 使用 git 应设置 branchFilter 为 *origin/(.\*)* (origin 是远程服务器名称)

参数类型:

  • PT_TAG
  • PT_BRANCH
  • PT_BRANCH_TAG
  • PT_REVISION
  • PT_PULL_REQUEST

如果您需要使用其他类型(除分支之外)参数,则必须在其中使用 git checkout

标签类型

// Using git within checkout
pipeline {
    agent any
    parameters {
        gitParameter name: 'TAG',
                     type: 'PT_TAG',
                     defaultValue: 'master'
    }
    stages {
        stage('Example') {
            steps {
                checkout([$class: 'GitSCM',
                          branches: [[name: "${params.TAG}"]],
                          doGenerateSubmoduleConfigurations: false,
                          extensions: [],
                          gitTool: 'Default',
                          submoduleCfg: [],
                          userRemoteConfigs: [[url: 'https://github.com/jenkinsci/git-parameter-plugin.git']]
                        ])
            }
        }
    }
}

分支标签类型

pipeline {
    agent any
    parameters {
        gitParameter name: 'BRANCH_TAG',
                     type: 'PT_BRANCH_TAG',
                     defaultValue: 'master'
    }
    stages {
        stage('Example') {
            steps {
                checkout([$class: 'GitSCM',
                          branches: [[name: "${params.BRANCH_TAG}"]],
                          doGenerateSubmoduleConfigurations: false,
                          extensions: [],
                          gitTool: 'Default',
                          submoduleCfg: [],
                          userRemoteConfigs: [[url: 'https://github.com/jenkinsci/git-parameter-plugin.git']]
                        ])
            }
        }
    }
}

修订类型

pipeline {
    agent any
    parameters {
        gitParameter name: 'REVISION',
                     type: 'PT_REVISION',
                     defaultValue: 'master'
    }
    stages {
        stage('Example') {
            steps {
                checkout([$class: 'GitSCM',
                          branches: [[name: "${params.REVISION}"]],
                          doGenerateSubmoduleConfigurations: false,
                          extensions: [],
                          gitTool: 'Default',
                          submoduleCfg: [],
                          userRemoteConfigs: [[url: 'https://github.com/jenkinsci/git-parameter-plugin.git']]
                        ])
            }
        }
    }
}

拉取请求类型

pipeline {
    agent any
    parameters {
        gitParameter name: 'PULL_REQUESTS',
                     type: 'PT_PULL_REQUEST',
                     defaultValue: '1',
                     sortMode: 'DESCENDING_SMART'
    }
    stages {
        stage('Example') {
            steps {
                checkout([$class: 'GitSCM',
                branches: [[name: "pr/${params.PULL_REQUESTS}/head"]],
                doGenerateSubmoduleConfigurations: false,
                extensions: [],
                gitTool: 'Default',
                submoduleCfg: [],
                userRemoteConfigs: [[refspec: '+refs/pull/*:refs/remotes/origin/pr/*', url: 'https://github.com/jenkinsci/git-parameter-plugin.git']]])
            }
        }
    }
}

选项

参数类型

在管道中使用的名称

type: 'PT_TAG' or 'PT_BRANCH' or 'PT_BRANCH_TAG' or 'PT_REVISION' or 'PT_PULL_REQUEST'

解释 PT_TAGPT_BRANCHPT_BRANCH_TAG

使用 git ls-remote 命令获取远程标签或分支的插件。

在使用来自 Git Client 的 getRemoteReferences 的代码插件中,查看 CliGitAPIImpl 中的实现。

package org.jenkinsci.plugins.gitclient
//...

public interface GitClient {
//...
    Map<String, ObjectId> getRemoteReferences(String remoteRepoUrl, String pattern, boolean headsOnly, boolean tagsOnly) throws GitException, InterruptedException;
//...
}

分支

在管道中使用的名称

branch

分支过滤器

在管道中使用的名称

branchFilter

标签过滤器

在管道中使用的名称

tagFilter

排序模式

在管道中使用的名称

sortMode: 'NONE' or 'ASCENDING_SMART' or 'DESCENDING_SMART' or 'ASCENDING' or 'DESCENDING'

您可以为 tags/revision/branches/branches_or_tags/pull 请求选择以下排序选项

  • none
  • descending
  • ascending
  • ascending smart
  • descending smart

对于智能变体,比较将数字序列视为单个字符。由格雷姆希尔提供。

默认值

在管道中使用的名称

defaultValue

在 0.9.9 或更高版本中,最好设置一个默认值,因为该值使用初始构建(在管道中)。

获取数据发生错误时返回默认值。

选定值

在管道中使用的名称

selectedValue: 'NONE' or 'TOP' or 'DEFAULT'

使用存储库

在管道中使用的名称

useRepository

您没有在插件中设置 git 存储库,此插件使用在 SCM 部分的项目中定义的 git 存储库!

如果在任务中定义了多个存储库,则此选项指定在获取数据时要考虑哪个存储库。

如果未定义该选项,则采用第一个定义的存储库。

此选项是一个正则表达式,与 “存储库 URL” 进行比较。

您可以通过几种方式定义多个 SCM,您可以使用 Multiple SCMs Plugin,在一个 SCM 中指定多个 “存储库 URL” 或在管道中定义它们。

考虑一个基于两个存储库的示例:

https://github.com/klimas7/exampleA.git
https://github.com/klimas7/exampleB.git

  • 管道:复杂示例

    pipeline {
        agent any
        parameters {
            gitParameter branchFilter: 'origin.*/(.*)', defaultValue: 'master', name: 'BRANCH_A', type: 'PT_BRANCH', useRepository: '.*exampleA.git'
            gitParameter branchFilter: 'origin.*/(.*)', defaultValue: 'master', name: 'BRANCH_B', type: 'PT_BRANCH', useRepository: '.*exampleB.git'
    
        }
        stages {
            stage('Example') {
                steps {
                    git branch: "${params.BRANCH_A}", url: 'https://github.com/klimas7/exampleA.git'
                    git branch: "${params.BRANCH_B}", url: 'https://github.com/klimas7/exampleB.git'
                }
            }
        }
    }
    

未设置 “使用存储库” 时的示例:

  • 管道:未设置使用存储库

    pipeline {
        agent any
        parameters {
            gitParameter branchFilter: 'origin.*/(.*)', defaultValue: 'master', name: 'BRANCH', type: 'PT_BRANCH'
        }
        stages {
            stage('Example') {
                steps {
                    git url: 'https://github.com/klimas7/exampleA.git'
                    dir('dir-for-exampleB') {
                        git url: 'https://github.com/klimas7/exampleB.git'
    		}
                }
            }
        }
    }
    
  • 5
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值