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 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,php

[root@nginx-php ~]# yum -y install epel-release nginx php-fpm php-mysql git

修改Nginx的配置文件html

[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 successfuljava

 

建立Nginx的首頁目錄node

[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,phpmysql

[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,mavenlinux

[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

 

從新構建git

 

 多節點流水線,須要修改流水線腳本,幾個節點,就將原來的節點複製幾份,將節點修改了,拉代碼是在同一臺服務器拉因此不用改。(另外的個節點須要安裝的工具,參考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.

 

相關文章
相關標籤/搜索