Jenkins - build specific commit on specific branch

时间:2017-06-19 14:03:16

标签: git maven svn github jenkins

in a jenkinsfile I have a declarative pipeline to build two Maven projects consecutively checked out from the Github repositories

repo2 (https://github.com/MaxHoefl/repo2)

repo1 (https://github.com/MaxHoefl/repo1)

Problem:

How can I checkout a Github repo on a specific branch and even a specific commit on that branch?

Jenkinsfile

pipeline {
agent any

tools {
    maven "localMaven"
    jdk "localJDK"
    git "localGit"
}

stages {
    stage('Checkout-Repo2') {
        steps {
            git 'https://github.com/MaxHoefl/repo2.git'
            sh "echo {$env.GIT_URL}"
        }
    }

    stage('Build-Repo2') {
        steps {
            sh "mvn clean install"
            archiveArtifacts artifacts: '**/target/*.jar', fingerprint: true 
        }
    }

    stage('Checkout-Repo1') {
        steps {
            git 'https://github.com/MaxHoefl/repo1.git'
            sh "echo {$env.GIT_URL}"
        }           
    }

    stage('Build-Repo1') {
        steps {             
            sh "mvn clean install"
        }   
    }
}

post {
    success {
      echo "Build ID:    {$env.BUILD_ID}"
      echo "Jenkins URL: {$env.JENKINS_URL}"
      echo "Jobname:     {$env.JOB_NAME}"
      echo "Branch:      {$env.BRANCH_NAME}" //null
      echo "Change id:   {$env.CHANGE_ID}"
    }

    failure {

    }

    always {
        echo 'Cleaning up....'
        deleteDir()
    }
  }
}

Thanks a lot!

Edit 1

Instead of using git command

stage('Checkout-Repo2') {
        steps {
            git 'https://github.com/MaxHoefl/repo2.git'
        }           
    }

I tried using the more general checkout command

stage('Checkout-Repo2') {
        steps {
            checkout([$class: 'GitSCM', branches: [[name: ''a5e1ddd4e45aeb396b44beece370f891bd0c7fe5'']],
                      userRemoteConfigs: [[url: 'https://github.com/MaxHoefl/repo2.git']]])
        }
    }

This works. Will try to also include the branch name

0 个答案:

没有答案
相关问题