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

本文详细介绍了使用Jenkins Pipeline实现PHP博客项目和Java项目的自动化发布流程,包括多节点流水线构建、从节点的SSH远程部署、Git参数化构建等。通过Pipeline DSL,将CD流程作为代码进行版本控制,确保了发布过程的可靠性和可重复性。
摘要由CSDN通过智能技术生成
Jenkins的Pipeline流水线
主机名 IP地址 备注
Git 192.168.146.136 Git服务器
Jenkins 192.168.146.137 Jenkins服务器
  1. Pipeline流水线介绍

(1) Jenkins Pipeline(或者简称为“Pipeline”)是一套插件,支持在 Jenkins 中实施和集成持续交付流水线。

持续交付(CD)流水线是将软件从版本控制发布到用户和客户的过程的自动化表达。对软件的每一次改变(在源代码控制中提交)都会在发布过程中经历一个复杂的过程。这个过程包括以可靠和可重复的方式构建软件,以及通过测试和部署的多个阶段来推进构建的软件(称为“构建”)。

Pipeline 提供了一套可扩展的工具,用于通过 Pipeline domain-specific language(DSL)语法将交付流水线“作为代码”建模。

Jenkins Pipeline 的定义写在称为 Jenkinsfile 的文本文件中,这个文件可以提交到项目的代码控制仓库。这是“Pipeline-as-code”的基础。 将 CD 流水线作为应用程序的一部分进行版本控制,并像任何其他代码一样进行审查。

(2) 创建 Jenkinsfile 文件并且提交到版本控制有下面几个好处:

对所有的 branches 和 pull 请求自动创建 Pipeline。
Pipeline 上的代码审查/迭代(along with the remaining source code)。
审核追踪 Pipeline
Pipeline 的单一真实来源,可由项目的多个成员查看和编辑。

虽然用于定义 Pipeline 的语法无论是在 Web UI 中还是在 Jenkinsfile 中是相同的,但在 Jenkinsfile 中定义 Pipeline 并提交到源代码控制中通常被认为是最佳实践。

(3) Pipeline的几个基本概念:

Stage: 阶段,一个Pipeline可以划分为若干个Stage,每个Stage代表一组操作。注意,Stage是一个逻辑分组的概念,可以跨多个Node。
Node: 节点,一个Node就是一个Jenkins节点,或者是Master,或者是Agent,是执行Step的具体运行期环境。
Step: 步骤,Step是最基本的操作单元,小到创建一个目录,大到构建一个Docker镜像,由各类Jenkins Plugin提供。
  1. 创建一个基于Pipeline流水线的项目
    在这里插入图片描述在这里插入图片描述在这里插入图片描述

  2. 添加项目Git参数化构建
    在这里插入图片描述
    在这里插入图片描述node:代表单台服务器的节点

  3. Pipeline脚本语法架构介绍

#Pipeline脚本语法架构
node ('slave节点名') {          #被操控的节点服务器
   def 变量    #def可以进行变量声明
   stage('阶段名A'){     #流水线阶段一
       执行步骤A
       执行步骤B
       执行步骤C
   }
   stage('阶段名B'){     #流水线阶段二
       执行步骤A
        执行步骤B
       执行步骤C
   }
    stage('阶段名C'){     #流水线阶段三
       执行步骤A
       执行步骤B
       执行步骤C
   }
}

在这里插入图片描述

#流水线模板脚本
node {
   def mvnHome
   stage('Preparation') { // for display purposes
      // Get some code from a GitHub repository
      git 'https://github.com/jglick/simple-maven-project-with-tests.git'
      // Get the Maven tool.
      // ** NOTE: This 'M3' Maven tool must be configured
      // **       in the global configuration.           
      mvnHome = tool 'M3'
   }
   stage('Build') {
      // Run the maven build         # “//” 注释
      if (isUnix()) {
         sh "'${mvnHome}/bin/mvn' -Dmaven.test.failure.ignore clean package"
      } else {
         bat(/"${mvnHome}\bin\mvn" -Dmaven.test.failure.ignore clean package/)
      }
   }
   stage('Results') {
      junit '**/target/surefire-reports/TEST-*.xml'
      archive 'target/*.jar'
   }
}
  1. 利用Pipeline Syntax,编写Pipeline Script并构建
    (1)进入Pipeline Syntax
    在这里插入图片描述(2)通过脚本代码生成器,生成Pipeline脚本代码
    在这里插入图片描述(3)将生成的代码复制到流水线脚本相应步骤的stage函数里
    在这里插入图片描述在这里插入图片描述在这里插入图片描述

(4)开始构建Pipeline项目
在这里插入图片描述

[root@localhost workspace]# cd /test/
[root@localhost test]# cd app/
[root@localhost app]# ls
test.txt
[root@localhost app]# git remote -v
origin	git@192.168.146.136:/home/git/repos/app.git (fetch)
origin	git@192.168.146.136:/home/git/repos/app.git (push)
[root@localhost app]# git branch -a
* dev
  master
  test
  remotes/origin/HEAD -> origin/master
  remotes/origin/dev
  remotes/origin/master
  remotes/origin/test
[root@localhost app]# cd /var/lib/jenkins/
[root@localhost jenkins]# cd workspace/

在这里插入图片描述在这里插入图片描述在这里插入图片描述

  1. 从远程仓库下载Pipeline Script,并构建
    (1)在Git服务器上创建一个存放Pipeline脚本的仓库
[root@localhost ~]# su - git
Last login: Thu Dec 27 00:56:35 EST 2018 on pts/1
Last failed login: Thu Dec 27 00:58:54 EST 2018 from 192.168.146.137 on ssh:notty
There were 12 failed login attempts since the last successful login.
[git@localhost ~]$ cd /home/git/repos/
[git@localhost repos]$ ls
app.git
[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 ~]# cd /test/
[root@localhost test]# ls
app
[root@localhost test]# rm -rf *
[root@localhost test]# git clone git@192.168.146.136:/home/git/repos/jenkinsfile
Cloning into 'jenkinsfile'...
warning: You appear to have cloned an empty repository.
Checking connectivity... done.
[root@localhost test]# ls
jenkinsfile
[root@localhost test]# cd jenkinsfile/
[root@localhost jenkinsfile]# ls
[root@localhost jenkinsfile]# mkdir itemA
[root@localhost jenkinsfile]# ls
itemA
[root@localhost jenkinsfile]# mkdir itemB
[root@localhost jenkinsfile]# cd itemA
[root@localhost itemA]# vim jenkinsfile
node {
   //def mvnHome
   stage('Git checkout') {
   checkout([$class: 'GitSCM', 
   branches: [[name: '${branch}']], 
   doGenerateSubmoduleConfigurations: false, 
   extensions: [], submoduleCfg: [], 
   userRemoteConfigs: [[credentialsId: 'a240769e-ed1a-4456-90a8-f0e124024801', 
   url: 'git@192.168.146.136:/home/git/repos/app.git']]])    
   }
   stage('Maven Build') {
       echo "maven build..."
   }
   stage('Deploy') {
       echo "success..."
   }
   stage('Test') {
       echo "OK"
   } 
}
#将脚本推送到远程仓库的master分支
[root@localhost itemA]# git add *
[root@localhost itemA]# git commit -m "jenkinsfile commit"
[master (root-commit) 1f3715d] jenkinsfile commit
 1 file changed, 21 insertions(+)
 create mode 100644 itemA/jenkinsfile
[root@localhost itemA]# git push -u origin master
Counting objects: 4, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (4/4), 559 bytes | 0 bytes/s, done.
Total 4 (delta 0), reused 0 (delta 0)
To 192.168.146.136:/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博客项目流水线自动发布

主机名 IP地址 备注
Git 192.168.146.136 Git服务器
Jenkins 192.168.146.137 Jenkins服务器
Web 192.168.146.138 Web服务器

在这里插入图片描述

  1. 创建一个Pipeline流水线项目并进行参数化构建
    流水线一般情况下是用在线上环境,测试环境一般不用流水线,因为数据经常变更;测试环境不支持Git参数化构建,而线上环境只拉取master。
    由于我们仍旧打算将pipeline脚本放在远程Git仓库里,因此我们需要从远程Git仓库拉取Pipeline脚本,所以,参数化构建不支持Git的参数化。我们只能使用字符结构的参数化构建。
    在这里插入图片描述在这里插入图片描述
  2. 下载用于自动化发布的PHP源码wordpress源码包,并上传远程git仓库
#在Git中操作
[root@localhost ~]# su - git
Last login: Thu Dec 27 00:56:35 EST 2018 on pts/1
Last failed login: Thu Dec 27 00:58:54 EST 2018 from 192.168.146.137 on ssh:notty
There were 12 failed login attempts since the last successful login.
[git@localhost jenkinsfile]$ cd /home/git/repos/
[git@localhost repos]$ ls
app.git  jenkinsfile
[git@localhost repos]$ mkdir wordpress
[git@localhost repos]$ cd wordpress/
[git@localhost wordpress]$ git --bare init
Initialized empty Git repository in /home/git/repos/wordpress/
[git@localhost wordpress]$ ls
branches  config  description  HEAD  hooks  info  objects  refs

在NginxWeb中操作(提前创建jdk、maven、git环境)

[root@localhost git-2.9.5]# git config --global user.email "2914632996@qq.com"
[root@localhost git-2.9.5]# git config --global user.name "daisy"
[root@localhost ~]# mkdir -p /mycode

#下载wordpres源代码
[root@localhost ~]# tar xf wordpress-4.9.4-zh_CN.tar.gz -C /mycode/
[root@localhost ~]# cd /mycode/
[root@localhost mycode]# mv wordpress wordpress111
[root@localhost mycode]# ls
wordpress111

#在jenkins服务器上,克隆创建好的远程Git仓库
[root@localhost mycode]# git clone git@192.168.146.136:/home/git/repos/wordpress
Cloning into 'wordpress'...
The authenticity of host '192.168.146.136 (192.168.146.136)' can't be established.
ECDSA key fingerprint is SHA256:14NKIeIbfU5Cx3usA72K/AmnoeDt/UyH+SII8+rV1dA.
ECDSA key fingerprint is MD5:60:ab:53:4a:f8:fc:74:2b:93:1e:6f:b3:4c:e4:33:de.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.146.136' (ECDSA) to the list of known hosts.
git@192.168.146.136's password: 
warning: You appear to have cloned an empty repository.
Checking connectivity... done.
[root@localhost mycode]# ls
w
  • 0
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值