MIDDLEWARE
Jenkinsfile配置submodule
最近在搞Jenkinsfile进行项目自动构建, 有一个项目配置了远程子模块依赖, 遇到了很多问题, 如下是尝试的步骤: 1, 利用checkbox
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
stages { stage('拉取代码'){ steps { checkout([ $class: 'GitSCM', branches: [[name: "${params.xxx_branch}"]], doGenerateSubmoduleConfigurations: false, extensions: [[ $class: 'SubmoduleOption', disableSubmodules: false, parentCredentials: true, recursiveSubmodules: true, reference: '', trackingSubmodules: false ]], submoduleCfg: [], userRemoteConfigs: [[credentialsId: "${git_auth}", url: "${xxx_git_address}"]] ]) } } |
报错:
1 |
Caused: java.io.IOException: Could not perform submodule update |
2. 尝试配置公钥认证
1 2 3 4 5 6 7 |
stage('构建'){ steps { container (JOB_NAME) { withCredentials([ sshUserPrivateKey(credentialsId: 'jenkins_server_pri_key', keyFileVariable: 'SSH_KEY') ]) { sh 'GIT_SSH_COMMAND="ssh -i $SSH_KEY" git submodule update --init --recursive' |
报错:
1 2 3 4 5 |
+ GIT_SSH_COMMAND=ssh -i **** git submodule update --init --recursive fatal: detected dubious ownership in repository at '/home/jenkins/agent/workspace/xxx-nginx-downloads' To add an exception for this directory, call: git config --global --add safe.directory /home/jenkins/agent/workspace/xxx-nginx-downloads |
需要增加 git config –global –add safe.directory ${WORKSPACE} 报错:
1 2 3 4 5 6 7 8 9 |
+ GIT_SSH_COMMAND='ssh -i ****' + git config --global --add safe.directory /home/jenkins/agent/workspace/plugin-xxx-cluster-nginx-downloads + git submodule update --init --recursive Submodule 'xx-plugin' (http://git.xxx.com/xxx/xxx-plugin) registered for path 'xxx-plugin' Submodule 'xxx-downloader' (http://git.xxx.com/xxx/xxx-downloader.git) registered for path 'xxx-downloader' Cloning into '/home/jenkins/agent/workspace/plugin-xxx-cluster-nginx-downloads/xxx-plugin'... fatal: could not read Username for 'http://git.xxx.com': No such device or address fatal: clone of 'http://git.xxx.com/iUAP_gPaaS/xxx-plugin' into submodule path '/home/jenkins/agent/workspace/plugin-xxx-cluster-nginx-downloads/xxx-plugin' failed Failed to clone 'xxx-plugin'. Retry scheduled |
3. 重新添加覆盖 .gitmodules
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
stage('构建'){ steps { container (JOB_NAME) { withCredentials([ usernamePassword(credentialsId: "${git_auth}", passwordVariable: 'git_password', usernameVariable: 'git_username'), ]) { sh '''#!/bin/bash set -ex git config --global --add safe.directory ${WORKSPACE} git submodule add -f http://$git_username:$git_password@git.xxx.com/xxx/xxx-plugin xxx-plugin || true git submodule add -f http://$git_username:$git_password@git.xxx.com/xxx/api-station.git api-station || true git submodule add -f http://$git_username:$git_password@git.xxx.com/xxx/xxx-downloader.git xxx-downloader || true cat .gitmodules git submodule update --init --recursive |
报错: fatal: could not read Username for ‘http://git.xxx.com‘: No such device or address 4. 修改.gitmodules 下面是原来的
1 2 3 4 5 6 7 8 9 10 |
$ cat .gitmodules [submodule "xxx-plugin"] path = xxx-plugin url = http://git.xxx.com/xxx/xxx-plugin [submodule "api-station"] path = api-station url = http://git.xxx.com/xxx/api-station.git [submodule "xxx-downloader"] path = xxx-downloader url = http://git.xxx.com/xxx/xxx-downloader.git |
通过sed进行修改
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
stage('构建'){ steps { container (JOB_NAME) { withCredentials([ usernamePassword(credentialsId: "${git_auth}", passwordVariable: 'git_password', usernameVariable: 'git_username'), usernamePassword(credentialsId: "${webmin_auth}", passwordVariable: 'webmin_password', usernameVariable: 'webmin_username') ]) { sh '''#!/bin/bash set -ex git config --global --add safe.directory ${WORKSPACE} git config --global --add safe.directory ${WORKSPACE}/xxx-plugin git config --global --add safe.directory ${WORKSPACE}/api-station git config --global --add safe.directory ${WORKSPACE}/xxx-downloader sed -i -E "s/(^\\s*.url = http:\\/\\/)(.*)$/\\1$git_username:$git_password@\\2/g" .gitmodules cat .gitmodules git submodule update --init --recursive |
最后拉取成功。 有时间再细细研究原因