Jenkins 管道 git 命令子模块更新
Jenkins pipeline git command submodule update
我想在 git 克隆上更新子模块。
有没有办法使用 Jenkins 管道 Git 命令来做到这一点?
目前我正在做这个...
git branch: 'master',
credentialsId: 'bitbucket',
url: 'ssh://bitbucket.org/hello.git'
然而,它不会在克隆后更新子模块
使用当前的 Git plugin,您甚至不需要它。
The Git plugin supports repositories with submodules which in turn have submodules themselves.
This must be turned on though:
in Job Configuration -> Section Source Code Management, Git -> Advanced Button (under Branches to build) -> Recursively update submodules
但是 OP 正在使用管道。
所以一个简单的第一个构建步骤就足够了:
git submodule update --init --recursive
但是,OP 补充说:
Yes but if I'm using sh 'git submodule update --init --recursive'
, this will use $HOME/id_rsa
right? I want to pass in my private key for this command if possible.
有可能:在Pipeline syntax, you can define environment variables.
这意味着您可以设置 GIT_SSH_COMMAND
(with Git 2.10+).
这使您可以 reference your own private key.
pipeline {
agent any
environment {
GIT_SSH_COMMAND = 'ssh -i /path/to/my/private/key'
}
stages {
stage('Build') {
steps {
sh 'printenv'
sh 'git submodule update --init --recursive'
}
}
}
}
如果任何克隆涉及 ssh url,则该 ssh 克隆将使用正确的私钥。
注意 sancelot points out :
unfortunately this does not work: JENKINS-38860
The error reported above:
FATAL: Command "git config --get submodule.MySubModule.url"
returned status code 1
Occurs for me whenever you have nested submodules.
Consider a scenario in which repo A
contains submodule B
, which contains submodule C
.
If "Advanced submodule behaviour
" with "Recursively update submodules
" is not enabled, Jenkins will clone A
, checkout/clone B
, and fail to initialise/clone C
.
This is probably expected behaviour.
If you enable "Recursively update submodules
", you get the error:
FATAL: Command "git config --get submodule.C.url"
returned status code 1
git command as a pipeline step is rather limited as it provides a default implementation of the more complex checkout command. For more advanced configuration, you should use checkout command,您可以为其传递大量参数,包括所需的子模块配置。
你要用的大概是这样的:
checkout([$class: 'GitSCM',
branches: [[name: '*/master']],
doGenerateSubmoduleConfigurations: false,
extensions: [[$class: 'SubmoduleOption',
disableSubmodules: false,
parentCredentials: false,
recursiveSubmodules: true,
reference: '',
trackingSubmodules: false]],
submoduleCfg: [],
userRemoteConfigs: [[url: 'your-git-server/your-git-repository']]])
根据文档,写这些行通常很麻烦,我建议您改用 Jenkins 非常好 Snippet Generator
(YourJenkins > yourProject > PipelineSyntax) 自动生成结帐行!
checkout([
$class: 'GitSCM',
branches: scm.branches,
doGenerateSubmoduleConfigurations: false,
extensions: [[
$class: 'SubmoduleOption',
disableSubmodules: false,
parentCredentials: true,
recursiveSubmodules: true,
reference: '',
trackingSubmodules: false
]],
submoduleCfg: [],
userRemoteConfigs: scm.userRemoteConfigs
])
如果您知道 checkout scm
使用的凭据名称,您还可以将其显式传递给 git 命令以更新子模块:
stage ('Clone') {
steps {
checkout scm
withCredentials([sshUserPrivateKey(credentialsId: 'bitbucket_ssh', keyFileVariable: 'SSH_KEY')]) {
sh 'GIT_SSH_COMMAND="ssh -i $SSH_KEY" git submodule update --init'
}
}
}
这是对之前答案的微小改动,但我相信它是最佳解决方案。
我将 branches 参数交换为使用 env.GIT_COMMIT
以便它检查通过作业触发的特定提交。否则它将构建特定分支的最新提交,在其他示例中,当使用 scm.branches
时,它将构建分支的提示而不是预期的提交。
checkout([
$class: 'GitSCM',
branches: [[name: "${env.GIT_COMMIT}"]],
doGenerateSubmoduleConfigurations: false,
extensions: [[
$class: 'SubmoduleOption',
disableSubmodules: false,
parentCredentials: true,
recursiveSubmodules: true,
reference: '',
trackingSubmodules: false
]],
submoduleCfg: [],
userRemoteConfigs: scm.userRemoteConfigs
])
我想在 git 克隆上更新子模块。
有没有办法使用 Jenkins 管道 Git 命令来做到这一点?
目前我正在做这个...
git branch: 'master',
credentialsId: 'bitbucket',
url: 'ssh://bitbucket.org/hello.git'
然而,它不会在克隆后更新子模块
使用当前的 Git plugin,您甚至不需要它。
The Git plugin supports repositories with submodules which in turn have submodules themselves.
This must be turned on though:in Job Configuration -> Section Source Code Management, Git -> Advanced Button (under Branches to build) -> Recursively update submodules
但是 OP 正在使用管道。
所以一个简单的第一个构建步骤就足够了:
git submodule update --init --recursive
但是,OP 补充说:
Yes but if I'm using
sh 'git submodule update --init --recursive'
, this will use$HOME/id_rsa
right? I want to pass in my private key for this command if possible.
有可能:在Pipeline syntax, you can define environment variables.
这意味着您可以设置 GIT_SSH_COMMAND
(with Git 2.10+).
这使您可以 reference your own private key.
pipeline {
agent any
environment {
GIT_SSH_COMMAND = 'ssh -i /path/to/my/private/key'
}
stages {
stage('Build') {
steps {
sh 'printenv'
sh 'git submodule update --init --recursive'
}
}
}
}
如果任何克隆涉及 ssh url,则该 ssh 克隆将使用正确的私钥。
注意 sancelot points out
unfortunately this does not work:
JENKINS-38860
The error reported above:
FATAL: Command "git config --get submodule.MySubModule.url" returned status code 1
Occurs for me whenever you have nested submodules.
Consider a scenario in which repo
A
contains submoduleB
, which contains submoduleC
.If "
Advanced submodule behaviour
" with "Recursively update submodules
" is not enabled, Jenkins will cloneA
, checkout/cloneB
, and fail to initialise/cloneC
.
This is probably expected behaviour.If you enable "
Recursively update submodules
", you get the error:FATAL: Command "git config --get submodule.C.url" returned status code 1
git command as a pipeline step is rather limited as it provides a default implementation of the more complex checkout command. For more advanced configuration, you should use checkout command,您可以为其传递大量参数,包括所需的子模块配置。
你要用的大概是这样的:
checkout([$class: 'GitSCM',
branches: [[name: '*/master']],
doGenerateSubmoduleConfigurations: false,
extensions: [[$class: 'SubmoduleOption',
disableSubmodules: false,
parentCredentials: false,
recursiveSubmodules: true,
reference: '',
trackingSubmodules: false]],
submoduleCfg: [],
userRemoteConfigs: [[url: 'your-git-server/your-git-repository']]])
根据文档,写这些行通常很麻烦,我建议您改用 Jenkins 非常好 Snippet Generator
(YourJenkins > yourProject > PipelineSyntax) 自动生成结帐行!
checkout([
$class: 'GitSCM',
branches: scm.branches,
doGenerateSubmoduleConfigurations: false,
extensions: [[
$class: 'SubmoduleOption',
disableSubmodules: false,
parentCredentials: true,
recursiveSubmodules: true,
reference: '',
trackingSubmodules: false
]],
submoduleCfg: [],
userRemoteConfigs: scm.userRemoteConfigs
])
如果您知道 checkout scm
使用的凭据名称,您还可以将其显式传递给 git 命令以更新子模块:
stage ('Clone') {
steps {
checkout scm
withCredentials([sshUserPrivateKey(credentialsId: 'bitbucket_ssh', keyFileVariable: 'SSH_KEY')]) {
sh 'GIT_SSH_COMMAND="ssh -i $SSH_KEY" git submodule update --init'
}
}
}
这是对之前答案的微小改动,但我相信它是最佳解决方案。
我将 branches 参数交换为使用 env.GIT_COMMIT
以便它检查通过作业触发的特定提交。否则它将构建特定分支的最新提交,在其他示例中,当使用 scm.branches
时,它将构建分支的提示而不是预期的提交。
checkout([
$class: 'GitSCM',
branches: [[name: "${env.GIT_COMMIT}"]],
doGenerateSubmoduleConfigurations: false,
extensions: [[
$class: 'SubmoduleOption',
disableSubmodules: false,
parentCredentials: true,
recursiveSubmodules: true,
reference: '',
trackingSubmodules: false
]],
submoduleCfg: [],
userRemoteConfigs: scm.userRemoteConfigs
])