gabrie-allaigre / sonar-gitlab-plugin

Add to each commit GitLab in a global commentary on the new anomalies added by this commit and add comment lines of modified files
GNU Lesser General Public License v3.0
713 stars 208 forks source link

fatal: repository not found when cloning #98

Closed evbo closed 6 years ago

evbo commented 6 years ago

Hi, I have followed the readme for setting up this plugin but am unable to run a job due to the below failure. What mistake did I make in configuration?

Job Failure:

Running with gitlab-runner 10.4.0 (857480b6)
  on sonarQube (d6cac072)
Using Shell executor...
Running on servername.com...
Cloning repository...
Cloning into '/home/gitlab-runner/builds/d6cac072/0/group/projectname'...
fatal: repository 'http://gitlab-ci-token:xxxxxxxxxxxxxxxxxxxx@servername.com/group/projectname.git/' not found
ERROR: Job failed: exit status 1

I created a "sonar" user in gitlab with developer role. I set the token in my sonar user that was created for that same user.

My GitLab url is set to:

http://servername.com:8088

My GitLab project id is set to (the repository):

git@servername.com:group/projectname.git

My .gitlab-ci.yml is:

sonarqube_preview:
  script:
    - git checkout origin/master
    - git merge $CI_BUILD_REF --no-commit --no-ff
    - mvn --batch-mode verify sonar:sonar -Dsonar.host.url=$SONAR_URL -Dsonar.analysis.mode=preview -Dsonar.gitlab.project_id=$CI_PROJECT_PATH -Dsonar.gitlab.commit_sha=$CI_BUILD_REF -Dsonar.gitlab.ref_name=$CI_BUILD_REF_NAME
  stage: test
  except:
    - develop
    - master
    - /^hotfix_.*$/
    - /.*-hotfix$/
  tags:
    - java

sonarqube:
  script:
    - mvn --batch-mode verify sonar:sonar -Dsonar.host.url=$SONAR_URL
  stage: test
  only:
    - master
  tags:
    - java
evbo commented 6 years ago

So this appears to be related to a gitlab bug: https://gitlab.com/gitlab-org/gitlab-ce/issues/22723

Further, it seems necessary to specify the port (8088 in my case). How do I configure the port to use in this plugin?

If I manually try to clone using the CI runners' token, I can repeat this issue:

git clone http://gitlab-ci-token:token@servername.com/group/projectname.git
Cloning into 'projectname'...
fatal: repository 'http://gitlab-ci-token:token@servername.com/group/projectname.git/' not found

git clone http://gitlab-ci-token:token@servername.com:8088/group/projectname.git
Cloning into 'projectname'...
fatal: Authentication failed for 'http://gitlab-ci-token:token@servername.com:8088/group/projectname.git/'

and then, as per that gitlab issue, if I use a personal auth token it works:

git clone http://username:authToken@servername.com:8088/group/projectname.git
Cloning into 'projectname'...
remote: Counting objects: 103, done.
remote: Compressing objects: 100% (58/58), done.
remote: Total 103 (delta 33), reused 82 (delta 29)
Receiving objects: 100% (103/103), 178.02 KiB | 0 bytes/s, done.
Resolving deltas: 100% (118/118), done.

So, how can I modify this plugin to clone with an explicit port (8088)? And how can I configure it to use a user auth token rather than CI runner token?

evbo commented 6 years ago

I'm still fairly new to gitlab, but I'm getting the feeling that this may be either a bug or at the very least an unclear feature with gitlab itself (not this sonar plugin). Following this recommendation, I essentially disabled cloning for the runner and performed the clone myself using shell commands: https://gitlab.com/gitlab-org/gitlab-runner/issues/1884

Here is what my before_script looked like to achieve this per instruction in the link above:

before_script:
  ## --------------------
  ## hard-coded variables that are not necessarily constant if gitlab config is changed:
  ## auth token is set for the gitlab-runner user in profile settings
  - AUTH_TOKEN=someAuthTokenHardCodedUnfortunately
  ## "Runner Token" is set in the Runners admin console:
  - RUNNER_TOKEN=someRunnerTokenHardCodedUnfortunately
  ## we assume just a single (0th) build step:
  - BUILD_DIR=/home/gitlab-runner/builds/$RUNNER_TOKEN/0
  ## --------------------

  ## ensure access to mvn command
  - export PATH=$PATH:/path/to/apache-maven-3.2.5/bin

  ## clean the working directory
  - cd $BUILD_DIR
  - CLONE_DIR="$BUILD_DIR/$CI_PROJECT_PATH"
  - rm -rf $CLONE_DIR
  - mkdir -p $CLONE_DIR

  ## clone the project each time (inefficient, consider performing fetch instead if it already exists)
  - git config --global user.email "my.email@some.com"
  - git config --global user.name "gitlab-runner"
  - git clone http://gitlab-runner:$AUTH_TOKEN@servername.com:8088/group/projectName.git $CLONE_DIR
  - cd $CLONE_DIR

So I'll close this issue as there's plenty of gitlab issues posted to suggest the problem I'm experiencing is not due to the plugin but the manner in which git repos are cloned before running plugins on them, e.g.:

https://gitlab.com/gitlab-org/gitlab-ce/issues/22723 https://stackoverflow.com/questions/39208420/how-do-i-enable-cloning-over-ssh-for-a-gitlab-runner https://gitlab.com/gitlab-org/gitlab-runner/issues/1884