Jenkins pipeline: checkout explicit git commit

前端 未结 3 1866
盖世英雄少女心
盖世英雄少女心 2021-02-03 23:39

I want to be able to say something like:

git branch: commitHash, credentialsId: credentialsId, url: url

The usecase: I\'m doing parallel build

相关标签:
3条回答
  • 2021-02-04 00:08

    Yuri G's example didn't work for me when jenkins lacked a workspace due to initial checkout. The following works in this case. I don't understand why they are all that different.

        def commitId = "<insert sha here>"
    
        checkout ( [$class: 'GitSCM',
            branches: [[name: commitId ]],
            userRemoteConfigs: [[
                credentialsId: 'deploy key for your repo', 
                url: 'repo url']]])
    
    0 讨论(0)
  • 2021-02-04 00:20

    To checkout a specific hash from your repository, in Jenkins pipeline, you can do something like this:

    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'"
            }  
          }
        }
      }
    }
    
    0 讨论(0)
  • 2021-02-04 00:24

    Use a general scm step

    checkout([$class: 'GitSCM', branches: [[name: commitHash ]],
         userRemoteConfigs: [[url: 'http://git-server/user/repository.git']]])
    
    0 讨论(0)
提交回复
热议问题