gitlab-plugin icon indicating copy to clipboard operation
gitlab-plugin copied to clipboard

Use share library can't update gitlab commit status with 'gitlabCommitStatus'

Open colinlabs opened this issue 5 years ago • 7 comments

Issue

Use share library can't update gitlab commitstatus with 'gitlabCommitStatus' when Share library set Load implicitly true

Context

  • Gitlab plugin version: 1.5.11
  • Gitlab version: 11.6.3
  • Jenkins version: 2.166
  • Job type: Pipeline

Logs & Traces

INFO: Build does not contain SCM revision action.
Feb 27, 2019 4:01:46 AM com.dabsquared.gitlabjenkins.util.CommitStatusUpdater retrieveGitlabProjectIds
INFO: Retrieving gitlab project ids

 

colinlabs avatar Feb 27 '19 04:02 colinlabs

I can confirm that use multiple branch git repo in pipeline, gitlabCommitStatus can not work , example code:

this will work

node() {
    git url: 'ssh://[email protected]:10022/root/spring-demo.git'
    git url: 'ssh://[email protected]:10022/root/spring-demo.git'
    gitlabBuilds(builds: ["build", "test"]) {
        stage("build") {
          gitlabCommitStatus("build") {
              // your build steps
          }
        }

        stage("test") {
          gitlabCommitStatus("test") {
              // your test steps
          }
        }
    }
}

this will not work

node() {
    git url: 'ssh://[email protected]:10022/root/spring-demo.git'
    git url: 'ssh://[email protected]:10022/root/jenkins-cicd.git'


    gitlabBuilds(builds: ["build", "test"]) {
        stage("build") {
          gitlabCommitStatus("build") {
              // your build steps
          }
        }

        stage("test") {
          gitlabCommitStatus("test") {
              // your test steps
          }
        }
    }
}

colinlabs avatar Feb 27 '19 07:02 colinlabs

We have this problem as well (also version 1.5.11 of the plugin). Is there a solution for that?

(It seems like in the past the same was broken and then was fixed: #523 )

Xennis avatar Mar 14 '19 13:03 Xennis

I have the same issue: note: I do use checkout scm as documentation says.

Result:

  • Send-status-1 not being send
  • test-me-again-${BUILD_NUMBER} is working.
  • gitlabBuilds(builds: ['deploy-status']) does nothing
@Library(['god-bless@NDA', 'with-love@sec-team']) _
  pipeline {
    agent none
    options {
      gitLabConnection('gitlab.exmaple.com')
      gitlabBuilds(builds: ['deploy-status'])
    }

    stages {
      stage('Send status'){
        agent { label 'jnlp' }
        steps {
          checkout scm
          gitlabCommitStatus(name: "Send-status-1") {
            sh "echo I should sent status to gitlab"
            sh "env"
          }
          gitlabCommitStatus(name: "test-me-again-${BUILD_NUMBER}",  builds: [[projectId: 'group/project', revisionHash: 'master']])
          {
            sh "echo I should sent status to gitlab"
          }
        }
      }
    }
  }

as workaround you could set projectId explicitly, but it's not very useful in shared library:

gitlabCommitStatus(name: "stage-${BUILD_NUMBER}",
    builds: [[projectId: 'gitlabgroup/project', revisionHash: 'master']]
          )
          {
            sh "echo I should sent status to gitlab"
          }

without @Library annotation it works in both cases.

I tried to trace down where it happens:

  1. I don't see a message in logs https://github.com/jenkinsci/gitlab-plugin/blob/master/src/main/java/com/dabsquared/gitlabjenkins/util/CommitStatusUpdater.java#L168 so buildDatas isn't empty
  2. https://github.com/jenkinsci/gitlab-plugin/blob/master/src/main/java/com/dabsquared/gitlabjenkins/util/CommitStatusUpdater.java#L172 buildDatas.size() is not equal to '1', so it's not empty and size isn't 1 (>1?)
  3. https://github.com/jenkinsci/gitlab-plugin/blob/master/src/main/java/com/dabsquared/gitlabjenkins/util/CommitStatusUpdater.java#L177 scmRevisionAction is null. I don't know why it's empty. Later it's trying to get projectId but I don't see anything in logs.

ipleten avatar Feb 19 '20 23:02 ipleten

Thanks for the workaround @ipleten I found that gitlabCommitStatus does not update statuses even with workaround above when the source branch name contains dots, the error in pipeline looks like this:

Failed to update Gitlab commit status for project 'my/awesome/project': HTTP 404 Not Found

And the error in Jenkins logs are the following:

Jan 24, 2022 10:21:18 AM FINEST com.dabsquared.gitlabjenkins.gitlab.api.impl.ResteasyGitLabClientBuilder
Call GitLab:
HTTP method: POST
URL: https://gitlab.example.com/api/v4/projects/my%2Fawesome%2Fproject/statuses/new_dummy_version_1.0.0
Request headers: [
<removed>
]
Jan 24, 2022 10:21:18 AM FINEST com.dabsquared.gitlabjenkins.gitlab.api.impl.ResteasyGitLabClientBuilder
Got response from GitLab:
URL: https://gitlab.example.com/api/v4/projects/my%2Fawesome%2Fproject/statuses/new_dummy_version_1.0.0
Status: 404 Not Found
Response headers: [
<removed>
]
Response body: {
  "error" : "404 Not Found"
}

But using a commit sha instead of branch names allows statuses to be sent:

builds: [[projectId: 'gitlabgroup/project', revisionHash: "${env.gitlabMergeRequestLastCommit}"]]

kdsnice avatar Jan 24 '22 11:01 kdsnice

I can confirm that use multiple branch git repo in pipeline, gitlabCommitStatus can not work , example code:

this will work

node() {
    git url: 'ssh://[email protected]:10022/root/spring-demo.git'
    git url: 'ssh://[email protected]:10022/root/spring-demo.git'
    gitlabBuilds(builds: ["build", "test"]) {
        stage("build") {
          gitlabCommitStatus("build") {
              // your build steps
          }
        }

        stage("test") {
          gitlabCommitStatus("test") {
              // your test steps
          }
        }
    }
}

this will not work

node() {
    git url: 'ssh://[email protected]:10022/root/spring-demo.git'
    git url: 'ssh://[email protected]:10022/root/jenkins-cicd.git'


    gitlabBuilds(builds: ["build", "test"]) {
        stage("build") {
          gitlabCommitStatus("build") {
              // your build steps
          }
        }

        stage("test") {
          gitlabCommitStatus("test") {
              // your test steps
          }
        }
    }
}

这个问题貌似有点儿历史了 #837 Jenkins version: 2.332.1 gitlab plugin version: 1.5.28 我通过sshagent插件包裹 git clone 命令来解决 git 认证问题,这里是我的笔记:https://blog.csdn.net/qq_33248407/article/details/123430187 image

ShoJinto avatar Mar 10 '22 15:03 ShoJinto

hi, I'm also facing this issue with:

  • gitlab-plugin 1.7.14
  • jenkins 2.387.3
  • the jenkinsfile triggering the build is in a library (i.e. not in the repository where the commit status should be updated)

it's ok when passing 'builds' option in gitlabCommitStatus as a workaround to target specifically the project (thanks @ipleten) but there is no such option for gitlabbuilds method : it is not possible then to declare statuses as 'expected' in gitlab before the whole pipeline gets finished.

ex with this code, stage 'test3' is never declared as 'pending' in gitlab project 'gitgroup/targetproject' whereas it should be:

def builds = ['test1', 'test2', 'test3']

node() {
  gitlabBuilds(builds: builds) {
    gitlabCommitStatus(name: 'test1', builds: [
            [projectId: 'gitgroup/targetproject', revisionHash: 'master']
    ]) {
      echo 'this is test1 stage'
    }

    gitlabCommitStatus(name: 'test2', builds: [
            [projectId: 'gitgroup/targetproject', revisionHash: 'master']
    ]) {
      echo 'this is test2 stage'
    }
  }
}

No status is declared 'pending' in gitlab whereas gitlabbuilds mentions all expected steps. Does anyone have a similar workaround to target a specific project when declaring 'pending' steps with gitlabbuilds method ?

aubertaa avatar Aug 31 '23 07:08 aubertaa