**背景:**在实际项目构建中有时会用到调用其他代码库的情况,例如调用公共代码库和基础代码库,此时就可以用git submodule这个模块实现这个需求,git submodule可以将一个git仓库以目录的方式作为另一个git仓库的子目录。Jenkins用来做项目的持续集成,如果使用了Git Submodule,每次Jenkins更新版本库的时候还需要更新submodule的内容。

Jenkis中的使用方法

  • 版本需求 **注意:**使用此功能,需要Jenkins的部分插件达到指定版本,我的版本如下:

    • Git clent plugin == 2.6.0
    • Git Plugin == 3.6.4
    • Credentials Plugin == 2.1.16
    • SSH Credentials Plugin == 1.13
  • 安装以上版本或更高版本插件 安装完成后需要重启Jenkins,此时就可以使用了

  • 在Jenkins上设置 20171129-jenkins-1 其中User credentials from default remote of parent repository意思是Git Submodule的repository会使用和主repository一样的验证,就是主repository使用的身份,低版本不会有这个勾选项,还会报错。

升级插件的方法

  • 1.使用Jenins默认方法 进入Jenkins管理页面即 Jenkins–>插件管理–>高级 如下: 20171129-jenkins-2 Jenkins默认更新的URL为http://updates.jenkins-ci.org/experimental/update-center.json ,但是由于网络问题推荐使用http://mirror.xmission.com/jenkins/updates/experimental/update-center.json,另外有条件也可以使用这个页面上面的网络代理选项

  • 2.直接下载插件通过web页面进行上传 依然在Jenkins的管理页面高级中,通过自己下载,然后上传即可,部分插件安装后需要重启Jenkins生效

    1
    2
    3
    4
    
    https://wiki.jenkins.io/display/JENKINS/Git+Plugin
    #git 3.6.4
    https://wiki.jenkins.io/display/JENKINS/Git+Client+Plugin
    #git-client 2.6.0
    

关于报错

  • 1.Permission报错
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    
    FATAL: Command "git submodule update --init --recursive" returned status code 128:
    stdout:
    stderr: Cloning into 'plxxm/pxxx'...
    Permission denied (publickey).
    fatal: Could not read from remote repository.
    Please make sure you have the correct access rights
    and the repository exists.
    fatal: clone of 'git@xxx.net:platform/planx.git' into submodule path 'plxxm/pxxx' failed
    hudson.plugins.git.GitException: Command "git submodule update --init --recursive" returned status code 128:
    stdout:
    stderr: Cloning into 'plxxm/pxxx'...
    Permission denied (publickey).
    fatal: Could not read from remote repository.
    Please make sure you have the correct access rights
    and the repository exists.
    

    **原因:**git submodule使用的是ssh方式管理repository,没有找到可以使用的ssh key做submodule的身份认证。按上面的方法升级Git插件版本后,就可以通过和主版本库一样的Credentials进行代码获取了