I have two branches on BitBucket: master
and develop
. I\'ve also got a BitBucket Team Folder job configured on my Jenkins server to build that reposito
After some hours of trial and error, I came up with a possible solution. It builds partly on Matt's answer, but I had to alter it to make it work.
Matt was correct in the essentials: checkout scm
simply wasn't flexible enough to allow me to do what I needed, so I had to use GitSCM
to customize it. The major points of interest are:
LocalBranch
to make sure I check out to an actual branch, and not just a detached HEAD
.WipeWorkspace
to delete everything in the workspace and force a complete clone. I don't think this was a part of the solution to my question, but it was still handy to have.credentialsId
property since the repository is private.For whatever reason, when the checkout
step is executed, it only checks out the branch, but does not set it to track the remote branch. Until I find a more elegant solution, I had to do this manually.
After all that was done, I could use regular sh "git checkout master"
and even sh "git push"
, as long as I enclosed them in an sshagent
step.
I added a working example of the resulting Jenkinsfile below, but please keep in mind that it shouldn't be used for anything close to production as it's still very much in its infancy; hardcoded version numbers and no checks for which branch you're in, for example.
node {
mvnHome = tool 'Maven'
mvn = "${mvnHome}/bin/mvn"
stage('Checkout') {
checkout([
$class: 'GitSCM',
branches: scm.branches,
extensions: scm.extensions + [[$class: 'LocalBranch'], [$class: 'WipeWorkspace']],
userRemoteConfigs: [[credentialsId: 'Bitbucket', url: 'git@bitbucket.org:NAVFREG/jenkinsfile-tests.git']],
doGenerateSubmoduleConfigurations: false
])
}
stage('Release') {
// Preparing Git
sh "git branch -u origin/develop develop"
sh "git config user.email \"jenkins@thomaskasene.com\""
sh "git config user.name \"Jenkins\""
// Making and committing new verison
sh "${mvn} versions:set -DnewVersion=2.0.0 -DgenerateBackupPoms=false"
sh "git commit -am \"Released version 2.0.0\""
// Merging new version into master
sh "git checkout master"
sh "git merge develop"
sh "git checkout develop"
// Making and committing new snapshot version
sh "${mvn} versions:set -DnewVersion=3.0.0-SNAPSHOT -DgenerateBackupPoms=false"
sh "git commit -am \"Made new snapshot version 3.0.0-SNAPSHOT\""
// Pushing everything to remote repository
sshagent(['Bitbucket']) {
sh "git push"
sh "git checkout master"
sh "git push"
}
}
}