/dev/null; then echo "检测到 jdk 已安装,跳过安装步骤" else if [[ -f "$CURRENT_D">
当前位置:   article > 正文

一键安装jenkins(shell脚本)

一键安装jenkins(shell脚本)

一键安装jenkisn,支持在线和离线安装
附带安装了jdk,node,maven
下载博主的脚本和安装包比在线下载安装快很多

shell脚本

#!/bin/bash 
# 当前目录
CURRENT_DIR=$(
   cd "$(dirname "$0")"
   pwd
)

#安装jdk
installJdk(){
    if which java >/dev/null; then
		echo "检测到 jdk 已安装,跳过安装步骤"
	else
		if [[ -f "$CURRENT_DIR/jdk-8u181-linux-x64.tar.gz" ]]; then
			echo "... 离线安装jdk"
			cd $CURRENT_DIR && tar -zxf jdk-8u181-linux-x64.tar.gz 
			cp /etc/profile $(pwd) && cd jdk1.8.0_181
			cat >> /etc/profile <<EOF
			export JAVA_HOME=$PWD 
			export CLASSPATH=.:\$JAVA_HOME/lib/dt.jar:\$JAVA_HOME/lib/tools.jar
			export PATH=\$PATH:\$JAVA_HOME/bin
EOF
			ln -s $(pwd)/bin/java /usr/bin/java
			source /etc/profile && java -version
			echo "... 离线安装jdk 完成"
		else
			echo "... 在线安装jdk"
			sudo yum install -y java-1.8.0-openjdk-devel
			java -version
			echo "... 在线安装jdk 完成"
		fi
	fi
}

#安装maven
installMaven(){
	if which mvn >/dev/null; then
		echo "检测到 maven 已安装,跳过安装步骤"
	else
		if [[ -f "$CURRENT_DIR/apache-maven-3.6.3-bin.tar.gz" ]]; then
			echo "... 离线安装maven"
		else
			echo "... 在线安装 maven"
			wget http://mirror.bit.edu.cn/apache/maven/maven-3/3.6.3/binaries/apache-maven-3.6.3-bin.tar.gz
		fi
		cd $CURRENT_DIR && tar -zxf apache-maven-3.6.3-bin.tar.gz
		cp /etc/profile $(pwd) && cd apache-maven-3.6.3
		cat >> /etc/profile <<EOF 
		export MAVEN_HOME=$PWD 
		export PATH=\$MAVEN_HOME/bin:\$PATH
EOF
		source /etc/profile && mvn -v 
		echo "... maven安装完成"
	fi
}

# 安装node
installNode(){
    if which node >/dev/null; then
		echo "检测到 node 已安装,跳过安装步骤"
	else
		if [[ -f "$CURRENT_DIR/node-v12.14.0-linux-x64.tar.xz" ]]; then
			echo "... 离线安装node"
		else
			echo "... 在线安装 node"
			cd $CURRENT_DIR && wget https://nodejs.org/dist/v12.14.0/node-v12.14.0-linux-x64.tar.xz
		fi
		cd $CURRENT_DIR && tar -xf node-v12.14.0-linux-x64.tar.xz
		cp /etc/profile $(pwd) && cd node-v12.14.0-linux-x64
		cat >> /etc/profile <<EOF 
		export NODE_HOME=$PWD 
		export PATH=\$PATH:\$NODE_HOME/bin 
EOF
		source /etc/profile && node -v && npm -v
		echo "... node安装完成"
	fi
}

#install cnpm、yarn
installCnpm(){
	echo " install cnpm、yarn 开始"
	npm install -g cnpm --registry=https://registry.npm.taobao.org
	cnpm install -g yarn
	yarn config set registry https://registry.npm.taobao.org
	echo " install cnpm、yarn 完成"
}

#jenkins 安装
installOffineJenkins(){
	# YUM安装 
	# 卸载 rpm -e jenkins-2.138.4-1.1.noarch 
	echo " jenkins安装 开始"
	cd $CURRENT_DIR && yum -y localinstall jenkins-2.235.1-1.1.noarch.rpm
	#/usr/lib/jenkins/jenkins.war WAR包 
	#/etc/sysconfig/jenkins 配置文件 
	#/var/lib/jenkins/ 默认的JENKINS_HOME目录 
	#/var/log/jenkins/jenkins.log Jenkins日志文件

	#只修改了端口和JENKINS_USER
	sed -i 's/JENKINS_USER="jenkins"/JENKINS_USER="root"/g' /etc/sysconfig/jenkins 

	sed -i 's/JENKINS_PORT="8080"/JENKINS_PORT="9999"/g' /etc/sysconfig/jenkins 


	#创建软连接,修改数据存储位置
	mkdir -p /home/jenkins && rm -rf /var/lib/jenkins && cd /var/lib/ && ln -s /home/jenkins jenkins && chmod 777 /home/jenkins && cd /home/jenkins


	# 开机自启
	sudo systemctl enable jenkins && sudo systemctl restart jenkins
	sleep 60
	# 修改jenkins插件下载源
	while(true)
	do
		if [ -f "/home/jenkins/hudson.model.UpdateCenter.xml" ]; then
			sed -i 's/https/http/g' /home/jenkins/hudson.model.UpdateCenter.xml
			echo "执行成功"
			break
		else
			sudo systemctl restart jenkins
			echo "重启jenkins" 
			sleep 90
		fi
	done
	# 修改jenkins插件下载网络检查链接
	while(true)
	do
		if [ -f "/home/jenkins/updates/default.json" ]; then
			sed -i 's/www.google.com/www.baidu.com/g' /home/jenkins/updates/default.json
			echo "执行成功"
			break
		else
			sudo systemctl restart jenkins
			echo "重启jenkins" 
			sleep 90
		fi
	done

	# 重启jenkins
	systemctl restart jenkins
	echo " jenkins安装 结束"
}

installJenkins(){
	systemctl status jenkins.service;
	if [ $? -eq  0 ]; then
		echo "检测到 jenkins 已安装,跳过安装步骤"
	else
		if [[ -f "$CURRENT_DIR/jenkins-2.235.1-1.1.noarch.rpm" ]]; then
			echo "... 离线安装jenkins"
		else
			echo "... 在线安装 jenkins"
			cd $CURRENT_DIR && wget https://pkg.jenkins.io/redhat-stable/jenkins-2.235.1-1.1.noarch.rpm ||exit
		fi
		installOffineJenkins
	fi
}

#操作
#禁用selinux
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config && setenforce 0
#关闭防火墙
systemctl stop firewalld && systemctl disable firewalld

installJdk
installMaven
installNode
installCnpm
#安装jenkins
installJenkins

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
在网络条件不好的情况下建议离线安装,请先下载下面的包

离线的安装包
链接:https://pan.baidu.com/s/1h_6aw7-om9ClY0_Ad0Bo4A
提取码:6vox

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/216819
推荐阅读
相关标签
  

闽ICP备14008679号