Een Jenkins-pijplijn gebruiken om meerdere Git-repo’s in dezelfde taak uit te checken

Ik gebruik de Jenkins Multiple SCM-plug-in om drie git-repositories uit te checken in drie submappen in mijn Jenkins-taak. Vervolgens voer ik een set opdrachten uit om een ​​enkele set artefacten te bouwen met informatie en code uit alle drie de opslagplaatsen.

Multiple SCM wordt nu afgeschreven en in de tekst wordt aanbevolen om over te stappen op pijplijnen. Ik heb het geprobeerd, maar ik weet niet hoe ik het moet laten werken.

Hier is de directorystructuur die ik graag wil zien vanaf het hoogste niveau van mijn Jenkins-jobdirectory:

$ ls
Combination
CombinationBuilder
CombinationResults

Elk van die drie submappen heeft een enkele git repo uitgecheckt. Met de Multiple SCM heb ik git gebruikt en vervolgens het gedrag “afrekenen aan een submap” toegevoegd. Hier was mijn poging met een pijplijnscript:

node('ATLAS && Linux') {
    sh('[ -e CalibrationResults ] || mkdir CalibrationResults')
    sh('cd CalibrationResults')
    git url: 'https://github.com/AtlasBID/CalibrationResults.git'
    sh('cd ..')
    sh('[ -e Combination ] || mkdir Combination')
    sh('cd Combination')
    git url: 'https://github.com/AtlasBID/Combination.git'
    sh('cd ..')
    sh('[ -e CombinationBuilder ] || mkdir CombinationBuilder')
    sh('cd CombinationBuilder')
    git url: 'https://github.com/AtlasBID/CombinationBuilder.git'
    sh 'cd ..'
    sh('ls')
    sh('. CombinationBuilder/build.sh')
}

Het git-commando lijkt echter te worden uitgevoerd in de directory op het hoogste niveau van de werkruimte (wat logisch is), en volgens de syntaxis lijkt er ook niet het gedrag van uitchecken naar subdirectory te zijn.


Antwoord 1, autoriteit 100%

U kunt de opdracht dirgebruiken om een ​​pijplijnstap in een submap uit te voeren:

node('ATLAS && Linux') {
    dir('CalibrationResults') {
        git url: 'https://github.com/AtlasBID/CalibrationResults.git'
    }
    dir('Combination') {
        git url: 'https://github.com/AtlasBID/Combination.git'
    }
    dir('CombinationBuilder') {
        git url: 'https://github.com/AtlasBID/CombinationBuilder.git'
    }
    sh('ls')
    sh('. CombinationBuilder/build.sh')
}

Antwoord 2, autoriteit 28%

Je kunt die drie git-opslagplaatsen uitchecken in drie submappen door de checkout SCM-stap drie keer als volgt te gebruiken:

stage('Checkout') {
 // Get CalibrationResults from GitHub
 checkout([  
            $class: 'GitSCM', 
            branches: [[name: 'refs/heads/master']], 
            doGenerateSubmoduleConfigurations: false, 
            extensions: [[$class: 'RelativeTargetDirectory', relativeTargetDir: 'CalibrationResults']], 
            submoduleCfg: [], 
            userRemoteConfigs: [[credentialsId: '6463627-ab54-4e42-bc29-123458', url: 'https://github.com/AtlasBID/CalibrationResults.git']]
        ])
 // Get Combination from GitHub
 checkout([  
            $class: 'GitSCM', 
            branches: [[name: 'refs/heads/master']], 
            doGenerateSubmoduleConfigurations: false, 
            extensions: [[$class: 'RelativeTargetDirectory', relativeTargetDir: 'Combination']], 
            submoduleCfg: [], 
            userRemoteConfigs: [[credentialsId: '6463627-ab54-4e42-bc29-123458', url: 'https://github.com/AtlasBID/Combination.git']]
        ])
 // Get CombinationBuilder from GitHub
 checkout([  
            $class: 'GitSCM', 
            branches: [[name: 'refs/heads/master']], 
            doGenerateSubmoduleConfigurations: false, 
            extensions: [[$class: 'RelativeTargetDirectory', relativeTargetDir: 'CombinationBuilder']], 
            submoduleCfg: [], 
            userRemoteConfigs: [[credentialsId: '6463627-ab54-4e42-bc29-123458', url: 'https://github.com/AtlasBID/CombinationBuilder.git']]
        ])
}

Antwoord 3, autoriteit 12%

Hier is de mijne

   stage('CheckoutModule1') {
        steps {
            sh 'mkdir -p Module1'
            dir("Module1")
            {
                git branch: "develop",
                credentialsId: 'aaa',
                url: '[email protected]:b/module1.git'
            }
        }
    }
    stage('CheckoutModule2') {
        steps {
            sh 'mkdir -p Module2'
            dir("Module2")
            {
                git branch: "develop",
                credentialsId: 'aaa',
                url: '[email protected]:b/module2.git'
            }
        }
    }

Antwoord 4, autoriteit 3%

Als je repository submodules heeft, gebruik dan git checkout

pipeline {
agent {label 'master'}
stages{
    stage("Demo"){
        steps{
            echo 'Hello World'
        }
    }
    stage("Source"){
        parallel{
            stage('CalibrationResults'){
                steps{
                    echo 'Checking out CalibrationResults'
                    checkout([$class: 'GitSCM', branches: [[name: '*/CI-CD-Demo']], doGenerateSubmoduleConfigurations: false, extensions: [[$class: 'CloneOption', depth: 0, noTags: true, reference: '', shallow: false, timeout: 60], [$class: 'SubmoduleOption', disableSubmodules: false, parentCredentials: false, recursiveSubmodules: true, reference: '', timeout: 60, trackingSubmodules: true], [$class: 'RelativeTargetDirectory', relativeTargetDir: 'server-core'],[$class: 'CheckoutOption', timeout: 60]], submoduleCfg: [], userRemoteConfigs: [[url: 'https://github.com/AtlasBID/CalibrationResults.git']]])
                }
            }
            stage('Combination'){
                steps{
                    echo 'Checking out server spoke'
                    checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [[$class: 'CloneOption', depth: 0, noTags: true, reference: '', shallow: false, timeout: 60], [$class: 'SubmoduleOption', disableSubmodules: false, parentCredentials: false, recursiveSubmodules: true, reference: '', timeout: 60, trackingSubmodules: true], [$class: 'RelativeTargetDirectory', relativeTargetDir: 'server-spoke'], [$class: 'CheckoutOption', timeout: 60]], submoduleCfg: [], userRemoteConfigs: [[url: 'https://github.com/AtlasBID/CombinationBuilder.git']]])
                }
            }
        }
    }
}
}

Gegenereerd met behulp van Checkout git snippet generator

Other episodes