Jenkins Pipeline 中使用 Git 插件对项目进行 Pull 与 Push

Jenkins Pipeline 中使用 Git 插件对项目进行 Pull 与 Push

文章目录

  !版权声明:本博客内容均为原创,每篇博文作为知识积累,写博不易,转载请注明出处。


系统环境:

  • Jenkins 版本:2.233

一、简介

       在 Jnekins 安装后,一般都会默认安装上 Git 插件,在写 Pipeline 脚本时候,也经常使用 Git 插件从 Git 仓库拉取项目进行编译,可以说使用 Git 已经是日常化操作,如何使用 Git 插件从 Git 远程仓库拉取与推送代码更是常用命令,这里介绍下如果通过 Git 插件进行这些操作。

这里为了演示方便,使用的是 Github 仓库。

二、Git 环境配置

1、配置 Git 工具

       一般安装完 Jenkins 会自动安装 Jenkins Git 插件,不过该插件也需要和 Git 工具配合使用,要求 Jenkins 所在服务器上也要安装 Git 并且配置环境。由于 Jnekins 安装现在有多种方式,常用的则是服务器上直接 Jenkins War 包启动,或者使用 Jenkins Docker 镜像启动 ,根据这两种情况,则:

  • 如果是服务器方式安装 Jenkins,需要服务器上线安装 Git 工具,然后在 "全局工具配置" 中指定 Git 工具地址。
  • 如果是 Docker 镜像方式安装 Jnekins,那么 Jenkins 镜像中已经包含 Git 工具,且已经配置好环境,在 "全局工具配置" 中 Git 工具一栏已经默认配置 Git 工具路径为 git 了,所以一般不需要配置 Git 相关设置。

Docker 启动各种工具与组件已经是大势所趋,所以,本人也是基于 Docker 镜像方式安装的 Jnekins,下面检查下是否安装 Git 插件以及配置 Git 环境。

打开 系统管理->全局工具设置->Git 工具 查看是否配置下面参数:

2、配置 Git 认证凭据

使用 Git 前还得提前配置 Git 环境,例如,配置 Git 仓库的用户名/密码的凭据,操作如下,打开 凭据->系统->全局凭据->添加凭据 添加 git 仓库的 "用户名/密码信息":

  • 用户名: Git 仓库的用户名。
  • 密码: Git 仓库的密码。
  • ID: 凭据的ID号,Pipeline 脚本中会使用到该 ID 来读取用户名/密码信息,这里设置该 ID 为 git-global-credentials,如果不设置则系统会生成一个随机 ID 值(不太直观)。

三、创建一个用于测试的 Jenkins Pipeline Job

创建一个 Jenkins Pipeline Job,然后配置里面的脚本,里面输入一些 Git 命令,然后进行测试。

这里使用是 Groovy Script 形式的 Jnekins Pipeline 脚本。

1、创建 Jenkins Job

2、配置 Jenkins Job Pipeline 脚本

进入 Jenkins Job 配置,然后拉倒最后输入 groovy 语法的 pipeline 脚本对 Git 工具进行测试,查看 Git 命令是否能执行:

脚本内容如下:

1node(){
2    sh "git version"
3}

3、然后进行执行构建测试

配置 Job 完成后,进入到 Job 中,然后点击 "立即构建" 安装执行 Pipeline 脚本:

然后输入日志内容如下:

 1Started by user 管理员
 2Running in Durability level: PERFORMANCE_OPTIMIZED
 3[Pipeline] Start of Pipeline
 4[Pipeline] node
 5Running on Jenkins in /var/jenkins_home/workspace/git-test
 6[Pipeline] {
 7[Pipeline] sh
 8+ git version
 9git version 2.11.0
10[Pipeline] }
11[Pipeline] // node
12[Pipeline] End of Pipeline
13Finished: SUCCESS

四、创建使用 Git 的 Jenkins Pipeline 脚本

接下来写一个完整的 Pipeline 脚本,脚本中使用 Git 插件进行 Git 远程仓库代码拉取与推送,脚本写法如下,这里分步骤进行。

1、远程 git 仓库拉取代码脚本

远程仓库代码拉取可以直接使用 git 插件提供好的方法执行拉取操作,使用如下:

  • node: 指定执行任务的节点,如果为空则寻找能够使用的节点,先从 master 开始,如果设置了 master 为不可执行任务,则使用 slave 节点。
  • stage: 设置脚本当前执行的阶段,相当于做标记,让脚本结构更加清晰。
  • git: Git 插件提供的方法,只能用于从远程 Git 仓库拉取代码操作。
    • url:远程仓库的地址。
    • branch:指定拉取的远程分支。
    • credentialsId:远程 git 仓库的用户名/密码认证凭据,这里使用的是上面步骤中配置好的凭据。
1node(){
2    stage("Git 远程仓库拉取"){
3        git url: "https://github.com/my-dlq/test.git",
4            credentialsId: "git-global-credentials",
5            branch: "master"
6    }
7}

2、拉取与推送代码到远程 Git 仓库脚本

上面执行了远程代码拉取,不过在实际使用中,我们经常会拉取远程代码,然后修改一些内容,然后再 commit 与 push 到远程仓库,这些操作在 Pipeline 中的用法如下:

  • withCredentials: 根据凭据 ID 引入凭据,然后使用 usernamePassword 策略,将凭据中的用户名/密码信息分别赋予 usernameVariable 与 passwordVariable 两个变量。
 1node(){
 2    // 先执行从远程 Git 仓库拉取代码
 3    stage("Git 远程仓库拉取"){
 4        git url: "https://github.com/my-dlq/test.git",
 5            credentialsId: "git-global-credentials",
 6            branch: "master"
 7    }
 8    // 再执行添加新的文件,然后推送到远程 Git 仓库
 9    stage("推送到 Git 远程仓库"){
10        withCredentials([usernamePassword(credentialsId:"git-global-credentials",
11                                          usernameVariable: "GIT_USERNAME", 
12                                          passwordVariable: "GIT_PASSWORD")]) {
13            // 配置 Git 工具中仓库认证的用户名、密码
14            sh 'git config --local credential.helper "!p() { echo username=\\$GIT_USERNAME; echo password=\\$GIT_PASSWORD; }; p"'
15            // 配置 git 变量 user.name 和 user.emai
16            sh 'git config --global user.name "my-dlq"'
17            sh 'git config --global user.email "mynamedlq@163.com"'
18            // 创建新的文件,存入 git 项目,内容为当前时间,方便后续测试
19            sh 'date | tee aaa.txt'
20            // 将文件推送到 git 远程仓库
21            sh 'git add -f aaa.txt'
22            sh 'git commit -m "进行 git 测试"'
23            sh 'git push -u origin master'
24        }
25    }
26}

注意:上面配置的 Git 环境中的 user.name 和 user.email 一定要和 Github 中用户设置的保持一致,不然会导致权限验证错误。

五、在 Job 中使用 Git 测试脚本

修改测试 Job 的配置,设置其中的 Pipeline Script 为上面的测试 Git 脚本:

然后执行任务构建,可以看到下面日志信息:

 1Started by user 管理员
 2Running in Durability level: PERFORMANCE_OPTIMIZED
 3[Pipeline] Start of Pipeline
 4[Pipeline] node
 5Running on Jenkins in /var/jenkins_home/workspace/git-test
 6[Pipeline] {
 7[Pipeline] stage
 8[Pipeline] { (Git 远程仓库拉取)
 9[Pipeline] git
10using credential git-global-credentials
11 > git rev-parse --is-inside-work-tree # timeout=10
12Fetching changes from the remote Git repository
13 > git config remote.origin.url https://github.com/my-dlq/test.git # timeout=10
14Fetching upstream changes from https://github.com/my-dlq/test.git
15 > git --version # timeout=10
16using GIT_ASKPASS to set credentials Github 用户名/密码凭据
17 > git fetch --tags --progress -- https://github.com/my-dlq/test.git +refs/heads/*:refs/remotes/origin/* # timeout=10
18 > git rev-parse refs/remotes/origin/master^{commit} # timeout=10
19 > git rev-parse refs/remotes/origin/origin/master^{commit} # timeout=10
20Checking out Revision b2bafc3cffb6321a0d088d87f95dc410199a2276 (refs/remotes/origin/master)
21 > git config core.sparsecheckout # timeout=10
22 > git checkout -f b2bafc3cffb6321a0d088d87f95dc410199a2276 # timeout=10
23 > git branch -a -v --no-abbrev # timeout=10
24 > git branch -D master # timeout=10
25 > git checkout -b master b2bafc3cffb6321a0d088d87f95dc410199a2276 # timeout=10
26Commit message: "进行 git 测试"
27 > git rev-list --no-walk b2bafc3cffb6321a0d088d87f95dc410199a2276 # timeout=10
28[Pipeline] }
29[Pipeline] // stage
30[Pipeline] stage
31[Pipeline] { (推送到 Git 远程仓库)
32[Pipeline] withCredentials
33Masking supported pattern matches of $GIT_USERNAME or $GIT_PASSWORD
34[Pipeline] {
35[Pipeline] sh
36+ git config --local credential.helper !p() { echo username=$GIT_USERNAME; echo password=$GIT_PASSWORD; }; p
37[Pipeline] sh
38+ git config --global user.name ****
39[Pipeline] sh
40+ git config --global user.email mynamedlq@163.com
41[Pipeline] sh
42+ date
43+ tee aaa.txt
44Sat Apr 25 16:01:10 UTC 2020
45[Pipeline] sh
46+ git add -f aaa.txt
47[Pipeline] sh
48+ git commit -m 进行 git 测试
49[master a3a9ae2] 进行 git 测试
50 1 file changed, 1 insertion(+), 1 deletion(-)
51[Pipeline] sh
52+ git push -u origin master
53To https://github.com/****/test.git
54   b2bafc3..a3a9ae2  master -> master
55Branch master set up to track remote branch master from origin.
56[Pipeline] }
57[Pipeline] // withCredentials
58[Pipeline] }
59[Pipeline] // stage
60[Pipeline] }
61[Pipeline] // node
62[Pipeline] End of Pipeline
63Finished: SUCCESS

已经验证能顺利的拉取与推送项目到远程 Git 仓库,到此结束。

---END---


  !版权声明:本博客内容均为原创,每篇博文作为知识积累,写博不易,转载请注明出处。