gunyoung
gunyoung

Reputation: 21

How to get pr number of gitea at jenkins pipeline?

I'm using gitea & jenkins & sonarqube combination now.

I need pr number of gitea for sonarqube pr comment but i don't know how to get pr number at jenkins pipeline.

I tried env variables like this.

PR_ID=env.BRANCH_NAME.replace(/^PR-/, '')

but there was no pr id...

So i checked just ${env.BRANCH_NAME} but i got just barnch name.

enter image description here

And i also tried env variable ${env.CHANGE_ID} but i got null. =(

(I'm using multibranch pipeline)

enter image description here

Is there any way to get pr number of gitea at jenkins pipeline...?

Upvotes: 1

Views: 362

Answers (1)

VonC
VonC

Reputation: 1328282

If your Jenkins job is set on the develop branch, it branch name is develop

But you can try and, in said Jenkins job, extract the branch merged into develop (a topic branch whose name should include a PR number), assuming the latest commit on develop is a merged commit.

From there, you can pass it to sonarqube.

-Dsonar.branch.name=${env.YOUR_VARIABLE}

The OP gunyoung adds in the comments:

I solve this problem by using your idea.

I'm using this variable in Jenkinsfile and there is PR number.

 GIT_COMMIT_MESSAGE = sh(script: "git --no-pager show -s --format=%B ${env.GIT_COMMIT}", returnStdout: true).trim(); 

So I make another variable using substring and I got just PR number.

PR_ID = sh(script: "git --no-pager show -s --format=%B ${env.GIT_COMMIT}", returnStdout:true).trim().substring("${GIT_COMMIT_MESSAGE}".indexOf('#')+1,"${GIT_COMMIT_MESSAGE}".indexOf(')'));

Upvotes: 1

Related Questions