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 ...
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.
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.
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. ...
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With