jenkins的流水线pipeline+项目实验php
声明:实验环境使用Jenkins的应用与搭建的环境
新建一个流水线
pipeline脚本语法架构
node('slave节点名'){
def 变量 #def可以进行变量声明
stage('阶段名A'){ #流水线阶段1
执行步骤A
执行步骤B
执行步骤C
}
stage('阶段名B'){ #流水线阶段2
执行步骤A
执行步骤B
执行步骤C
}
stage('阶段名C'){ #流水线阶段3
执行步骤A
执行步骤B
执行步骤C
}
}
用流水线脚本来构建流水线
首先在git服务器上建立放置流水线脚本的git仓库
[root@git ~]# su - git
Last login: Sun Dec :: CST from 192.168.200.132 on pts/
Last failed login: Tue Dec :: CST from 192.168.200.132 on ssh:notty
There were failed login attempts since the last successful login.
[git@git ~]$ ls
repos
[git@git ~]$ cd repos/
[git@git repos]$ ls
app.git
[git@git repos]$ mkdir -p jenkinsfile
[git@git repos]$ cd jenkinsfile/
[git@git jenkinsfile]$ git --bare init
Initialized empty Git repository in /home/git/repos/jenkinsfile/
[git@git jenkinsfile]$ ls
branches config description HEAD hooks info objects refs
[git@git jenkinsfile]$
在Jenkins服务器上上传流水线脚本到git仓库
[root@jenkins ~]# cd /test/
[root@jenkins test]# ls
app
[root@jenkins test]# git clone git@192.168.200.151:/home/git/repos/jenkinsfile
Cloning into 'jenkinsfile'...
warning: You appear to have cloned an empty repository.
[root@jenkins test]# ls
app jenkinsfile
[root@jenkins test]# cd jenkinsfile/
[root@jenkins jenkinsfile]# mkdir -p itemA
[root@jenkins jenkinsfile]# mkdir -p itemB
[root@jenkins jenkinsfile]# ls
itemA itemB
[root@jenkins jenkinsfile]# cd itemA
[root@jenkins itemA]# vim jenkinsfile
[root@jenkins itemA]# cat jenkinsfile
node {
// def mvnHome
stage('git checkout') {
checkout([$class: 'GitSCM',
branches: [[name: '${branch}']],
doGenerateSubmoduleConfigurations: false,
extensions: [], submoduleCfg: [],
userRemoteConfigs: [[credentialsId: '3d9f3769-3fc9-4e9f-b6c3-b3a93f894d36',
url: 'git@192.168.200.151:/home/git/repos/app.git']]])
}
stage('maver Build') {
echo "build ..."
}
stage('deploy') {
echo "success"
}
stage('test') {
echo "ok"
}
}
[root@jenkins itemA]# cd ..
[root@jenkins jenkinsfile]# git add *
[root@jenkins jenkinsfile]# git commit -m ""
[master (root-commit) 69f09a5]
file changed, insertions(+)
create mode itemA/jenkinsfile
[root@jenkins jenkinsfile]# git push -u origin master
Counting objects: , done.
Compressing objects: % (/), done.
Writing objects: % (/), bytes | bytes/s, done.
Total (delta ), reused (delta )
To git@192.168.200.151:/home/git/repos/jenkinsfile
* [new branch] master -> master
Branch master set up to track remote branch master from origin.
[root@jenkins jenkinsfile]#
修改web上的Jenkins的流水线的配置
实验自动流水线php实验
需要多备一台服务器php,搭建,
ngin-php服务器: IP地址 192.168.200.152 操作系统centos7.5
用yum安装Nginx,php,
[root@nginx-php ~]# yum -y install epel-release nginx php-fpm php-mysql git
修改Nginx的配置文件
[root@nginx-php ~]# cd /etc/nginx/
[root@nginx-php nginx]# egrep -v "#|^$" nginx.conf >> nginx.conf.bak
[root@nginx-php nginx]# mv nginx.conf /tmp/
[root@nginx-php nginx]# mv nginx.conf.bak nginx.conf
[root@nginx-php nginx]# vim nginx.conf
[root@nginx-php nginx]# cat nginx.conf
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
include /etc/nginx/conf.d/*.conf;
server {
listen 80;
server_name www.yunjisuan.com;
root /usr/share/nginx/htm/www.yunjisuan.com;
include /etc/nginx/default.d/*.conf;
location / {
index index.html index.php index.htm;
}
location ~\.php {
fastcgi_index index.php;
fastcgi_pass 127.0.0.1:9000;
include fastcgi.conf;
}
}
}
[root@nginx-php nginx]#
[root@nginx-php nginx]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
创建Nginx的首页目录
[root@nginx-php nginx]# cd /usr/share/nginx/html/
[root@nginx-php html]# ls
.html 50x.html index.html nginx-logo.png poweredby.png
[root@nginx-php html]# rm -rf *
[root@nginx-php html]# mkdir -p www.yunjisuan.com
[root@nginx-php html]# echo "192.168.200.152 www.yunjisuan.com" >> /etc/hosts
[root@nginx-php html]# id apache
uid=(apache) gid=(apache) groups=(apache)
[root@nginx-php html]# id nginx
uid=(nginx) gid=(nginx) groups=(nginx)
[root@nginx-php html]# cd ..
[root@nginx-php nginx]# chown -R apache html
[root@nginx-php nginx]#
启动Nginx,php
[root@nginx-php nginx]# systemctl start nginx
[root@nginx-php nginx]# systemctl start php-fpm
[root@nginx-php nginx]# ss -antup | grep
tcp LISTEN *: *:* users:(("nginx",pid=,fd=),("nginx",pid=,fd=))
tcp LISTEN ::: :::* users:(("nginx",pid=,fd=),("nginx",pid=,fd=))
[root@nginx-php nginx]# ss -antup | grep
tcp LISTEN 127.0.0.1: *:* users:(("php-fpm",pid=,fd=),("php-fpm",pid=,fd=),("php-fpm",pid=,fd=),("php-fpm",pid=,fd=),("php-fpm",pid=,fd=),("php-fpm",pid=,fd=))
[root@nginx-php nginx]#
安装jjdk,maven
[root@nginx-php ~]# tar xf jdk-8u171-linux-x64.tar.gz -C /usr/local/
[root@nginx-php ~]# cd /usr/local/
[root@nginx-php local]# mv jdk1..0_171/ jdk
[root@nginx-php local]# vim /etc/profile
[root@nginx-php local]# tail - /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@nginx-php local]# source /etc/profile
[root@nginx-php local]#ln -s /usr/local/jdk/bin/java /usr/bin/
[root@nginx-php ~]# tar xf apache-maven-3.5.-bin.tar.gz -C /usr/local/
[root@nginx-php ~]# cd /usr/local/
[root@nginx-php local]# mv apache-maven-3.5./ maven
[root@nginx-php local]# vim /etc/profile
[root@nginx-php local]# tail - /etc/profile
MAVEN_HOME=/usr/local/maven
export PATH=${MAVEN_HOME}/bin:$PATH
[root@nginx-php local]# source /etc/profile
在git服务器上建立php的远程仓库
[root@git ~]# su - git
Last login: Tue Dec :: CST on pts/
Last failed login: Tue Dec :: CST from 192.168.200.132 on ssh:notty
There were failed login attempts since the last successful login.
[git@git ~]$ cd repos/
[git@git repos]$ mkdir wordpress
[git@git repos]$ cd wordpress/
[git@git wordpress]$ git --bare init
Initialized empty Git repository in /home/git/repos/wordpress/
[git@git wordpress]$ ls
branches config description HEAD hooks info objects refs
[git@git wordpress]$
在Jenkins上准备上传代码到git仓库
[root@jenkins ~]# cd /test/
[root@jenkins test]# git clone git@192.168.200.151:/home/git/repos/wordpress
Cloning into 'wordpress'...
warning: You appear to have cloned an empty repository.
[root@jenkins test]# ls
app jenkinsfile wordpress
[root@jenkins test]# cd wordpress/
[root@jenkins wordpress]# ls
[root@jenkins wordpress]# cd ~
[root@jenkins ~]# ls
anaconda-ks.cfg apache-maven-3.5.-bin.tar.gz jdk-8u171-linux-x64.tar.gz test wordpress-4.9.-zh_CN.tar.gz
[root@jenkins ~]# tar xf wordpress-4.9.-zh_CN.tar.gz
[root@jenkins ~]# ls
anaconda-ks.cfg apache-maven-3.5.-bin.tar.gz jdk-8u171-linux-x64.tar.gz test wordpress wordpress-4.9.-zh_CN.tar.gz
[root@jenkins ~]# ls wordpress
index.php wp-activate.php wp-comments-post.php wp-cron.php wp-load.php wp-settings.php xmlrpc.php
license.txt wp-admin wp-config-sample.php wp-includes wp-login.php wp-signup.php
readme.html wp-blog-header.php wp-content wp-links-opml.php wp-mail.php wp-trackback.php
[root@jenkins ~]# mv wordpress/* /test/wordpress/
添加一个测试主页,推送代码到仓库
[root@jenkins ~]# cd /test/wordpress/
[root@jenkins wordpress]# vim status.html
[root@jenkins wordpress]# cat status.html
ok php2.
[root@jenkins wordpress]# ls
index.php readme.html wp-activate.php wp-blog-header.php wp-config-sample.php wp-cron.php wp-links-opml.php wp-login.php wp-settings.php wp-trackback.php
license.txt status.html wp-admin wp-comments-post.php wp-content wp-includes wp-load.php wp-mail.php wp-signup.php xmlrpc.php
[root@jenkins wordpress]# git add *
[root@jenkins wordpress]# git commit -m "wordpress"
[root@jenkins wordpress]# git push -u origin master
Counting objects: , done.
Compressing objects: % (/), done.
Writing objects: % (/), 8.86 MiB | 6.59 MiB/s, done.
Total (delta ), reused (delta )
To git@192.168.200.151:/home/git/repos/wordpress
* [new branch] master -> master
Branch master set up to track remote branch master from origin.
构建分布式节点
修改流水线项目的配置
编写流水线脚本
在Jenkins上修改流水线脚本,并推到git上
[root@jenkins ~]# cd /test/jenkinsfile/itemA/
[root@jenkins itemA]# cp jenkinsfile jenkinsfile-php
[root@jenkins itemA]# vim jenkinsfile-php
[root@jenkins itemA]# cat jenkinsfile-php
node ("PHP-slave-192.168.200.152") {
// def mvnHome
stage('git php checkout') {
checkout([$class: 'GitSCM', branches: [[name: '${branch}']],
doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [],
userRemoteConfigs: [[credentialsId: '3d9f3769-3fc9-4e9f-b6c3-b3a93f894d36',
url: 'git@192.168.200.151:/home/git/repos/wordpress']]])
}
stage('copy') {
sh '''rm -rf ${WORKSPACE}/.git
[ -d /data/backup ] || mkdir -p /data/backup
mv /usr/share/nginx/html/www.yunjisuan.com /data/backup/www.yunjisuan.com-$(date +%F_%T)
cp -rf ${WORKSPACE} /usr/share/nginx/html/www.yunjisuan.com'''
}
stage('test') {
sh 'curl http://www.yunjisuan.com/status.html'
} }
[root@jenkins itemA]#
[root@jenkins itemA]# git add *
[root@jenkins itemA]# git commit -m "php"
[master 20b2de7] php
1 file changed, 19 insertions(+)
create mode 100644 itemA/jenkinsfile-php
[root@jenkins itemA]# git push -u origin master
Counting objects: 6, done.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (4/4), 730 bytes | 0 bytes/s, done.
Total 4 (delta 0), reused 0 (delta 0)
To git@192.168.200.151:/home/git/repos/jenkinsfile
69f09a5..20b2de7 master -> master
Branch master set up to track remote branch master from origin.
[root@jenkins itemA]#
重新构建
多节点流水线,需要修改流水线脚本,几个节点,就将原来的节点复制几份,将节点修改了,拉代码是在同一台服务器拉所以不用改。(另外的个节点需要安装的工具,参考Nginx的。)配置脚本如下
[root@jenkins itemA]# cat jenkinsfile-php
node ("PHP-slave-192.168.200.152") {
// def mvnHome
stage('git php checkout') {
checkout([$class: 'GitSCM', branches: [[name: '${branch}']],
doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [],
userRemoteConfigs: [[credentialsId: '3d9f3769-3fc9-4e9f-b6c3-b3a93f894d36',
url: 'git@192.168.200.151:/home/git/repos/wordpress']]])
}
stage('copy') {
sh '''rm -rf ${WORKSPACE}/.git
[ -d /data/backup ] || mkdir -p /data/backup
mv /usr/share/nginx/html/www.yunjisuan.com /data/backup/www.yunjisuan.com-$(date +%F_%T)
cp -rf ${WORKSPACE} /usr/share/nginx/html/www.yunjisuan.com'''
}
stage('test') {
sh 'curl http://www.yunjisuan.com/status.html'
} }
node ("PHP-slave-192.168.200.153") {#第二个节点,就是节点名字改了其余的都是复制第一个节点的
// def mvnHome
stage('git php checkout') {
checkout([$class: 'GitSCM', branches: [[name: '${branch}']],
doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [],
userRemoteConfigs: [[credentialsId: '3d9f3769-3fc9-4e9f-b6c3-b3a93f894d36',
url: 'git@192.168.200.151:/home/git/repos/wordpress']]])
}
stage('copy') {
sh '''rm -rf ${WORKSPACE}/.git
[ -d /data/backup ] || mkdir -p /data/backup
mv /usr/share/nginx/html/www.yunjisuan.com /data/backup/www.yunjisuan.com-$(date +%F_%T)
cp -rf ${WORKSPACE} /usr/share/nginx/html/www.yunjisuan.com'''
}
stage('test') {
sh 'curl http://www.yunjisuan.com/status.html'
} }
node ("PHP-slave-192.168.200.154") { #第3给节点,就是节点名字改了,其余的都是复制第一个节点的
// def mvnHome
stage('git php checkout') {
checkout([$class: 'GitSCM', branches: [[name: '${branch}']],
doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [],
userRemoteConfigs: [[credentialsId: '3d9f3769-3fc9-4e9f-b6c3-b3a93f894d36',
url: 'git@192.168.200.151:/home/git/repos/wordpress']]])
}
stage('copy') {
sh '''rm -rf ${WORKSPACE}/.git
[ -d /data/backup ] || mkdir -p /data/backup
mv /usr/share/nginx/html/www.yunjisuan.com /data/backup/www.yunjisuan.com-$(date +%F_%T)
cp -rf ${WORKSPACE} /usr/share/nginx/html/www.yunjisuan.com'''
}
stage('test') {
sh 'curl http://www.yunjisuan.com/status.html'
} }
[root@jenkins itemA]#
直接构建就可以了
让节点通过ssh支持执行命令道web服务器的,流水线,需要装插件
即节点拉取代码到本地,节点将代码发送到web服务器,通过ssh,执行写在本地的叫本在web服务器上。
在Jenkins上修改流水线脚本
[root@jenkins itemA]# pwd
/test/jenkinsfile/itemA
[root@jenkins itemA]# ls
jenkinsfile jenkinsfile-php jenkinsfile-php.bak
[root@jenkins itemA]# cat jenkinsfile-php
node ("PHP-slave-192.168.200.152") {
def remote = [:]
remote.name = 'test'
remote.user = 'root'
remote.allowAnyHosts = true
stage('git php checkout') {
sh 'echo "`hostname -I`"'
checkout([$class: 'GitSCM', branches: [[name: '${branch}']],
doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [],
userRemoteConfigs: [[credentialsId: '3d9f3769-3fc9-4e9f-b6c3-b3a93f894d36',
url: 'git@192.168.200.151:/home/git/repos/wordpress']]])
}
// stage('copy') {
// sh '''rm -rf ${WORKSPACE}/.git
// [ -d /data/backup ] || mkdir -p /data/backup
// mv /usr/share/nginx/html/www.yunjisuan.com /data/backup/www.yunjisuan.com-$(date +%F_%T)
// cp -rf ${WORKSPACE} /usr/share/nginx/html/www.yunjisuan.com'''
//}
// stage('test') {
// sh 'curl http://www.yunjisuan.com/status.html'
// }
stage ('SSH 200.151') {
remote.host = '192.168.200.151'
remote.password = '123123'
writeFile file: 'ssh.sh', text:'echo "`hostname -I`"'
sshScript remote:remote, script: "ssh.sh"
}
}
[root@jenkins itemA]#
提交到代码仓库
[root@jenkins itemA]# cd ..
[root@jenkins jenkinsfile]# git add *
[root@jenkins jenkinsfile]# git commit -m ""
[master 986f3c8]
file changed, insertions(+), deletions(-)
[root@jenkins jenkinsfile]# git push -u origin master
Counting objects: , done.
Compressing objects: % (/), done.
Writing objects: % (/), bytes | bytes/s, done.
Total (delta ), reused (delta )
To git@192.168.200.151:/home/git/repos/jenkinsfile
646a34e..986f3c8 master -> master
Branch master set up to track remote branch master from origin.
jenkins的流水线pipeline+项目实验php的更多相关文章
- 试用Jenkins 2 的 Pipeline 项目
目前Jenkins最新的版本是2.7,现在试用一下pipeline类型的项目,本来想构建一个1.651版本的Jenkins为例,无奈大陆的网络 访问github不稳定,只好改为简单的工程. 目前有一个 ...
- 9.Jenkins进阶之流水线pipeline基础使用实践(2)
目录一览: 0x01 基础实践 0x02 进阶实践 (1) Sonarqube 代码质量检测之 Pipeline Script from SCM (2) Gitlab 自动触发构建之 Pipeline ...
- 在Jenkins的pipeline项目中运行jmeter测试-教程
Jenkins 2.0的发布引入了一种新的项目类型 - Pipeline,以前只能通过插件获得.从Jenkins 2.0开始,Pipeline项目开箱即用. 与通常的“自由式”项目相比,管道构建具有几 ...
- 8.Jenkins进阶之流水线pipeline基础使用实践(1)
目录一览: 0x01 基础实践 (1) Maven 构建之 Pipeline Script (2) Maven 构建之 Pipeline Script from SCM (3) Jenkins pi ...
- 7.Jenkins进阶之流水线pipeline语法入门学习(2)
目录一览: (2) Declarative Pipeline Syntax 2.1) Sections - 章节 2.2) Directives - 指令 2.3) Sequential Stages ...
- 6.Jenkins进阶之流水线pipeline语法入门学习(1)
目录一览: 0x00 前言简述 Pipeline 介绍 Pipeline 基础知识 Pipeline 扩展共享库 BlueOcean 介绍 0x01 Pipeline Syntax (0) Groov ...
- Jenkins流水线(pipeline)实战之:从部署到体验
关于Jenkins流水线(pipeline) Jenkins 流水线 (pipeline) 是一套插件,让Jenkins可以实现持续交付管道的落地和实施. 关于blueocean Blue Ocean ...
- OpenShift应用镜像构建(3) - Jenkins的流水线构建
Jenkins方式构建的定位是使用专门的CICD平台. 既支持把JenKins作为一个Pod部署到openshift内部,也支持部署在Openshift集群外部,操作上的区别是 openshift自己 ...
- Jenkins - 构建流水线
1 - 以流水线的方式进行构建 关联多任务形成流水线的两种方法 通过定义项目的后续项目,将项目直接关联起来按顺序执行, 另外定义一个用于统筹管理的项目,定义各项目之间的关联性,然后以流水线的方式执行 ...
随机推荐
- gvim写html代码时如何快速地跳转到一个标签的结束位置: 终极插件: matchit.vim
gvim写html代码时如何快速地跳转到一个标签的结束位置 参考这个vimrc的配置, 里面有一些 很好的东西, 配置很有用, 以前没有用到: http://www.cnblogs.com/wangj ...
- swift设计模式学习 - 原型模式
移动端访问不佳,请访问我的个人博客 设计模式学习的demo地址,欢迎大家学习交流 原型模式 用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象. 定义 用原型实例指定创建对象的种类,并且通 ...
- 【第一章】 第一个spring boot程序
环境: jdk:1.8.0_73 maven:3.3.9 spring-boot:1.2.5.RELEASE(在pom.xml中指定了) 注意:关于spring-boot的支持, 最少使用jdk7(j ...
- [luogu 1070]道路游戏(NOIP2009T4)
题目链接 题解: 题目描述好长啊.... 大概就是设一下$f[i]$表示第i秒的最大价值 首先枚举时间,然后因为机器人这一秒无论在哪里都是有可能的,所以要枚举一下每个点,又因为最多走p秒所以再枚举一下 ...
- Python CSV Reader/Writer 例子--转载
CSV(comma-separated values) 是跨多种形式导入导出数据的标准格式,比如 MySQL.Excel. 它以纯文本存储数和文本.文件的每一行就代表一条数据,每条记录包含了由逗号分隔 ...
- pyqt 实现的俄罗斯方块
from PyQt5.QtWidgets import QMainWindow, QFrame, QDesktopWidget, QApplication from PyQt5.QtCore impo ...
- XML_CPP_资料_libXml2_01_Code
ZC: 这里的代码,就是 http://www.cnblogs.com/cppskill/p/6207609.html(我的文章"XML_CPP_资料_libXml2_01 - CppSki ...
- AngularJs filter 过滤器基础【转】
Filter Ng里的过滤器. currency:把一个数字格式化成货币模式(如$1,234.56).当没有提供任何货币符号时,默认使用当前区域的符号. 使用: HTML:{{ currency_ex ...
- URAL 1303 Minimal Coverage
URAL 1303 思路: dp+贪心,然后记录路径 mx[i]表示从i开始最大可以到的位置 sufmx[i]表从1-i的某个位置开始最大可以到达的位置 比普通的贪心效率要高很多 代码: #inclu ...
- Codeforces 838A - Binary Blocks(二维前缀和+容斥)
838A - Binary Blocks 思路:求一下前缀和,然后就能很快算出每一小正方块中1的个数了,0的个数等于k*k减去1的个数,两个的最小值就是要加进答案的值. 代码: #include< ...