Jenkins 自动化部署PHP项目

·  阅读 1100

安装 Jenkins

sudo wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat-stable/jenkins.repo

sudo rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io.key

sudo yum upgrade

sudo yum install epel-release

sudo yum install java-11-openjdk-devel

sudo yum install jenkins

sudo systemctl daemon-reload

sudo systemctl status jenkins
复制代码

修改Jenkins默认端口和用户

# 如用户名修改为:root; 端口修改为:8989 
vim /etc/sysconfig/jenkins
复制代码

image.png

启动Jenkins

sudo systemctl start jenkins
复制代码

查看Jenkins状态

sudo systemctl status jenkins
复制代码

image.png

更新防火墙

sudo firewall-cmd --zone=public --permanent --add-port=8989/tcp
sudo firewall-cmd --reload
复制代码

访问Jenkins

浏览器输入:http://192.168.2.240:8989

image.png

安装推荐插件

image.png

image.png

创建第一个管理员用户

image.png

开始使用Jenkins

image.png

安装插件

  1. GitLab

image.png

  1. Publish Over SSH

image.png

前提准备

配置证书(用于从GitLab中拉取代码)

Dashboard -> Manage Credentials -> Jenkins(global) -> 全局凭据 -> Add Credentials

image.png

在 Jenkins 所在的发布机安装 Git

yum install git
复制代码

在 Jenkins 所在的发布机安装php7

sudo yum install epel-release
sudo yum install yum-utils
sudo yum install http://rpms.remirepo.net/enterprise/remi-release-7.rpm
sudo yum-config-manager --enable remi-php74
sudo yum install php php-common php-opcache php-mcrypt php-cli php-gd php-curl php-mysqlnd

# ext 
yum install php-xml
yum install php-intl
yum install php-mbstring
yum install php-pecl-zip

# 重启php
systemctl restart httpd
复制代码

在 Jenkins 所在的发布机安装 node

curl -sL https://rpm.nodesource.com/setup_10.x | sudo bash -

sudo yum install nodejs

node --version

npm --version
复制代码

在 Jenkins 所在的发布机安装 composer

# 下载 composer 安装程序脚本
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"

# 验证下载的完整性,签名匹配会输出 Installer verified
HASH="$(wget -q -O - https://composer.github.io/installer.sig)"
php -r "if (hash_file('SHA384', 'composer-setup.php') === '$HASH') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"

# 安装composer
sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer
复制代码

配置 SSH 服务器

  1. 首先需要将发布机的公钥传输到生产机。

将发布机上的 /root/.ssh/id_rsa.pub 传送到 生产机的 .ssh/(目录没有.ssh请创建)

将 id_rsa.pub 改名为 authorized_keys

测试无密码链接 ssh 生产机IP

# 发布机生成rsa密钥
# ssh-keygen -t rsa -b 2048 这个是高版本的密钥,Jenkins不支持
ssh-keygen -m PEM -t rsa -b 4096

# 传送到生产机
scp /root/.ssh/id_rsa.pub root@192.168.2.134:/root/.ssh/authorized_keys
复制代码
  1. 配置 SSH Servers

Dashboard -> Manage Jenkins -> Configure System

在页面找到Publish over SSH栏,配置 SSH Servers

image.png

记得勾选 Use password authentication,or use a different key

image.png

  1. 测试配置是否成功

image.png

创建 Jenkins 项目配置

创建一个 Pipeline 风格的项目

image.png

参数化构建

Dashboard -> oa-pipeline -> Configure -> General 记得选择 String Parameter

image.png

pull code

image.png

pipeline {
    agent any

    stages {
        stage('pull code') {
            steps {
                checkout([$class: 'GitSCM', 
                branches: [[name: '*/${branch}']], 
                extensions: [], 
                userRemoteConfigs: [[credentialsId: 'fe3b2a84-557c-431d-84a5-e4d93e9e9717', url: ${git仓库地址}]]])
            }
        }
    }
}
复制代码

如果是从tag拉取代码,需要这么配置

pipeline {
    agent any

    stages {
        stage('pull code') {
            steps {
                checkout([$class: 'GitSCM', 
                branches: [[name: 'refs/tags/${tag}']],
                extensions: [], 
                userRemoteConfigs: [[credentialsId: 'fe3b2a84-557c-431d-84a5-e4d93e9e9717', url: ${git仓库地址}]]])
            }
        }
    }
}
复制代码

build project

image.png

pipeline {
    agent any

    stages {
        stage('pull code') {
            steps {
                checkout([$class: 'GitSCM', 
                branches: [[name: 'refs/tags/${tag}']], 
                extensions: [], 
                userRemoteConfigs: [[credentialsId: 'fe3b2a84-557c-431d-84a5-e4d93e9e9717', url: ${git仓库地址}]]])
            }
        }
        stage('build project') {
            steps {
                sh '/usr/local/oa/oa_build.sh'
            }
        }
        
    }
}
复制代码

脚本内容如下:

#!/bin/bash
cd /var/lib/jenkins/workspace/php-oa && /usr/local/bin/composer install
cd /var/lib/jenkins/workspace/php-oa/slimvue-oa && npm install
cd /var/lib/jenkins/workspace/php-oa/slimvue-oa && npm run publish
复制代码

保存退出。并修改文件权限为可执行:

chmod 764 /usr/local/oa/oa_build.sh
复制代码

post build

image.png

在目标主机中编写脚本

#!/bin/bash
cp /data/projects/oa_new/resource/config.yml /data/projects/oa_new/oa/config/config.yml
rm -rf /data/projects/oa_new/oa/cache/*
复制代码

保存退出。并修改文件权限为可执行:

chmod 764 deploy.sh
复制代码

最终Pipeline配置如下:

pipeline {
    agent any

    stages {
        stage('pull code') {
            steps {
                checkout([$class: 'GitSCM', 
                branches: [[name: 'refs/tags/${tag}']], 
                extensions: [], 
                userRemoteConfigs: [[credentialsId: 'fe3b2a84-557c-431d-84a5-e4d93e9e9717', url: ${git仓库地址}]]])
            }
        }
        stage('build project') {
            steps {
                sh '/usr/local/oa/oa_build.sh'
            }
        }
        stage('post build'){
            steps{
                sshPublisher(publishers: [sshPublisherDesc(configName: '98', transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: '''cd /data/projects/oa_new
sh deploy.sh''', execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: '/data/projects/oa_new/oa', remoteDirectorySDF: false, removePrefix: '', sourceFiles: '**/*')], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)])
            }
        }
    }
}
复制代码

开始部署

Build with Parameters

image.png

点击 Build,查看日志可以看到部署已经成功。

image.png

因为前端项目比较古老了,一直没优化,可以看到构建还是比较慢的。

image.png

分类:
开发工具
标签:
分类:
开发工具
标签:
收藏成功!
已添加到「」, 点击更改