0

Hi i am using aws code build with sonar for bitbucket pull request decoration.... but i am not getting any comments on pull request....

aws codebuild logs....

[sonar4bitbucket] No open pull requests with source branch '' found. No analysis will be performed. ....

here is my aws code build command ....

sonar-scanner  -Dsonar.branch=$GIT_BRANCH -Dsonar.bitbucket.branchName=$GIT_BRANCH -Dsonar.analysis.mode=issues -Dsonar.bitbucket.oauthClientSecret=secret -Dsonar.bitbucket.oauthClientKey=key -Dsonar.bitbucket.minSeverity=INFO -Dsonar.host.url=my server-url -Dsonar.bitbucket.teamName=teamname -Dsonar.bitbucket.repoSlug=myreponame -Dsonar.bitbucket.accountName=my-account-name -Dsonar.bitbucket.approvalFeatureEnabled=false -Dsonar.bitbucket.buildStatusEnabled=true 

i think the problem is with these two properties i,e -Dsonar.branch=$GIT_BRANCH -Dsonar.bitbucket.branchName=$GIT_BRANCH but i don't know how to get the values of these from aws code build environmental variables

(https://docs.aws.amazon.com/codebuild/latest/userguide/build-env-ref-env-vars.html)

Husnain
  • 1
  • 2

1 Answers1

0

Just run this command in the buildspec to infer the branch name:

version: 0.2
phases:
  build:
    commands:
      - MY_BRANCH_NAME=$(git for-each-ref --format='%(objectname) %(refname:short)' refs/heads | awk "/^$(git rev-parse HEAD)/ {print \$2}")
      - echo $MY_BRANCH_NAME

Output is something like:

[Container] 2020/01/03 10:41:04 Running command MY_BRANCH_NAME=$(git for-each-ref --format='%(objectname) %(refname:short)' refs/heads | awk "/^$(git rev-parse HEAD)/ {print \$2}") 

[Container] 2020/01/03 10:41:04 Running command echo $MY_BRANCH_NAME 
master

Thanks to @cascabal's answer: How to find the current git branch in detached HEAD state

shariqmaws
  • 8,152
  • 1
  • 16
  • 35
  • 1
    Thanks @cascabal i read this one but very difficult to get this one i found solution here https://github.com/thii/aws-codebuild-extras – Husnain Jan 03 '20 at 11:29