Merge branch B to Branch A
Merge branch B to Branch A
I m running a jenkins pipeline for my project.
Jenkins pulls the repository from gitlab and then runs my pipeline.
So the problem is that i want to merge B to A on gitlab when the pipeline runs successfully.
This is my Jenkinsfile which of course doesn't satisfy my need because it gives me the error
merge: test - not something we can merge
node
try
stage('Build')
def mavenHome = tool 'Maven'
checkout scm
sh "mvn clean install"
stage('SonarQube')
steps
sh "mvn clean verify sonar:sonar -Dsonar.branch.name="+ env.BRANCH_NAME
stage('Merge with Dev')
sh "git checkout dev"
sh "git pull gitlab dev"
sh "git merge test"
catch(error)
currentBuild.result = "FAILED"
mail to: 'mail here',
subject: "Failed Pipeline: $currentBuild.fullDisplayName",
body: "Something is wrong with $env.BUILD_URL"
I want to merge to be automatic, no merge request. Thank you
how to do that exactly ? i mean i m on my jenkins machine now but i have no physical git repository of my project on it. i m using gitlab which is also on the same machine tho.
– Ziyed Becha
Aug 31 at 21:02
The repository is cloned onto the machine. You need to find where it is cloning. You can use
sh 'pwd' in your script to find out an exact path where Jenkins has cloned your repository. On Linux usually Jenkins clone projects in /var/lib/jenkins/workspace directory– Talha Junaid
Aug 31 at 21:07
sh 'pwd'
/var/lib/jenkins/workspace
The path is
/var/lib/jenkins/workspace/projettest_issue5-ZP3ZOFFJDVDMG2NKVVH5JTLVMAZGUC2OAY2C7E255STSKQUIUKVA but this is not helping. i mean it's not a git repository and i can't access the git repositry of gitlab. What would be ideal if gitlab create a not bare repository on my server that i can access via jenkins and make my merges.– Ziyed Becha
Aug 31 at 21:18
/var/lib/jenkins/workspace/projettest_issue5-ZP3ZOFFJDVDMG2NKVVH5JTLVMAZGUC2OAY2C7E255STSKQUIUKVA
1 Answer
1
I found a way to create a merge request when the pipeline is successful and that's by using API.Well, it doesn't do exactly do what i want (directly merge the branch when possible) but it's good enough for now (automatically create a merge request in gitlab).
This is the final stage in jenkinsfile
stage('Merge with Dev')
sh 'curl --request POST --header "PRIVATE-TOKEN: APITOKENHERE" "GITURL/api/v4/projects/PROJECTID/merge_requests?source_branch='+env.BRANCH_NAME+'&target_branch=A&title='+env.BRANCH_NAME+'ToA"'
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Try to do it from the command line on Jenkins machine, It looks like something is wrong with git not with Jenkins.
– Talha Junaid
Aug 31 at 20:32