「Jenkins Plugins」- Git

  CREATED BY JENKINSBOT

问题描述

该笔记将记录:在 Jenkins 中,与 Git 插件有关的内容,以及相关问题的解决方法。

解决方案

插件信息

插件功能: 该插件用于从仓库中拉取代码到工作空间中,并进行一系列动作。
插件地址: https://plugins.jenkins.io/git
仓库链接: https://github.com/jenkinsci/git-plugin
其他链接: https://wiki.jenkins.io/display/JENKINS/Git+Plugin

(Push)推送代码到 GitHub 仓库

Jenkins Pipeline Git Push – Stack Overflow
Is it possible to Git merge / push using Jenkins pipeline – Stack Overflow

(Pull)在 Jenkins Pipeline 中,拉取代码

What is “workspace polling” in the context of Jenkins?

使用 Git 拉取

1)安装 Git plugin 插件
2)拉取代码:

git(
   url: "http://git-server/user/repository.git",
   branch: "master",
   credentialsId: "",       // 在 Jenkins 中配置的用户名和密码
   changelog: true,
   poll: false              // Fast Remote Polling / https://plugins.jenkins.io/git
)

常见问题处理

在Job中,如何检出多个仓库

Checkout multiple git repos into same Jenkins workspace

目前(10/28/2019)Jenkins还不支持在一个Workspace中检出多个仓库,至少通过Jenkins + Git插件是不太可行的。可以考虑通过多分支流水解决该问题。

如果在「Source Code Management」的「Repositories」中设置多个「Repository URL」参数,那么Jenkins的行为有些”奇怪“。之所以这么说,是因为在工作目录中的仓库是这个样子的:

[core]
    repositoryformatversion = 0
    filemode = true
    bare = false
    logallrefupdates = true
[remote "origin"]
    url = http://gitlab.example.org/foo/bar.git
    fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
    remote = origin
    merge = refs/heads/master
[branch "develop"]
    remote = origin
    merge = refs/heads/develop
[remote "origin1"]
    url = http://gitlab.example.org/foo_b/bar_b.git
[remote "origin2"]
    url = http://gitlab.example.org/foo_a/bar_a.git

如上所示,Jenkins将仓库地址加入到当前仓库中,这就是我所说的“奇怪”。

关于「Branches to build」的「Branch Specifier (blank for ‘any’)」参数

参数「Branch Specifier (blank for ‘any’)」也可以添加多个,但是依旧不能”直接“决定要构建的分支,甚至构建顺序与设置的顺序也无任何关系。

当仅这只一个「Branch Specifier (blank for ‘any’)」参数时,并且明确指定分支,则不存在任何问题,构建的分支为指定的分支。

但是,当指定多个「Branch Specifier (blank for ‘any’)」参数时:(1)构建分支为自动化(Webhooks)推送的分支;(2)当手动构建时,为上次构建时所使用的分支(当然你如果中途调整,情况可能会复杂);

这就好像,虽然悬崖边没有护栏,但是你不过去就不会有危险。