Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jenkins pipeline - git branch

I am writing pipeline script in order to implement continuous integration flow in our project. The process is for users to do work in a specific branch that follows naming convention, test_id1, test_id2 etc. Once these are pushed to remote repo, Jenkins kicks-in, it checks out branch with test* and does rest of the build process.

Based on my initial instinct, I wrote the pipeline script as follows: stage 'build'

node {
    git url: 'git@hd1:testing', branch: test*
    sh "pwd"
    sh "cat simple.csh"
    sh "echo $PATH"
    sh "csh simple.csh"
    echo("end of pipeline")
}

Btw, I have tried "test*", 'test*'.

Jenkins bails out with the following error:

Caused by: hudson.plugins.git.GitException: Command "git checkout -b test* 264dc398372cba41c026568bd764d2656ebfc511" returned status code 128:

So, the question is whether I am going in the right direction with this. I also looked at the error above and obviously, git checkout with a wildcard is something that would not serve the purpose. So, would the following approach work:

1) Checkout git master as usual 2) Check if any new branch by the name test* exists 3) If it does, checkout the branch and then do rest of the build

Need some directions on this ...

like image 271
Sharanbr Avatar asked Jan 26 '17 11:01

Sharanbr


People also ask

How to get git branch name in Jenkins?

To get git branch name in Jenkins, If it is a multibranch pipeline then we can easily use the env.GIT_BRANCH . But if it is a normal pipeline then we can use the SCM plugin object to retrieve the branch name. The below code works for both normal as well as multibranch pipelines.

What is Jenkins feature branch pipeline in Jenkins?

Jenkins receives the PR and finds the relevant multibranch pipeline, and creates a feature branch pipeline automatically. It then runs the jobs with the steps mentioned in the Jenkinsfile from the feature branch. During checkout, the source and target branches in the PR gets merged.

How does Jenkins integrate with GitHub?

1 When a developer creates a PR from a feature branch to develop a branch, Github sends a webhook with the PR information to Jenkins. 2 Jenkins receives the PR and finds the relevant multibranch pipeline, and creates a feature branch pipeline automatically. ... 3 Once the build finishes, Jenkins will update the status to Github PR. ...

How do I use Bitbucket and GitLab in Jenkins?

You can also use Bitbucket or Gitlab as SCM source for a multi-branch pipeline Step 1: From the Jenkins home page create a “new item”. Step 2: Select the “Multibranch pipeline” from the option and click ok. Step 3: Click “Add a Source” and select Github.


1 Answers

You can use SCM checkout plugin with regular expression. I've used to poll the feature branches with the format feature/US1234. Modify like you need.

checkout([$class: 'GitSCM', 
branches: [[name: ':^(?i)origin/feature/[a-z|A-Z]{2}[0-9].*']], 
doGenerateSubmoduleConfigurations: false, 
extensions: [[$class: 'WipeWorkspace'], 
[$class: 'LocalBranch', localBranch: '**']], submoduleCfg: [], 
userRemoteConfigs: [[credentialsId: 'YOURID', url: 'GITURL']]]) 

Use pipeline syntax generator for more option.

like image 63
Samy Avatar answered Oct 17 '22 21:10

Samy