编程语言
首页 > 编程语言> > Jenkins + pipeline + Git + PHP (九)

Jenkins + pipeline + Git + PHP (九)

作者:互联网

一、准备环境介绍

192.168.5.71    # gitlab 仓库IP
192.168.5.72    # 开发环境,用于提交代码等
192.168.5.150   # www.leon.com 运行wordpress的网站地址
192.168.5.239   # jenkins 服务器

二、Jenkins需要先安装pipeline插件

Jenkins -> 系统管理 -> 插件管理 -> 可选插件 -> 过滤pipeline -> 钩选后直接安装

 三、创建两个git仓库,用于存放jenkinsfile脚本及php线上代码

在192.168.5.71上面使用git用户,也可以用root用户。

# 初始化一个仓库用于存储jenkins pipeline 的SCM脚本,jenkinsfile名称可修改成别的名字
# su - git
$ mkdir -p /home/git/repo/jenkinsfile/
$ cd repo/jenkinsfile/
$ git --bare init
Initialized empty Git repository in /home/git/repo/jenkinsfile/
-----------------------------------------------------------------------------
# 初始化之后的仓库地址是:git@192.168.5.71:/home/git/repo/jenkinsfile

# 初始化第二个git仓库,用于上线wordpress代码
$ mkdir -p /home/git/repo/wordpress
$ cd /home/git/repo/wordpress/
$ git --bare init
Initialized empty Git repository in /home/git/repo/wordpress/
------------------------------------------------------------------------------
# 初始化之后的仓库地址是:git@192.168.5.71:/home/git/repo/wordpress

四、开发机(72)和LNMP环境的web机器(150)配置对git服务器(71)免密钥认证

开发机和web机在两台机器上面都要进行操作
LNMP环境安装可参见:https://www.cnblogs.com/cyleon/p/10110060.html
# 在两台(72、150)机器上面做如下操作
# ssh-keygen -t rsa -P '' -f ~/.ssh/id_dsa # ssh-copy-id -i /root/.ssh/id_dsa.pub git@192.168.5.71 # 完成后测试是否能免密码拉取代码 # git clone git@192.168.5.71:/home/git/repo/jenkinsfile Cloning into 'jenkinsfile'... remote: Counting objects: 12, done. remote: Compressing objects: 100% (7/7), done. remote: Total 12 (delta 1), reused 0 (delta 0) Receiving objects: 100% (12/12), done. Resolving deltas: 100% (1/1), done.

五、在开发机(72)下载wordpress代码,并push到wordpress仓库中

# LNMP环境版本:nginx/1.8.1、PHP 5.5.30、MariaDB-5.5.64、wordpress-4.7.4
# tar xf wordpress-4.7.4-zh_CN.tar.gz 
# cd wordpress/
# cp wp-config-sample.php wp-config.php
# echo "OK" > status.html # git init # git remote add origin git@192.168.5.71:/home/git/repo/wordpress # git add . # git commit -m 'wordpress all files' # git push origin master

六、编写jenkinsfile脚本文件

# git clone git@192.168.5.71:/home/git/repo/jenkinsfile
# cd jenkinsfile/
# mkdir team-a/
# cd team-a/
# cat jenkinsfile-php 
node ("wordpress-192.168.5.150") {  # 指定jenkins的slave节点,也是WEB服务器
   stage('git checkout') {
        checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'ccf5f140-9a28-49d6-abc8-40bc6c750323', url: 'git@192.168.5.71:/home/git/repo/wordpress']]])
   }
   stage('copy code') {
        sh ''' rm -rf ${WORKSPACE}/.git
        mv /data/html/www.leon.com /data/backup/www.leon.com-$(date +%F_%T)
        cp -rf ${WORKSPACE} /data/html/www.leon.com '''
   }
   stage('check web test'){
        sh "curl http://www.leon.com/status.html"   # 需要在150本地配置/etc/hosts解析
   }
}

七、nginx域名配置及hosts文件修改

# mkdir -p /data/html/www.leon.com
# mkdir /data/backup
# cat /etc/nginx/conf.d/www.leon.com.conf 
server {
        listen       80;
        server_name  www.leon.com;
        access_log   /var/log/nginx/access.log  main;
        location / {
                root   /data/html/www.leon.com;
                index  index.html index.htm index.php;
                try_files $uri $uri/ /index.php$is_args$args;
        }

        location ~ \.php {
                root  /data/html/www.leon.com; 
                fastcgi_pass   unix:/tmp/php-cgi.sock;
                fastcgi_index  index.php;
                fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
                fastcgi_param  PATH_INFO $fastcgi_script_name;
                include        fastcgi_params;
        }
}
# nginx -s reload
# echo '192.168.5.150 www.leon.com' >> /etc/hosts

八、配置jenkins pipeline

8.1 创建pipeline的任务并进行配置

 8.2 进行构建

 

 默认master分支,无需修改

 

 

 可以点击查看部署日志

 九、pipeline语法

可以自行生成checkout流水线等语法

标签:pipeline,Git,www,192.168,repo,git,wordpress,Jenkins,jenkinsfile
来源: https://www.cnblogs.com/cyleon/p/11913088.html