github environment variables in jenkinsfile pipeline

609 views
Skip to first unread message

Leonidas Kanellopoulos

unread,
Sep 27, 2017, 9:39:24 PM9/27/17
to Jenkins Users
I have a multi repo job and want to check out in one pipeline 2 branches ( 1 from each repo ) 

From repo A I Poll for a feature*  and then I want to attempt to build repo B with that same feature* or fallback to develop branch.

In the GUI job version I was using checkout to specific local branch with ** which populated the GIT_LOCAL_BRANCH env variable. and hence repo B had alternative strategy checkout with 
GIT_LOCAL_BRANCH or develop. 

In the jenkinsfile pipeline
the env,GIT_LOCAL_BRANCH always returns null . 


I read online that this variable is only accessed for a multibranch pipeline ?

Is there no other way to get the git branch that  was just checked out so I can pass it to the resolve scm step ? 


Below is the jenkinsfile.

node {

  stage ('Stage CheckOut 1'){ 
   
//Repo A ( has Polling )    
 checkout([$class: 'GitSCM', branches: [[name: '*/feature**']], 
 doGenerateSubmoduleConfigurations: false, extensions: [[$class:
 'RelativeTargetDirectory', relativeTargetDir: 'android'], [$class:'LocalBranch', localBranch: '**']],
 submoduleCfg: [], userRemoteConfigs:
 [[credentialsId: '', url:

//Checking if similar branch name as repo A exists in repo B 
 def b  = resolveScm source: [$class: 'GitSCMSource', credentialsId: '',
 id: '_', remote: 'https://github.com/.....git', traits: [[$class: 'jenkins.plugins.git.traits.BranchDiscoveryTrait']]],
 targets: [env. GIT_LOCAL_BRANCH, 'develop']
 checkout b
 
  }   
  }  

Mark Waite

unread,
Sep 27, 2017, 9:43:34 PM9/27/17
to jenkins...@googlegroups.com
On Wed, Sep 27, 2017 at 7:39 PM Leonidas Kanellopoulos <leonid...@gmail.com> wrote:
I have a multi repo job and want to check out in one pipeline 2 branches ( 1 from each repo ) 

From repo A I Poll for a feature*  and then I want to attempt to build repo B with that same feature* or fallback to develop branch.

In the GUI job version I was using checkout to specific local branch with ** which populated the GIT_LOCAL_BRANCH env variable. and hence repo B had alternative strategy checkout with 
GIT_LOCAL_BRANCH or develop. 

In the jenkinsfile pipeline
the env,GIT_LOCAL_BRANCH always returns null . 


If you're using Jenkins 2.60 or newer, and the latest releases of the pipeline plugins, then you can assign the return value of checkout to a groovy variable and use that as a map to access the name of the branch.

def repoMap = checkout( /* your setup here */ )

then access the contents of repoMpa.
 

I read online that this variable is only accessed for a multibranch pipeline ?

Is there no other way to get the git branch that  was just checked out so I can pass it to the resolve scm step ? 


Below is the jenkinsfile.

node {

  stage ('Stage CheckOut 1'){ 
   
//Repo A ( has Polling )    
 checkout([$class: 'GitSCM', branches: [[name: '*/feature**']], 
 doGenerateSubmoduleConfigurations: false, extensions: [[$class:
 'RelativeTargetDirectory', relativeTargetDir: 'android'], [$class:'LocalBranch', localBranch: '**']],
 submoduleCfg: [], userRemoteConfigs:
 [[credentialsId: '', url:

//Checking if similar branch name as repo A exists in repo B 
 def b  = resolveScm source: [$class: 'GitSCMSource', credentialsId: '',
 id: '_', remote: 'https://github.com/.....git', traits: [[$class: 'jenkins.plugins.git.traits.BranchDiscoveryTrait']]],
 targets: [env. GIT_LOCAL_BRANCH, 'develop']
 checkout b
 
  }   
  }  

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-use...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/b6d82bc9-9f55-443c-b188-8d73f973a1f9%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages