创建一个基于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
Counting objects: 4, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (4/4), 565 bytes | 0 bytes/s, done.
Total 4 (delta 0), reused 0 (delta 0)
To git@192.168.182.149:/home/git/repos/jenkinsfile* [new branch]      master -> master
Branch master set up to track remote branch master from origin.

(3)利用远程仓库里的pipeline脚本,进行流水线构建

Jenkins+pipeline+Git+PHP一键部署博客(项目流水线自动发布)

一般情况下流水线用在了线上环境上,一般测试环境是不用流水线的

参数化构建一般都是测试环境下去做的,线上环境下,默认拉取的是master代码

下载用于自动化发布的PHP源码wordpress源码包,并上传远程git仓库

#在git服务器上创建用于存放源码的Git仓库
[root@localhost /]# cd /home/git/repos/
[root@localhost repos]# ls
app.git  jenkinsfile
[root@localhost repos]# mkdir wordpress
[root@localhost repos]# cd wordpress/
[root@localhost wordpress]# git --bare init
初始化空的 Git 版本库于 /home/git/repos/wordpress/
[root@localhost wordpress]# cd ..
[root@localhost repos]# chown -R git.git wordpress/
[root@localhost repos]# ll
总用量 0
drwxrwxr-x. 8 git git 130 12月 26 15:40 app.git
drwxrwxr-x. 7 git git 119 12月 28 16:36 jenkinsfile
drwxr-xr-x. 7 git git 119 12月 28 19:49 wordpress#在Jenkins服务器,克隆创建好的远程Git仓库
[root@localhost ~]# mkdir php-app
[root@localhost ~]# ls
anaconda-ks.cfg                jdk-8u171-linux-x64.tar.gz    php-app     test
apache-maven-3.5.0-bin.tar.gz  jenkins-2.156-1.1.noarch.rpm  pipline.sh  zxw
[root@localhost ~]# cd php-app/
[root@localhost php-app]# git clone git@192.168.182.149:/home/git/repos/wordpress
Cloning into 'wordpress'...
warning: You appear to have cloned an empty repository.
[root@localhost php-app]# ls
wordpress#下载wordprss源代码
[root@localhost php-app]# wget https://cn.wordpress.org/wordpress-4.9.4-zh_CN.tar.gz
--2018-12-28 19:54:40--  https://cn.wordpress.org/wordpress-4.9.4-zh_CN.tar.gz
Resolving cn.wordpress.org (cn.wordpress.org)... 198.143.164.252
Connecting to cn.wordpress.org (cn.wordpress.org)|198.143.164.252|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 9082696 (8.7M) [application/octet-stream]
Saving to: ‘wordpress-4.9.4-zh_CN.tar.gz’100%[============================================>] 9,082,696    360KB/s   in 63s    2018-12-28 19:55:45 (140 KB/s) - ‘wordpress-4.9.4-zh_CN.tar.gz’ saved [9082696/9082696][root@localhost php-app]# ls
wordpress  wordpress-4.9.4-zh_CN.tar.gz
[root@localhost php-app]# tar xf wordpress-4.9.4-zh_CN.tar.gz -C wordpress/            #解压包
[root@localhost wordpress]# mv wordpress/* .
[root@localhost wordpress]# ls
index.php        wp-admin              wp-cron.php        wp-mail.php
license.txt      wp-blog-header.php    wp-includes        wp-settings.php
readme.html      wp-comments-post.php  wp-links-opml.php  wp-signup.php
wordpress        wp-config-sample.php  wp-load.php        wp-trackback.php
wp-activate.php  wp-content            wp-login.php       xmlrpc.php
[root@localhost wordpress]# rm -rf wordpress/
[root@localhost wordpress]# ls
index.php        wp-blog-header.php    wp-includes        wp-settings.php
license.txt      wp-comments-post.php  wp-links-opml.php  wp-signup.php
readme.html      wp-config-sample.php  wp-load.php        wp-trackback.php
wp-activate.php  wp-content            wp-login.php       xmlrpc.php
wp-admin         wp-cron.php           wp-mail.php#在Jenkins上提交代码到远程Git仓库
[root@localhost wordpress]# git add *
[root@localhost wordpress]# git commit -m "第一次提交"
[root@localhost wordpress]# git push -u origin master
Counting objects: 1660, done.
Compressing objects: 100% (1635/1635), done.
Writing objects: 100% (1660/1660), 8.86 MiB | 4.97 MiB/s, done.
Total 1660 (delta 173), reused 0 (delta 0)
To git@192.168.182.149:/home/git/repos/wordpress* [new branch]      master -> master
Branch master set up to track remote branch master from origin.

设置分布式构建的slave管理节点

我们计划利用分布式构建的方式,启动pipeline的流水线项目发布
slave管理节点就设置为需要用于发布项目的Web服务器

添加并设置slave管理从节点

slave从节点安装java环境,并启动jenkins的slave管理节点

1.   #解压安装jdk
[root@Web ~]# tar xf jdk-8u171-linux-x64.tar.gz -C /usr/local
[root@Web ~]# cd /usr/local
[root@Web local]# mv jdk1.8.0_171 jdk
[root@Web local]# /usr/local/jdk/bin/java -version
java version "1.8.0_171"
Java(TM) SE Runtime Environment (build 1.8.0_171-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.171-b11, mixed mode)#配置java环境
[root@Web ~]# sed -i.org '$a export JAVA_HOME=/usr/local/jdk/' /etc/profile
[root@Web ~]# sed -i.org '$a export PATH=$PATH:$JAVA_HOME/bin' /etc/profile
[root@Web ~]# sed -i.org '$a export CLASSPATH=.$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/lib/tools.jar' /etc/profile
[root@Web ~]# tail -3 /etc/profile
export JAVA_HOME=/usr/local/jdk/
export PATH=$PATH:$JAVA_HOME/bin
export CLASSPATH=.$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/lib/tools.jar
[root@Web ~]# source /etc/profile
[root@Web ~]# java -version
java version "1.8.0_171"
Java(TM) SE Runtime Environment (build 1.8.0_171-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.171-b11, mixed mode)

Web服务器安装LNMP环境,并手动拉取代码模拟访问

[root@localhost html]# yum -y install epel-release
[root@localhost html]# yum -y install nginx php-fpm php-mysql
[root@localhost html]# cd /etc/nginx/
[root@localhost nginx]# ls
conf.d                  koi-utf             scgi_params
default.d               koi-win             scgi_params.default
fastcgi.conf            mime.types          uwsgi_params
fastcgi.conf.default    mime.types.default  uwsgi_params.default
fastcgi_params          nginx.conf          win-utf
fastcgi_params.default  nginx.conf.default
[root@localhost nginx]# egrep -v "#|^$" nginx.conf.default > nginx.conf#修改配置文件
[root@localhost nginx]# cat nginx.conf
worker_processes  1;
events {worker_connections  1024;
}
http {include       mime.types;default_type  application/octet-stream;sendfile        on;keepalive_timeout  65;server {listen       80;server_name  www.yunjisuan.com;location / {root   /usr/share/nginx/html/www.yunjisuan.com;index  index.html index.htm index.php;location ~\.php {fastcgi_index index.php;fastcgi_pass 127.0.0.1:9000;include fastcgi.conf;}}}}#创建网页目录
[root@Web ~]# cd /usr/share/nginx/html/
[root@Web html]# ls
404.html  50x.html  index.html  nginx-logo.png  poweredby.png
[root@Web html]# mkdir www.yunjisuan.com
[root@Web html]# cd www.yunjisuan.com#克隆Git仓库代码到本地网页目录
[root@localhost nginx]# yum install git
[root@localhost nginx]# git clone git@192.168.182.149:/home/git/repos/wordpress
[root@localhost nginx]# ls
[root@localhost nginx]# cd wordpress/
[root@localhost nginx]# mv wordpress/* .
[root@localhost nginx]# rm -rf wordpress/#将网页目录授权给Apache程序用户
[root@localhost nginx]# cd ..
[root@localhost nginx]# id apache
[root@localhost nginx]# chown apache.apache /usr/share/nginx/html/www.yunjisuan.com/#启动nginx服务和php-fpm服务
[root@localhost nginx]# systemctl start nginx
[root@localhost nginx]# systemctl start php-fpm
[root@localhost nginx]# ss -antup | egrep "80|9000"
tcp    LISTEN     0      128    127.0.0.1:9000                  *:*                   users:(("php-fpm",pid=11801,fd=0),("php-fpm",pid=11769,fd=0),("php-fpm",pid=11768,fd=0),("php-fpm",pid=11767,fd=0),("php-fpm",pid=11766,fd=0),("php-fpm",pid=11765,fd=0),("php-fpm",pid=11763,fd=6))
tcp    LISTEN     0      128       *:80                    *:*                   users:(("nginx",pid=11756,fd=6),("nginx",pid=11755,fd=6))

做好主机映射,浏览器访问测试

在远程Git仓库中创建一个用于构建的Pipeline脚本

#在Jenkins服务器上操作
[root@localhost ~]# rm -rf test/
[root@localhost ~]# mkdir test
[root@localhost ~]# cd test/
[root@localhost test]# git clone git@192.168.182.149:/home/git/repos/jenkinsfile
Cloning into 'jenkinsfile'...
remote: Counting objects: 8, done.
remote: Compressing objects: 100% (5/5), done.
remote: Total 8 (delta 1), reused 0 (delta 0)
Receiving objects: 100% (8/8), done.
Resolving deltas: 100% (1/1), done.
[root@localhost test]# ls
jenkinsfile
[root@localhost test]#
[root@localhost test]# cd jenkinsfile/
[root@localhost jenkinsfile]# cd itemA/#通过流水线脚本生成器生成如下脚本内容
[root@localhost itemA]# cat jenkinsfile-php-wp         #这个是我以前做的,可以复制一份,然后改个名字,在进行修改
node ("php-slave1-192.168.182.151") {//def mvnHomestage('Git php-wp checkout') {checkout([$class: 'GitSCM', branches: [[name: '${branch}']],doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [],userRemoteConfigs: [[credentialsId: 'df31cc32-ccd2-4748-ae2f-ac971eef9189',url: 'git@192.168.182.149:/home/git/repos/wordpress']]])}stage('cpde copy') {sh ''' rm-rf ${WORKSPACE}/.git             #这个是Jenkins的内置变量=/var/lib/jenkins/项目名字  ,然后把.git删除了[ -d /data/backup ] || mkdir -p /data/backupmv /usr/share/nginx/html/www.yunjisuan.com /data/backup/www.yunjisuan.com-$(date +%F-%T)cp -rf ${WORKSPACE} /usr/share/nginx/html/www.yunjisuan'''}stage('test') {sh 'curl http://www.yunjisuan.com/status.html'}
}#推送到git远程仓库
[root@localhost itemA]# git add *
[root@localhost itemA]# git commit "pipeline脚本测试"
[root@localhost itemA]# git push -u origin master

流水线脚本生成器的代码

在PHP项目代码里增加Pipeline验证用的测试页面

[root@www test]# git clone git@192.168.182.149:/home/git/repos/wordpress
Cloning into 'wordpress'...
git@192.168.182.149's password:
remote: Counting objects: 1660, done.
remote: Compressing objects: 100% (1462/1462), done.
remote: Total 1660 (delta 173), reused 1660 (delta 173)
Receiving objects: 100% (1660/1660), 8.86 MiB | 14.46 MiB/s, done.
Resolving deltas: 100% (173/173), done.
[root@www test]# ls
wordpress
[root@www test]# cd wordpress/
[root@www wordpress]# ls
index.php        wp-blog-header.php    wp-includes        wp-settings.php
license.txt      wp-comments-post.php  wp-links-opml.php  wp-signup.php
readme.html      wp-config-sample.php  wp-load.php        wp-trackback.php
wp-activate.php  wp-content            wp-login.php       xmlrpc.php
wp-admin         wp-cron.php           wp-mail.php
[root@www wordpress]# echo "OK-versionV2.0" >> status.html#将测试用页面提交到远程Git仓库
[root@www wordpress]# git add *
[root@www wordpress]# git commit -m "version V2.0"
[root@www wordpress]# git push -u origin master#在web服务器做域名映射(应为要进行curl验证)
[root@www wordpress]# tail -1 /etc/hosts
192.168.182.151 www.yunjisuan.com

浏览器访问Jenkins进行php项目流水线发布构建

输出这句话代表成功了,能登录博客了

主节点一般都不敢事情,一般给从节点下任务,从节点以远程的方式部署别人,实现用ssh,去给别人发脚本,然后Jenkins上装一个插件,让ssh去支持pipeline流水线

用ssh方式发送脚本

对上图做解释
第一行:第一变量,什么都不写
第二行:远程主机的名字
第三行:远程主机的IP地址
第四行:远程账号
第五行:远程密码
第六行:不动,照抄
第七行:剩下的都是代码,writefile:写一个文件,先把text后面的值先入到abc.sh这个脚本中,然后在吧这个脚本发到远程服务器上,sshscript:远程位置以及名字,最终写到了空间目录上

部署ssh脚本

[root@localhost itemA]# cat jenkinsfile-php-wp
node ("php-slave1-192.168.182.151") {def remote = [:]remote.name = 'test'remote.user = 'root'remote.allowAnyHosts = truestage('Git php-wp checkout') {sh 'echo "`hostname -I`"'checkout([$class: 'GitSCM', branches: [[name: '${branch}']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'df31cc32-ccd2-4748-ae2f-ac971eef9189', url: 'git@192.168.182.149:/home/git/repos/wordpress']]])}stage('ssh 182.152') {remote.host = '192.168.182.152'remote.password = '666666'writeFile file: 'ssh.sh', text:'echo "`hostname -I`welcoame to yunjisuan"'sshScript remote:remote, script: "ssh.sh"}stage('ssh 182.153') {remote.host = '192.168.182.153'remote.password = '666666'writeFile file: 'ssh.sh', text:'''
echo "`hostname -I`"
echo "`hostname -I`"
echo "`hostname -I`"
echo "`hostname -I`"
'''sshScript remote:remote, script: "ssh.sh"
}stage('test') {}
}[root@localhost itemA]# git add *
[root@localhost itemA]# git commit -m "333"
[master c3a9270] 3331 file changed, 22 insertions(+), 7 deletions(-)
[root@localhost itemA]# git push -u origin master
Counting objects: 7, done.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (4/4), 757 bytes | 0 bytes/s, done.
Total 4 (delta 0), reused 0 (delta 0)
To git@192.168.182.149:/home/git/repos/jenkinsfile76f178d..c3a9270  master -> master
分支 master 设置为跟踪来自 origin 的远程分支 master。


A是上线的,B是备用的,前面是nginx
反向代理,推4个web服务器,后面是共享数据库和存储,例如双十一数据量特别大,A,B互相切换,

一但gitlab里的master版本有变化,或者是主动激活,钩子激活,触发Jenkins代码上线,触发slave覆盖到B业务架构,A,B切换的,把所有的流量都引流到B业务 ,

pipeline流水线及分布式流水线发布php项目相关推荐

  1. Pipeline流水线及分布式流水线发布PHP项目及JAVA项目

    Jenkins的Pipeline流水线 主机名 IP地址 备注 Git 192.168.146.136 Git服务器 Jenkins 192.168.146.137 Jenkins服务器 Pipeli ...

  2. jenkins+kubernetes(k8s)发布Springboot项目

    jenkins+kubernetes(k8s)发布Java项目 准备工作 机器列表 镜像列表 创建一个java项目 创建jenkins镜像 Jenkins服务配置 启动jenkins 访问jenkin ...

  3. 【DevOps】Jenkins:配置jenkins 流水线/多分支流水线任务构建成功通知企业微信@相关人

    目录 视频教程 需求说明 最终效果 实现过程 安装插件 编写shell 编写jenkinsfile 测试一下 视频教程 视频教程:https://edu.csdn.net/learn/38191/60 ...

  4. Jenkins-pipeline + gitlab + nodejs 自动发布vue项目

    Jenkins-pipeline + gitlab + nodejs 自动发布vue项目 环境 OS SOFTWARE NOTE CentOS7.4 Jenkins2.327 x nodejs16.3 ...

  5. VMware顺容器之势而为,发布开源项目Lightwave和Photon

    本文讲的是VMware顺容器之势而为,发布开源项目Lightwave和Photon,[编者的话]VMware是老牌的虚拟化技术提供商,但Docker等容器技术对其核心业务造成了不小的冲击,为了应付这种 ...

  6. 引入yml依赖包_手把手教你发布 Python 项目开源包

    编译:机器之心,作者:Gabriel Lerner.Nathan Toubiana 好不容易码了个 python 项目,是不是很兴奋?那么怎么把这个项目发出去让大家看到呢?本文作者写了一份在 GitH ...

  7. 使用 jetty-maven-plugin发布maven项目

    2019独角兽企业重金招聘Python工程师标准>>> 使用 jetty-maven-plugin发布maven项目 1.先去maven官方查找插件 jetty-maven-plug ...

  8. Linux上部署web服务器并发布web项目

    近在学习如何在linux上搭建web服务器来发布web项目,由于本人是linux新手,所以中间入了不少坑,搞了好久才搞出点成果. 以下是具体的详细步骤以及我对此做的一些总结和个人的一些见解,希望对跟我 ...

  9. Tomcat发布Maven项目遇到异常:java.lang.OutOfMemoryError: PermGen space

    前言: 本问题出现在tomcat 7发布 web3.0Maven项目的时候出现. 问题阐述: 异常:java.lang.OutOfMemoryError:PermGen space 解决如下: 1.  ...

最新文章

  1. 【java】兴唐第二十三节课(暑期第一节TreeSet)
  2. 合工大python期末复习知识点汇总
  3. 映射网络驱动器会自动断开的解决方法
  4. linux服务器用哪个面板好,Linux服务器管理面板哪家比较好用?
  5. 【Android】魅族Flyme OS 3摄像头无法预览的问题
  6. 计算机组成原理完整学习笔记(三):存储器
  7. 用于HTML5移动开发的10大移动APP开发框架
  8. uni-app微信小程序短信验证、微信支付
  9. 二手房数据分析预测系统
  10. WIN11电脑如何使用IE浏览器进行正常办公操作-以建行网银为例
  11. html5 励志名言,5个字的励志名言
  12. Linux服务器开通443端口
  13. AR技术简谈:相关原理,技术应用以及设备推荐,带你感受虚拟信息与真实世界巧妙融合。
  14. Unity3D 矩阵运算
  15. 金额大小写转化、阿拉伯数字转大写数字,大写数字转阿拉伯数字
  16. localStorage使用实例-进入显示广告,点击关闭之后,刷新网页不再出现
  17. 评高级用计算机合格证吗,副教授评审什么情况下可以免计算机合格证
  18. 三维人脸重建 (一)
  19. 【C语言】利用条件运算符的嵌套完成学习成绩的例题
  20. 原生JS超级马里奥(第五天)

热门文章

  1. win10 dos命令行修改DNS
  2. Windows 10 LTSC官方版本下载地址
  3. BiliBili2020校招笔试题
  4. Day 5:自己编写的mysql类
  5. 颈椎病及腰椎间盘突出病因病理
  6. 如何避免过敏,哮喘和湿疹断奶宝宝正确
  7. ai与虚拟现实_AI医疗的神话与现实
  8. java泛型的上界和下界_java泛型中的上界(extend)和下界(super)
  9. 浏览器导航被劫持解决方法
  10. oracle重做control,Oracle 通过Database Control 向重做日志组中添加成员