声明:实验环境使用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 23 13:49:25 CST 2018 from 192.168.200.132 on pts/1 Last failed login: Tue Dec 25 14:03:45 CST 2018 from 192.168.200.132 on ssh:notty There were 2 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 "111" [master (root-commit) 69f09a5] 111 1 file changed, 20 insertions(+) create mode 100644 itemA/jenkinsfile [root@jenkins jenkinsfile]# git push -u origin master Counting objects: 4, done. Compressing objects: 100% (2/2), done. Writing objects: 100% (4/4), 551 bytes | 0 bytes/s, done. Total 4 (delta 0), reused 0 (delta 0) 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 404.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=48(apache) gid=48(apache) groups=48(apache) [root@nginx-php html]# id nginx uid=998(nginx) gid=996(nginx) groups=996(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 80 tcp LISTEN 0 128 *:80 *:* users:(("nginx",pid=6435,fd=6),("nginx",pid=6434,fd=6)) tcp LISTEN 0 128 :::80 :::* users:(("nginx",pid=6435,fd=7),("nginx",pid=6434,fd=7)) [root@nginx-php nginx]# ss -antup | grep 9000 tcp LISTEN 0 128 127.0.0.1:9000 *:* users:(("php-fpm",pid=6448,fd=0),("php-fpm",pid=6447,fd=0),("php-fpm",pid=6446,fd=0),("php-fpm",pid=6445,fd=0),("php-fpm",pid=6444,fd=0),("php-fpm",pid=6442,fd=6)) [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.8.0_171/ jdk [root@nginx-php local]# vim /etc/profile [root@nginx-php local]# 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@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.0-bin.tar.gz -C /usr/local/ [root@nginx-php ~]# cd /usr/local/ [root@nginx-php local]# mv apache-maven-3.5.0/ maven [root@nginx-php local]# vim /etc/profile [root@nginx-php local]# tail -2 /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 25 14:47:07 CST 2018 on pts/1 Last failed login: Tue Dec 25 14:57:42 CST 2018 from 192.168.200.132 on ssh:notty There were 2 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.0-bin.tar.gz jdk-8u171-linux-x64.tar.gz test wordpress-4.9.4-zh_CN.tar.gz [root@jenkins ~]# tar xf wordpress-4.9.4-zh_CN.tar.gz [root@jenkins ~]# ls anaconda-ks.cfg apache-maven-3.5.0-bin.tar.gz jdk-8u171-linux-x64.tar.gz test wordpress wordpress-4.9.4-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.0 [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: 1661, done. Compressing objects: 100% (1635/1635), done. Writing objects: 100% (1661/1661), 8.86 MiB | 6.59 MiB/s, done. Total 1661 (delta 173), reused 0 (delta 0) 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 "123123" [master 986f3c8] 123123 1 file changed, 2 insertions(+), 2 deletions(-) [root@jenkins jenkinsfile]# git push -u origin master Counting objects: 7, done. Compressing objects: 100% (3/3), done. Writing objects: 100% (4/4), 436 bytes | 0 bytes/s, done. Total 4 (delta 1), reused 0 (delta 0) 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.