安装 jenkins
IP地址 | 操作系统 | 服务版本 |
---|---|---|
192.168.75.137 | Rocky9.2 | jenkins = 2.450-1.1 jdk = 11.0.22 git = 2.39.3 |
192.168.75.138 | Rocky9.2 | gitlab-ce = 16.10.0 |
结合上文
jenkins安装
前期准备:
yum install -y epel-release
yum -y install net-tools vim lrzsz wget git yum-utils telnet unzip tar rsync
systemctl disable --now firewalld
sed -i 's/^SELINUX=enforcing$/SELINUX=disabled/' /etc/selinux/config
setenforce 0
准备开始安装
bash
wget http://pkg.jenkins.io/redhat/jenkins.repo
mv jenkins.repo /etc/yum.repos.d/
yum -y install java
rpm --import http://pkg.jenkins.io/redhat/jenkins.io.key
rpm --import https://pkg.jenkins.io/redhat/jenkins.io.key
yum install -y jenkins --nogpgcheck
报错 :
[MIRROR] jenkins-2.451-1.1.noarch.rpm: Curl error (60): SSL peer certificate or SSH remote key was not OK for https://mirrors.jenkins.io/redhat/jenkins-2.451-1.1.noarch.rpm [SSL certificate problem: certificate is not yet valid]
[FAILED] jenkins-2.451-1.1.noarch.rpm: No more mirrors to try - All mirrors were already tried without success
The downloaded packages were saved in cache until the next successful transaction.
You can remove cached packages by executing 'yum clean packages'.
Error: Error downloading packages:
jenkins-2.451-1.1.noarch: Cannot download, all mirrors were already tried without success
问题原因:
请确保您主机的系统时间设置正确,包括日期、时间和时区。SSL 证书的有效性与当前系统时间密切相关。
如果 系统时间不正确,可能会导致证书无法验证。
处理:
date
修改系统时间即可
date -s 2024/3/28
date -s 17:38:59
报错:
Public key for jenkins-2.451-1.1.noarch.rpm is not installed
The downloaded packages were saved in cache until the next successful transaction.
You can remove cached packages by executing 'yum clean packages'.
Error: GPG check FAILED
处理:
vim /etc/yum.repo/jenkins.repo
[jenkins]
name=Jenkins
baseurl=http://pkg.jenkins.io/redhat
enabled=1
gpgcheck=0
继续安装
bash
yum install -y jenkins
修改配置文件
配置文件位置 不一定在哪 ,目前作者的配置文件是在 /usr/下,有的小伙伴是在/etc/下
/etc/sysconfig/jenkins
/usr/lib/systemd/system/jenkins.service
vim /usr/lib/systemd/system/jenkins.service
User=jenkins
Group=jenkins
# 在该配置文件中,找到 [Service] 部分,然后添加或修改 StandardOutput 和 StandardError 选项,
# 指定日志的输出路径。配置如下:
StandardOutput=file:/var/log/jenkins/jenkins.log
StandardError=file:/var/log/jenkins/jenkins.log
# 配置文件中有端口,如果8080端口冲突可以自定义修改,默认8080
Environment="JENKINS_PORT=8080"
# 创建 log 日志目录
mkdir /var/log/jenkins/
systemctl daemon-reload
systemctl restart jenkins
systemctl enable jenkins
安装 git 客户端
bash
yum install -y git
git --version
whereis git
安装mvn
bash
wget https://dlcdn.apache.org/maven/maven-3/3.9.6/binaries/apache-maven-3.9.6-bin.tar.gz
tar -xf apache-maven-3.9.6-bin.tar.gz
mv apache-maven-3.9.6 /usr/local/maven
vim /etc/profile
export MAVEN_HOME=/usr/local/maven/
export PATH=$PATH:${JAVA_PATH}:${MAVEN_HOME}/bin:
source /etc/profile
mvn --version
访问地址:
http://192.168.75.137:8080
密码:more /var/lib/jenkins/secrets/initialAdminPassword
如果懒得自定义的话,直接安装推荐的插件就够用。
等待安装完成即可。
创建一个管理员账户
bash
bash
如果汉化不完全,一半中文一半英文,重启Jenkins就好了
创建任务
点击 新建item
关联上方的 gitlab项目名称(也可以自定义名称)
bash
bash
bash
bash
bash
报错处理:
创建SSH连接方式
点击添加,添加gitlab的私钥
bash
选择完成之后报错解决。
创建HTTP连接方式
go
- 以上两种连接方式(ssh、http)选择其中一种即可
编辑要拉取执行shell
bash
#!/bin/bash
#action="$1"
case $action in
'deploy')
# 目标目录路径
TARGET_DIR="/data/www/diting/sources/admin/${BUILD_NUMBER}"
echo "将当前jenkins项目的工作目录(git库的PHP代码)复制到指定目录"
rsync -av --exclude='.git' ${WORKSPACE}/* $TARGET_DIR
echo "创建vendor目录软连接"
ln -s /data/www/diting/test-admin.vendor $TARGET_DIR/vendor
echo "执行composer更新"
cd $TARGET_DIR
/usr/local/bin/composer update
# echo "修改目录所有者"
# chown -R gitlab-runner:gitlab-runner $TARGET_DIR
echo "删除旧软连接"
unlink /data/www/diting/test-admin
echo "创建源码软连接"
ln -s $TARGET_DIR /data/www/diting/test-admin
echo "部署完成"
;;
#
'rollback')
echo "回滚操作"
version="$2"
echo "回滚版本 #$version"
# 目标目录路径
TARGET_DIR="/data/www/diting/sources/admin/$version"
echo "源码目录 $TARGET_DIR"
echo "检查源码目录"
if [ ! -d $TARGET_DIR ]; then
echo "源码目录不存在了"
exit 1
fi
echo "删除旧软连接"
unlink /data/www/diting/test-admin
echo "创建源码软连接"
ln -s $TARGET_DIR /data/www/diting/test-admin
echo "回滚完成"
;;
#
#*)
# echo "无效的选项,请输入 'deploy' 或 'rollback'"
# exit 1
#;;
esac
可以把脚本写到一个指定位置
然后再执行
也可以直接把脚本写到执行shell中。
创建目录:
mkdir -p /data/www/diting/
mkdir -p /data/www/diting/sources/admin/
mkdir -p /data/www/diting/test-admin.vendor
chmod 755 /data/www/jenk-deploy.sh
chown -R jenkins.jenkins /data/www/jenk-deploy.sh
chown -R jenkins.jenkins /data/www/diting/
### 这一步可忽略 。。。。。。
安装 composer
yum -y install php
php -v
cd /usr/local/bin/
curl -sS https://getcomposer.org/installer | php
mv composer.phar composer
chmod a+x /usr/local/bin/composer
[root@localhost ~]# composer
______
/ ____/___ ____ ___ ____ ____ ________ _____
/ / / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/
/ /___/ /_/ / / / / / / /_/ / /_/ (__ ) __/ /
\____/\____/_/ /_/ /_/ .___/\____/____/\___/_/
/_/
Composer version 2.7.2
# 因下载好的composer默认的下载地址是国外的,在下载的过程中网络不好或可能被墙,将下载的地址换成国内的地址即可实现加速。
# 即将配置信息添加到 Composer 的全局配置文件 config.json 中
composer config -g repo.packagist composer https://packagist.phpcomposer.com
进入到项目目录
cd /data/www/diting/test-admin
/usr/local/bin/composer require
查看是否有报错
构建
点击保存,然后构建,点击 Build with Parameters
需求:将gitlab中更新代码,分发到两台web服务器中
一台是本机,一台是远端服务器
通过SSH发送文件或执行命令到远程服务器上
安装插件
- 点击系统管理 --->插件管理---> Available plugins ---> 搜索: publish over ssh --> 点击安装(install)
添加一台远程服务器
- 点击系统管理 ---》系统配置 ---》在最下方找到 Publish over SSH 设置 (如图所示)
修改配置
- 点击项目 ---》配置 ---》点击 构建后操作 ---》选择 Send build artifacts over SSH
go
go
go
添加脚本
- 在 Exec command 中 添加脚本,脚本如下
bash
#!/bin/bash
#action="$1"
case $action in
'deploy')
# 目标目录路径
TARGET_DIR_copy="/data/www/diting/sources/admin/"
TARGET_DIR="/data/www/diting/sources/admin/${BUILD_NUMBER}"
echo "将当前jenkins项目的工作目录(git库的PHP代码)复制到指定目录"
# rsync -av --exclude='.git' ${WORKSPACE}/* $TARGET_DIR
rsync -av --exclude='.git' root@192.168.75.137:/data/www/diting/sources/admin/${BUILD_NUMBER} $TARGET_DIR_copy
echo "创建vendor目录软连接"
ln -s /data/www/diting/test-admin.vendor $TARGET_DIR/vendor
echo "执行composer更新"
cd $TARGET_DIR
/usr/local/bin/composer update
# echo "修改目录所有者"
# chown -R gitlab-runner:gitlab-runner $TARGET_DIR
echo "删除旧软连接"
unlink /data/www/diting/test-admin
echo "创建源码软连接"
ln -s $TARGET_DIR /data/www/diting/test-admin
echo "部署完成"
;;
#
'rollback')
echo "回滚操作"
version="$2"
echo "回滚版本 #$version"
# 目标目录路径
TARGET_DIR="/data/www/diting/sources/admin/$version"
echo "源码目录 $TARGET_DIR"
echo "检查源码目录"
if [ ! -d $TARGET_DIR ]; then
echo "源码目录不存在了"
exit 1
fi
echo "删除旧软连接"
unlink /data/www/diting/test-admin
echo "创建源码软连接"
ln -s $TARGET_DIR /data/www/diting/test-admin
echo "回滚完成"
;;
esac
启动程序
查看结果
bash
在服务器上查看
bash
[root@localhost www]# ll -lrth /data/www/diting/sources/admin/
total 0
drwxr-xr-x. 3 jenkins jenkins 105 Apr 7 14:52 1
drwxr-xr-x. 3 jenkins jenkins 105 Apr 7 14:53 2
drwxr-xr-x. 3 jenkins jenkins 105 Apr 7 14:57 3
..... (省略多个)
drwxr-xr-x. 3 jenkins jenkins 105 Apr 7 18:01 35
drwxr-xr-x. 3 jenkins jenkins 105 Apr 8 09:26 36
drwxr-xr-x. 3 jenkins jenkins 105 Apr 8 09:43 37
drwxr-xr-x. 3 jenkins jenkins 105 Apr 8 09:44 38
drwxr-xr-x. 3 jenkins jenkins 105 Apr 8 13:28 39
在服务器上会显示你所有完成的构建( 失败的并不会构建成功 )
# 脚本中,先将程序拉取到 /data/www/diting/sources/admin/ 的目录下,然后软链到 test-admin中
# test-admin目录就是当前程序展示目录
[root@localhost www]# ll -lrth /data/www/diting/
total 0
drwxr-xr-x. 2 jenkins jenkins 6 Apr 3 09:30 test-admin.vendor
drwxr-xr-x. 3 jenkins jenkins 19 Apr 7 14:46 sources
lrwxrwxrwx. 1 jenkins jenkins 33 Apr 8 13:28 test-admin -> /data/www/diting/sources/admin/39
正常情况下,两台服务器的展示结果为一样。如果不一样,则错误,需要排查。
回滚操作
查看日志
查看服务器
bash
[root@localhost www]# ll -lrth /data/www/diting/
total 0
drwxr-xr-x. 2 jenkins jenkins 6 Apr 3 09:30 test-admin.vendor
drwxr-xr-x. 3 jenkins jenkins 19 Apr 7 14:46 sources
lrwxrwxrwx. 1 jenkins jenkins 33 Apr 8 13:28 test-admin -> /data/www/diting/sources/admin/39
[root@localhost www]# ll -lrth /data/www/diting/
total 0
drwxr-xr-x. 2 jenkins jenkins 6 Apr 3 09:30 test-admin.vendor
drwxr-xr-x. 3 jenkins jenkins 19 Apr 7 14:46 sources
lrwxrwxrwx. 1 jenkins jenkins 33 Apr 8 13:51 test-admin -> /data/www/diting/sources/admin/37
远程服务器
bash
[root@gitlab ~]# ll -lrth /data/www/diting/
total 0
drwxr-xr-x 3 jenkins jenkins 19 Apr 3 10:18 sources
drwxr-xr-x 2 jenkins jenkins 31 Apr 7 17:16 test-admin.vendor
lrwxrwxrwx 1 root root 33 Apr 8 13:28 test-admin -> /data/www/diting/sources/admin/39
[root@gitlab ~]# ll -lrth /data/www/diting/
total 0
drwxr-xr-x 3 jenkins jenkins 19 Apr 3 10:18 sources
drwxr-xr-x 2 jenkins jenkins 31 Apr 7 17:16 test-admin.vendor
lrwxrwxrwx 1 root root 33 Apr 8 13:51 test-admin -> /data/www/diting/sources/admin/37
jenkins删除构建历史并重置构建序号
- 系统管理 ---> 工具和动作 --> 脚本命令行(Script Console) --> 输入脚本
println(Jenkins.instance.getJobNames()) //查看获取任务名列表
//要重置的任务名称
def jobName = Jenkins.instance.getJobNames()[0] //获取任务名
item = Jenkins.instance.getItemByFullName(jobName)
item.builds.each() { build ->
build.delete() //删除构建历史
}
//重置构建序号
item.updateNextBuildNumber(1)
- 結果為空(服务器中并不会删除记录)