创建一个基于pipeline流水线的项目
第一个选项是流水线脚本(不是shell脚本),右上角有一个脚本的范例
范例:
1:尝试自己写一个脚本
2:hello world脚本(里面是函数,输出的内容,里面的node就代表单台服务器的节点,然后输出了一句话)
3:maven脚本
第二个是脚本在别的地方你把它取回来
Pipeline脚本语法架构介绍
#Pipeline脚本语法架构
node ('slave节点名') { #被操控的节点服务器
def 变量 #def可以进行变量声明
stage('阶段名A'){ #流水线阶段一
执行步骤A
执行步骤B
执行步骤C
}
stage('阶段名B'){ #流水线阶段二
执行步骤A
执行步骤B
执行步骤C
}
stage('阶段名C'){ #流水线阶段三
执行步骤A
执行步骤B
执行步骤C
}
}
添加项目git参数化构建
在流水线里面git参数化构建是不让用
利用Pipeline Syntax,编写Pipeline Script并构建
(1)进入Pipeline Syntax
通过脚本代码生成器,生成Pipeline脚本代码
将生成的代码复制到流水线脚本相应步骤的stage函数里
如果执行失败的话,可能时间不同步(时间间隔差的很多)
安装时间服务器
[root@localhost ~]# yum install -y ntpdate
/usr/sbin/ntpdate ntp1.aliyun.com # 时间同步
/bin/cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime #覆盖成上海的时区
[root@localhost ~]# systemctl reload jenkins
然后平滑重启Jenkins服务
(1)从远程仓库下载pipeline script,并构建
[root@localhost ~]# su - git #切换用户
Last login: Thu Dec 27 08:56:53 CST 2018 from 192.168.182.148 on pts/1
Last failed login: Thu Dec 27 17:21:45 CST 2018 from 192.168.182.148 on ssh:notty
There were 4 failed login attempts since the last successful login.
[git@localhost ~]$ pwd
/home/git
[git@localhost ~]$ cd /home/git/repos/
[git@localhost repos]$ ls
app.git
[git@localhost repos]$
[git@localhost repos]$ pwd
/home/git/repos
[git@localhost repos]$ mkdir jenkinsfile #创建存放pipeline脚本的仓库
[git@localhost repos]$ cd jenkinsfile/
[git@localhost jenkinsfile]$ git --bare init #初始化仓库
Initialized empty Git repository in /home/git/repos/jenkinsfile/
(2)在Jenkins服务器上,往远程仓库提交一个pipeline脚本
[root@localhost test]# git clone git@192.168.182.149:/home/git/repos/jenkinsfile
Cloning into 'jenkinsfile'...
warning: You appear to have cloned an empty repository.
[root@localhost test]# ls
jenkinsfile
[root@localhost test]# cd jenkinsfile/
[root@localhost jenkinsfile]# mkdir itemA
[root@localhost jenkinsfile]# vim itemA/jenkinsfile
#将脚本推送到远程仓库的master分支
[root@localhost jenkinsfile]# git add *
[root@localhost jenkinsfile]# git commit -m "第一次提交"
[master (root-commit) 39752f6] 第一次提交
1 file changed, 19 insertions(+)
create mode 100644 itemA/jenkinsfile
[root@localhost jenkinsfile]# git push -u origin master