web-dev-qa-db-ja.com

2つのgitリポジトリを備えたJenkinsfile

JenkinsfileでJenkinsパイプラインプラグインを使用しています。

Vms.gitという1つのリポジトリに、Jenkinsfileとそれがビルドするアプリケーションがあります。

Deploy.gitという別のリポジトリがあり、vms.gitでアプリケーションをデプロイするために使用するスクリプトが含まれています。

現在、私のJenkinsfileはこのようになっています

node {
  stage 'build'
  checkout scm

また、ジョブ構成でvms.gitリポジトリを定義しています。

そこで、両方のリポジトリをチェックアウトし、vms.gitのJenkinsfileを使用して、残りのビルドを定義します。他のパイプラインでdeploy.gitスクリプトを再利用したいので、そこにJenkinsfileを入れたくありません。

19
Mark Chorley

checkoutを使用して複数のディレクトリをチェックアウトできますが、これをチェックアウトするディレクトリを指定する必要があります。 jenkins(スニペットジェネレーターの以下のスクリプトフィールド)を使用してスニペットを生成できます。チェックアウトを選択し、次のgitリポジトリを選択し、[追加の動作]で[サブディレクトリへのチェックアウト]を選択します。

2つのリポジトリがある場合、loadで使用するリポジトリからスクリプトをロードできます。例:

node {
    // first repository
    checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [[$class: 'RelativeTargetDirectory', relativeTargetDir: 'subdirectory1']], submoduleCfg: [], userRemoteConfigs: [[url: 'repo1.git']]])
    // second repository
    checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [[$class: 'RelativeTargetDirectory', relativeTargetDir: 'subdirectory2']], submoduleCfg: [], userRemoteConfigs: [[url: 'repo2.git']]])
    // run first script
    load 'subdirectory1/Jenkinsfile'
    // run second script
    load 'subdirectory2/Jenkinsfile'
}
32
krynio

単一のパイプライン内で複数のGitリポジトリを処理するための別のエレガントなソリューションは、 このスレッドで にあります。

node {
    dir('RepoOne') {
        git url: 'https://github.com/somewhere/RepoOne.git'
    }
    dir('RepoTwo') {
        git url: 'https://github.com/somewhere/RepoTwo.git'
    }

    sh('. RepoOne/build.sh')
    sh('. RepoTwo/build.sh')
}
25
czerwin