Jenkins环境:
一般需要安装三个基础环境:git、Jenkins、jdk
1、YUM安装方式(比较慢,生成环境中不建议)
安装步骤:Redhat Jenkins Packages
- 添加Jenkins下载repo源:
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
- 安装jdk/jenkins:
yum install -y fontconfig java-11-openjdk
yum install -y jenkins
3.安装git:
版本要求:版本在2.0以上,编译安装
https://www.kernel.org/pub/software/scm/git/
先卸载之前的版本,yum -y remove git
注意:卸载完后,新开一个shell窗口,不然git会有报错:
git 源码安装后报错/usr/bin/git: No such file or directory
解决方法:
先卸载的旧的git默认路径为/usr/bin/git,新编译安装的git在/usr/local/bin目录下,终端session保存了原来的路径,重新打开新的终端即可解决
编译安装git步骤:
此处下载的:git-2.13.3.tar.gz
[root@VM-4-16-centos ~]# cd /usr/local/src/ # 安装路径
[root@VM-4-16-centos src]# wget https://www.kernel.org/pub/software/scm/git/git-2.13.3.tar.gz
[root@VM-4-16-centos src]# tar -zxvf git-2.13.3.tar.gz
[root@VM-4-16-centos src]# cd git-2.13.3/
[root@VM-4-16-centos git-2.13.3]# ./configure --prefix=/usr/local/git # 执行配置文件
[root@VM-4-16-centos git-2.13.3]# make && make install # 编译并安装
#添加软链接(这里不建议这样操作,坑很多)
[root@VM-4-16-centos bin]# ln -s /usr/local/git/bin/git /usr/bin/git
[root@VM-4-16-centos bin]# ln -s /usr/local/git/bin/git-upload-pack /usr/bin/git-upload-pack
[root@VM-4-16-centos bin]# ln -s /usr/local/git/bin/git-receive-pack /usr/bin/git-receive-pack
# 建议这样操作:
vim /etc/profile
export PATH="/usr/local/git/bin:$PATH" # 添加到行尾
source /etc/profile # 使配置立马生效
验证git版本和java版本
[root@VM-4-16-centos bin]# git --version # 验证git版本
git version 2.13.3
[root@VM-4-16-centos ~]# cat /etc/profile
......
export JAVA_HOME=/usr/local/jdk
export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH
export CLASSPATH=.$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/jre/lib:$JAVA_HOME/lib/tools.jar
export PATH="/usr/local/git/bin:$PATH"
[root@VM-4-16-centos ~]# java --version
openjdk 11.0.15 2022-04-19 LTS
OpenJDK Runtime Environment 18.9 (build 11.0.15+9-LTS)
OpenJDK 64-Bit Server VM 18.9 (build 11.0.15+9-LTS, mixed mode, sharing)
2、编译安装方式(git在上面,这里不做重复叙述)
- 下载编译安装Jenkins
centos:Jenkins_redhat
https://mirrors.tuna.tsinghua.edu.cn/jenkins/redhat/
此处下载的:jenkins-2.350-1.1.noarch.rpm
cd /usr/local/src
rpm -ivh jenkins-2.350-1.1.noarch.rpm
# 修改端口号,默认8080,ss -ntl查看本地是否有服务在占用此端口号
vim /etc/sysconfig/jenkins
JENKINS_PORT="8080" # Jenkins的默认端口号
JENKINS_USER="jenkins" # 执行Jenkins服务的用户
- 下载编译java
java包:https://www.oracle.com/cn/java/technologies/javase/javase8-archive-downloads.html
此处下载的:jdk-8u333-linux-x64.tar.gz
java环境:
[root@VM-4-16-centos src]# tar xvf jdk-8u192-linux-x64.tar.gz
[root@VM-4-16-centos src]# ln -sv /usr/local/src/jdk1.8.0_192/ /usr/local/jdk
[root@VM-4-16-centos src]# ln -sv /usr/local/jdk/bin/java /usr/bin/ #java 命令软连接
添加环境变量
[root@VM-4-16-centos ~]# vim /etc/profile
[root@VM-4-16-centos ~]# source /etc/profile
export JAVA_HOME=/usr/local/jdk
export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH
export CLASSPATH=.$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/jre/lib:$JAVA_HOME/lib/tools.jar
3、启动并配置Jenkins
- 启动服务
[root@VM-4-16-centos ~]# systemctl daemon-reload # 修改配置后生效
[root@VM-4-16-centos ~]# systemctl start jenkins # 启动Jenkins
[root@VM-4-16-centos ~]# systemctl status jenkins # 查看服务是否启动成功
- 查看Jenkins的端口号:
netstat -antp 8080
或者
ss -ntl(个人一般用这个命令查看端口号,-p可以查看具体服务对应的端口)
ps -aux | grep jenkins # 确认进程启动
-
网页访问:
http://xxx.xxx.xxx.xxx:8080/
-
Jenkins的密码可以在日志中查看,也可以直接cat具体文件:
cat /var/lib/jenkins/secrets/initialAdminPassword
成功页面:
注意:因为是Jenkins用户,使用Jenkins账号的git服务时,会因为权限出现各种问题,主要的是:
1、Jenkins通过pipeline获取代码的时候,报错stderr: Host key verification failed.
解决办法:
1. 切换到Jenkins用户sudo su -s /bin/bash jenkins
2. 执行git命名拉取代码git clone ssh://git@xxx.xxx.xxx.xxx:xxx/shell.git会报错,显示没有权限
3.配置Jenkins用户的ssh-keygen,将公钥配置到github就可以了(/var/lib/jenkins/.ssh/id_rsa.pub)
正常:(如果用https地址,则需要配置密钥,此密钥就是GitHub仓的账号密码)
2、git clone下载失败:
报错如下:
bash: git-upload-pack: command not found
bash: git-receive-pack: command not found
fatal: The remote end hung up unexpectedly
原因:原来代码服务器上的git安装路径是/usr/local/git,不是默认路径,根据提示,在git服务器上, 建立链接文件:
解决方法:
# ln -s /usr/local/git/bin/git-upload-pack /usr/bin/git-upload-pack
再次执行 git clone 成功!
3、git下载代码仓失败
fatal: '/home/git/repos/app.git' does not appear to be a git repository fatal: Could not read from remote repository.
解决方法:要用这个命令git init --bare szb.git(你的仓库名)
初始化server端的仓库!