jenkins pipeline指定分支和tag
如下pipeline可以指定github仓库的分支和tag(使用git参数,插件地址 https://plugins.jenkins.io/git-parameter/)
pipeline {
agent any
parameters {
gitParameter name: 'BRANCH_TAG',
type: 'PT_BRANCH_TAG',
branchFilter: 'origin/(.*)',
defaultValue: 'main',
selectedValue: 'DEFAULT',
sortMode: 'DESCENDING_SMART',
description: 'Select your branch or tag.'
}
stages {
stage('Example') {
steps {
checkout([$class: 'GitSCM',
branches: [[name: "${params.BRANCH_TAG}"]],
doGenerateSubmoduleConfigurations: false,
extensions: [],
gitTool: 'Default',
submoduleCfg: [],
userRemoteConfigs: [[url: 'https://github.com/xinxiaoyu/test_git.git', credentialsId: '',]]
])
}
}
}
}
这个时候工作是正常的,分支和tag会对应显示test_git仓库。但是如果在第一行加入共享库’@Library(‘pipeline-library’) _‘则会显示共享库的分支tag,这显然冲突了。 可以用list git braches parameter插件(地址 https://plugins.jenkins.io/list-git-branches-parameter/ )
@Library('pipeline-library') _
pipeline {
agent any
environment {
GITLAB_CREDENTIAL_ID = ''
GITLAB_URL = 'https://github.com/xinxiaoyu/test_git.git'
web_site = 'xinxiaoyu.github.io'
BRANCH = 'main'
}
parameters {
listGitBranches branchFilter: 'refs/heads/(.*)',
defaultValue: 'main',
name: 'BRANCH_TAG',
type: 'PT_BRANCH_TAG', // PT_BRANCH, PT_TAG
remoteURL: 'https://github.com/xinxiaoyu/test_git.git',
credentialsId: '',
selectedValue: 'DEFAULT',
sortMode: 'ASCENDING',
quickFilterEnabled: true
}
stages {
stage ('Checkout') {
steps {
git(credentialsId: "${env.GITLAB_CREDENTIAL_ID}", url: "${env.GITLAB_URL}", branch: "${env.BRANCH_TAG}", changelog: true, poll: false)
}
}
stage('library test') {
steps {
getCode()
}
}
}
}