Jenkins管道:签出显式git提交

Jenkins管道:签出显式git提交,jenkins,jenkins-pipeline,Jenkins,Jenkins Pipeline,我想说一些类似的话: git branch: commitHash, credentialsId: credentialsId, url: url [Linux64 Build] > git rev-parse origin/e4b6c976a0a986c348a211579f1e8fd32cf29567^{commit} # timeout=10 [Pipeline] [Linux64 Build] } [Pipeline] [Linux64 Build] // dir [Pipel

我想说一些类似的话:

git branch: commitHash, credentialsId: credentialsId, url: url
[Linux64 Build]  > git rev-parse origin/e4b6c976a0a986c348a211579f1e8fd32cf29567^{commit} # timeout=10
[Pipeline] [Linux64 Build] }
[Pipeline] [Linux64 Build] // dir
[Pipeline] [Linux64 Build] }
[Pipeline] [Linux64 Build] // node
[Pipeline] [Linux64 Build] }
[Linux64 Build] Failed in branch Linux64 Build
用例:我在不同的平台上进行并行构建和测试运行,并希望确保每个平台获得相同的代码。它是C++,我们在单独的平台上构建,并在它们上面建立。 如果我执行上述操作,它将失败-底层代码假定给定的分支实际上是一个分支,或者您会得到如下结果:

git branch: commitHash, credentialsId: credentialsId, url: url
[Linux64 Build]  > git rev-parse origin/e4b6c976a0a986c348a211579f1e8fd32cf29567^{commit} # timeout=10
[Pipeline] [Linux64 Build] }
[Pipeline] [Linux64 Build] // dir
[Pipeline] [Linux64 Build] }
[Pipeline] [Linux64 Build] // node
[Pipeline] [Linux64 Build] }
[Linux64 Build] Failed in branch Linux64 Build
我以前看到过关于这个问题的各种说法,虽然没有实际的答案——只是建议将源代码隐藏起来,等等。这并不是我真正想要的

文档建议应该可以给出显式的提交散列,也可以使用分支,但我无法计算语法,也找不到任何示例。当我这样做的时候,我得到了主分支,我想-在我们的设置中,主分支不工作

到目前为止,我找到的唯一解决方案是签出分支,然后显式调用git以获得提交:

                git branch: branch, credentialsId: credentialsId, url: url
                sh 'git checkout ' + commitHash
(其中branch是我最初在作业顶部获得哈希值的分支。它可以工作,但不是最整洁的

有人有更好的方法吗?

用一个步骤


当jenkins由于初始签出而缺少一个工作区时,Yuri G的例子对我不起作用。下面的例子在这种情况下起作用。我不明白为什么它们如此不同

    def commitId = "<insert sha here>"

    checkout ( [$class: 'GitSCM',
        branches: [[name: commitId ]],
        userRemoteConfigs: [[
            credentialsId: 'deploy key for your repo', 
            url: 'repo url']]])
def commitId=“”
签出([$class:'GitSCM',
分支机构:[[名称:commitId]],
userRemoteConfigs:[[
credentialsId:“为您的回购部署密钥”,
url:“回购url']])

要在Jenkins管道中从存储库中签出特定哈希,可以执行以下操作:

pipeline {
  agent none
  options {
      timeout(time: 1, unit: 'HOURS')
  }
  environment {
    // Jenkins credentials for git auth
    SOURCECODE_JENKINS_CREDENTIAL_ID = 'sourcecode-credential'
    // URL of the repo
    SOURCE_CODE_URL = 'https://sourcecode.com/repo.git'
    // branch
    RELEASE_BRANCH = 'dev'
    // hash
    GIT_HASH = 'your_hash'
  }
  stages {
    stage('Git') {
      agent any
      steps {
        sleep(5)
        
       // Clean dir
        deleteDir()
        
        // Checkout branch
        git branch: "$RELEASE_BRANCH", credentialsId: "$SOURCECODE_JENKINS_CREDENTIAL_ID" , url: "$SOURCE_CODE_URL"
        
    // Checkout hash
        withCredentials([usernamePassword(credentialsId: "$SOURCECODE_JENKINS_CREDENTIAL_ID", usernameVariable: 'GIT_USER', passwordVariable: 'GIT_PASS')]) {
          sh "git checkout '$GIT_HASH'"
        }  
      }
    }
  }
}

您知道如何添加credentialsId位吗?我依赖于Jenkins用户的ssh密钥。我想我成功地解决了这个问题-我刚刚注意到有一个“编辑器”片段。它类似于:checkout([$class:'GitSCM',branchs:[[name:commitHash]],userRemoteConfigs:[[credentialsId:credentialsId,url:')好的,如果以前发生过克隆,则可以这样做。否则,如果提交ID不在refspec中,则将找不到它。因此,如果它是标记或分支的头,则一切正常。如果不是,则fails@PeterKahn这对我来说似乎很好,除了我使用
CloneOptions
时,例如
扩展:[[$class:'CloneOption',noTags:true,reference:'',shall:true]]
为了解决这个问题,我现在通过检查
branch
是否引用SHA-1散列(
branch.matches('^[a-fA-F0-9]{40}$')
)有条件地传递这个参数。我一直在使用:steps.checkout([$class:'GitSCM',branchs:[[name:hash]],userRemoteConfigs:[[url:url,CredentialId:'凭证的密钥']]这和我得到的几乎一样。我只是把行分开,使用不同的顺序。顺序应该不重要我们和Yuri之间的区别是信用证-可能就是这样。公共回购与私人回购。2017-07-12和2017-07-13在[JENKINS-31826]中的愚蠢评论()