How to configure a Jenkins 2 Pipeline so that Jenkinsfile uses a predefined variable -
i have several projects use jenkinsfile practically same. difference git project has checkout. forces me have 1 jenkinsfile per project although share same one:
node{ def mvnhome = tool 'm3' def artifactid def pomversion stage('commit stage'){ echo 'downloading git...' git branch: 'develop', credentialsid: 'xxx', url: 'https://bitbucket.org/xxx/yyy.git' echo 'building project , generating docker image...' sh "${mvnhome}/bin/mvn clean install docker:build -dskiptests" ...
is there way preconfigure git location variable during job creation can reuse same jenkinsfile?
... stage('commit stage'){ echo 'downloading git...' git branch: 'develop', credentialsid: 'xxx', url: env.git_repo_location ...
i know can set way:
this project parameterized -> string parameter -> git_repo_location, default= http://xxxx, , access env.git_repo_location.
the downside user promted start build default value or change it. need transparent user. there way it?
you can use pipeline shared groovy library plugin have library projects share in git repository. in documentation can read in detail.
if have lot of pipelines similar, global variable mechanism provides handy tool build higher-level dsl captures similarity. example, jenkins plugins built , tested in same way, might write step named buildplugin:
// vars/buildplugin.groovy def call(body) { // evaluate body block, , collect configuration object def config = [:] body.resolvestrategy = closure.delegate_first body.delegate = config body() // build, based on configuration provided node { git url: "https://github.com/jenkinsci/${config.name}-plugin.git" sh "mvn install" mail to: "...", subject: "${config.name} plugin build", body: "..." } }
assuming script has either been loaded global shared library or folder-level shared library resulting jenkinsfile dramatically simpler:
jenkinsfile (scripted pipeline)
buildplugin { name = 'git' }
the example shows how jenkinsfile passes name = git library. use similar setup , happy it.
Comments
Post a Comment