企业级自动化代码发布——jenkins部署与项目示例

本文详细介绍了如何在Jenkins上进行自动化部署,包括安装配置流程、插件安装、SSH配置及实战PHP程序自动发布过程。涵盖了Jenkins的安装、依赖软件配置、插件管理、系统环境变量设置以及常见故障排查等内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

jenkins安装部署

war包安装

#安装依赖软件
[root@jenkins ~]# yum install -y java-1.8.0-openjdk && mkdir -p /usr/local/jenkins
#启动jenkins
[root@jenkins ~]# java -jar /usr/local/jenkins/jenkins.war &
This may also be found at: /root/.jenkins/secrets/initialAdminPassword
#修改为国内源
[root@jenkins ~]# vim .jenkins/hudson.model.UpdateCenter.xml
<url>https://mirrors.tuna.tsinghua.edu.cn/jenkins/updates/update-center.json</url>
#kill %1,重启
#修改插件下载地址
[root@jenkins .jenkins]# sed -i 's/https:\/\/siteproxy.ruqli.workers.dev:443\/http\/updates.jenkins-ci.org\/download/https:\/\/siteproxy.ruqli.workers.dev:443\/https\/mirrors.tuna.tsinghua.edu.cn\/jenkins/g' updates/default.json
[root@jenkins .jenkins]# sed -i 's/https:\/\/siteproxy.ruqli.workers.dev:443\/http\/www.google.com/https:\/\/siteproxy.ruqli.workers.dev:443\/https\/www.baidu.com/g' updates/default.json
[root@jenkins .jenkins]# grep -o "tuna.tsinghua" updates/default.json
#kill %1,重启
[root@jenkins ~]# cat /root/.jenkins/secrets/initialAdminPassword
0b9b294831aa4646b0c7ba7d4b5d853c

访问http://192.168.213.188:8080/
安装插件,创建管理员用户
在这里插入图片描述

配置git

#卸载低版本
yum remove git
[root@jenkins ~]# yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel gcc-c++ perl-ExtUtils-CBuilder perl-Extuntils-MakeMaker wget autoconf -y 
[root@jenkins git-2.7.3]# make configure
[root@jenkins git-2.7.3]# ./configure --prefix=/usr/local/git
[root@jenkins git-2.7.3]# make profix=/usr/local/git
[root@jenkins git-2.7.3]# make install
[root@jenkins ~]# echo "export PATH=$PATH:/usr/local/git/bin" > /etc/profile.d/git.sh
[root@jenkins ~]# source /etc/profile.d/git.sh
[root@jenkins ~]# git --version
git version 2.7.3

版本必须是2.0以上
在这里插入图片描述

配置maven

[root@jenkins ~]# tar xf apache-maven-3.5.4-bin.tar.gz -C /usr/local/
[root@jenkins ~]# echo "export PATH=$PATH:/usr/local/apache-maven-3.5.4/bin" > /etc/profile.d/maven.sh
[root@jenkins ~]# source /etc/profile.d/maven.sh
[root@jenkins ~]# mvn --version
Apache Maven 3.5.4

在这里插入图片描述

配置jdk

[root@jenkins ~]# rpm -e --nodeps java-1.8.0-openjdk-headless-1.8.0.252.b09-2.el7_8.x86_64
[root@jenkins ~]# ln -sv /usr/local/jdk1.8.0_161 /usr/local/jdk1.8
[root@jenkins ~]# echo "export PATH=$PATH:/usr/local/jdk1.8/bin" > /etc/profile.d/java.sh
[root@jenkins ~]# source /etc/profile.d/java.sh
[root@jenkins ~]# java -version
java version "1.8.0_161"

在这里插入图片描述

配置svn

[root@jenkins ~]# yum install subversion -y
[root@jenkins ~]# mkdir /svnrepo
[root@jenkins ~]# svnadmin create /svnrepo/project1
[root@jenkins ~]# cd /svnrepo/project1/conf/
[root@jenkins conf]# egrep -v "^#|^$" authz
[aliases]
[groups]
[/]
xuel = rw
[root@jenkins conf]# egrep -v "^#|^$" passwd
[users]
xuel = xuel@qnchnet.com
[root@jenkins conf]# egrep -v "^#|^$" svnserve.conf
[general]
anon-access = read
auth-access = write
password-db = passwd
authz-db = authz
[sasl]
# 启动项目
[root@jenkins ~]# svnserve -d -r /svnrepo/project1/

在这里插入图片描述

配置系统环境变量

在这里插入图片描述

密码破解

https://blog.csdn.net/h106140873/article/details/95480258

实战PHP程序自动发布

搭建lnmp环境

[root@lnmp ~]# yum install http php php-mysql
#https://blog.csdn.net/qq_39719589/article/details/81835330

插件安装

Publish Over SSH
Git plugin

SSH配置

系统管理一系统设置一Publish over SSH

[root@jenkins ~]# ssh-keygen -t rsa
[root@jenkins ~]# ssh-copy-id -i .ssh/id_rsa.pub git@192.168.213.136

在这里插入图片描述

SSH Server配置

系统管理一系统设置一Publish over SSH一SSH Server

表项含义
name需要将php程序发布到目标服务器的名称,可自定义
Hostname目标服务器的IP地址
Username发布用户(密钥互信的用户)
Remote Directory发布到目标服务器的相对根路径,建议填写/,防止后续填写路径异常

在这里插入图片描述

构建项目

新建Item---->填入项目名称---->选择构建一个自由风格的软件项目----> 确定

源码管理 git
Repository URL填写具体git上的仓库url https://github.com/baijunyao/thinkphp-bjyblog.git,如果为私有,需要添加Credentials
在这里插入图片描述构建环境 Send files or execute commands over SSH after the build runs
构建后操作:生成运行后通过SSH发送文件或执行命令

表项内容含义
Source files**/*此路径以Jenkins的工作目录为基准,不支持全路径
Remote directory/var/www/html此路径以SSH Server路径为基准,不支持绝对路径
Exec commandchown -R apache:apache /var/www/html-

在这里插入图片描述
构建
保存并构建,可以点击console output查看日志
在这里插入图片描述
此时在lnmp主机上查看,程序文件已经成功发布到目标服务器上
在这里插入图片描述
在浏览器上访问,并安装程序: http://192.168.213.136/Public/install/index.php
在这里插入图片描述

故障排查

error: RPCfaied; curl 18 transfer closed with outstanding read data remaining

(1)curl的postBuffer的默认值太小

[root@jenkins ~]# vim .jenkins/.gitconfig	#增加
[http]
    postBuffer = 524288000

(2)调整构建时间

java -Dorg.jenkinsci.plugins.gitclient.Git.timeOut=60 -jar jenkins.war

jenkins自动从github下载代码编译,时间不能超过10分钟

jenkins对接私有仓库

配置git

(1)在jenkins服务器上生成秘钥对

[root@jenkins ~]# ssh-keygen -t rsa -C 1284915257@qq.com

(2)将公钥放置在git仓库的秘钥库中并ssh连接测试

[root@jenkins ~]# ssh-copy-id -i .ssh/id_rsa root@192.168.213.122

在这里插入图片描述

配置Jenkins

jenkins使用私有仓库私有仓库需要添加Credentials

配置一个Credentials

在jenkins界面,依次点击:凭据—系统—添加域—添加凭证
类型	SSH Username with private keyPrivate Key为jenkins主机的私钥【请确保公钥已用Username传到git主机上】

安装Maven插件

Maven Integration Plugin 或者 Pipeline Maven Integration Plugin

构建任务

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

验证结果

在这里插入图片描述
在这里插入图片描述在这里插入图片描述在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值