Devops-day4-Jenkins参数化构建Java+PHP
文章目录
参数化构建类型介绍:
1.git branch list
2.文本参数
3.密码参数
4.凭证参数
5.布尔参数
6.隐藏参数
7.下拉参数
Jenkins 部署PHP
1.Jenkins免密
# jenkins服务器对目标服务器做免密
[root@jenkins ~]# ssh-copy-id -i .ssh/id_rsa.pub root@192.168.12.11
2.增加server
系统管理 ---> 系统配置 ---> Publish over ssh 增加一个ssh server
3.配置gitlab代码仓库
来到gitlab仓库,进入项目分支
创建文件index.php,编辑内容:
<?php
echo "我就是一个PHP,红红火火恍恍惚惚~";
>
新建标签,这里为stable3,复制链接地址,填入jenkins配置URL页面~
4.配置nfs
# 准备nfs挂载点目录:v4
[root@k8s-master1 ~]# ll /nfs
total 0
drwxr-xr-x 2 root root 6 Apr 10 22:01 v1
drwxr-xr-x 2 root root 6 Apr 12 19:31 v2
drwxr-xr-x 4 polkitd root 145 Apr 13 23:12 v3
drwxr-xr-x 2 root root 23 Apr 17 13:32 v4
# 授权nfs访问权限
[root@k8s-master1 ~]# chmod -R 755 /nfs/v4
# 安装nfs
[root@k8s-master1 ~]# yum install -y nfs-utils
# 添加挂载配置信息
[root@k8s-master1 ~]# vim /etc/exports
/nfs/v1 172.16.12.0/24(rw,no_root_squash)
/nfs/v2 172.16.12.0/24(rw,no_root_squash)
/nfs/v3 172.16.12.0/24(rw,no_root_squash)
/nfs/v4 172.16.12.0/24(rw,no_root_squash)
# 查看是否生效
[root@k8s-master1 ~]# showmount -e
Export list for k8s-master1:
/nfs/v4 172.16.12.0/24
/nfs/v3 172.16.12.0/24
/nfs/v2 172.16.12.0/24
/nfs/v1 172.16.12.0/24
[root@k8s-master1 ~]# exportfs
/nfs/v1 172.16.12.0/24
/nfs/v2 172.16.12.0/24
/nfs/v3 172.16.12.0/24
/nfs/v4 172.16.12.0/24
# 启动nfs
[root@k8s-master1 ~]# systemctl enable --now nfs
# jenkins服务器对目标服务器做免密
[root@localhost ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.12.11
# 增加server
系统管理 ---> 系统配置 ---> Publish over ssh 增加一个ssh server
- 准备配置清单
cat > php.yaml <<EOF
kind: Deployment
apiVersion: apps/v1
metadata:
name: jenkins-nginx-php
spec:
selector:
matchLabels:
app: jenkins-nginx-php
template:
metadata:
labels:
app: jenkins-nginx-php
spec:
containers:
- name: php
image: alvinos/php:discuz-v2
volumeMounts:
- mountPath: /usr/share/nginx/html
name: data
- name: nginx
image: alvinos/nginx:discuz-v1
volumeMounts:
- mountPath: /usr/share/nginx/html
name: data
volumes:
- name: data
nfs:
path: /nfs/v4
server: 192.168.12.11
---
kind: Service
apiVersion: v1
metadata:
name: jenkins-nginx-php-svc
spec:
ports:
- port: 80
targetPort: 80
selector:
app: jenkins-nginx-php
type: nodePort
EOF
4.创建PHP项目
Jenkins部署TomCat
编译环境(Jenkins服务器)
运行环境(web服务器)
编译环境
1.部署maven编译环境
# 编译环境(jenkins服务器上部署)
[root@jenkins ~]# wget https://mirrors.aliyun.com/apache/maven/maven-3/3.6.3/binaries/apache-maven-3.6.3-bin.tar.gz
[root@jenkins ~]# tar -xf apache-maven-3.6.3-bin.tar.gz -C /usr/local/
[root@jenkins ~]# vim /etc/profile
export MAVEN_HOME=/usr/local/apache-maven-3.6.3
export PATH=$MAVEN_HOME/bin:$PATH
[root@localhost opt]# source /etc/profile
# 测试mvn
[root@jenkins ~]# mvn --version
Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f)
2.配置Jenkins + maven
-
配置JDK
-
配置maven环境变量
- 安装maven插件
3.创建代码仓库
4.创建maven仓库
# 在Jenkins服务器上创建
[root@jenkins ~]# mkdir -pv /data/software/repository
mkdir: created directory ‘/data’
mkdir: created directory ‘/data/software’
mkdir: created directory ‘/data/software/repository’
[root@jenkins ~]# chown -R jenkins. /data/software/repository
# 修改配置文件
[root@localhost apache-maven-3.6.3]# vim /usr/local/apache-maven-3.6.3/conf/settings.xml
# 158行 -->:另起一行粘贴
# 将maven仓库所需插件下载到本地仓库
</mirror>
-->
<!--阿里云镜像-->
<mirror>
<id>aliyun-maven</id>
<mirrorOf>central</mirrorOf>
<name>aliyun maven mirror</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
</mirror>
</mirrors>
<!-- profiles
# 搜索# localRepository,另起一行粘贴,将maven仓库所需插件下载到此本地仓库
<!--本地仓库-->
<localRepository>/data/software/repository</localRepository>
5.测试maven
- 仓库地址
git@192.168.12.82:root/Jenkins-TomCat.git # ssh
http://192.168.12.82/root/Jenkins-TomCat.git # http
准备仓库、文件,并推送至新仓库
[root@gitlab ~]# tree Jenkins-TomCat/
Jenkins-TomCat/
└── springboot
├── mvnw
├── mvnw.cmd
├── pom.xml
└── src
├── main
│ ├── java
│ │ └── com
│ │ └── example
│ │ └── gitlab
│ │ ├── controller
│ │ │ └── HelloWorldController.java
│ │ ├── GitLabApplication.java
│ │ └── ServletInitializer.java
│ └── resources
│ └── application.properties
└── test
└── java
└── com
└── example
└── gitlab
└── GitLabApplicationTests.java
14 directories, 8 files
# 绑定用户名、邮箱
[root@k8s-master1 test]# git config --global user.name "Peng"
[root@k8s-master1 test]# git config --global user.email "1710724925@qq.com"
# 查看绑定
[root@k8s-master1 data]# git config -l | grep user
user.email=1710724925@qq.com
user.name=Peng
# 指定查看
[root@k8s-master1 test]# git config --get user.name
Peng
[root@k8s-master1 test]# it config --get user.email
1710724925@qq.com
[root@k8s-master1 test]# git add .
[root@k8s-master1 test]# git commit 'init' .
[root@k8s-master1 test]# git push -u origin master
运行环境
- Web服务器
1.安装tomcat
# 安装Java环境
[root@k8s-master1 ~]# yum install java-1.8.0-openjdk* -y
# 下载tomcat包
[root@k8s-master1 ~]# wget https://ftp.wayne.edu/apache/tomcat/tomcat-9/v9.0.45/bin/apache-tomcat-9.0.45.tar.gz
[root@k8s-master1 ~]# tar -xf apache-tomcat-9.0.45.tar.gz -C /usr/local/
[root@k8s-master1 ~]# /usr/local/apache-tomcat-9.0.45/bin/startup.sh
2.配置权限
-
此时访问官网可以,但管理web无权限
-
配置tomcat权限
1.用户和密码都是:tomcat
[root@k8s-master1 ~]# vim /usr/local/apache-tomcat-9.0.45/conf/tomcat-users.xml
<role rolename="tomcat"/>
<role rolename="role1"/>
<role rolename="manager-script"/>
<role rolename="manager-gui"/>
<role rolename="manager-status"/>
<role rolename="admin-gui"/>
<role rolename="admin-script"/>
<user username="tomcat" password="tomcat"
roles="manager-gui,manager-script,tomcat,admin-gui,admin-script"/>
</tomcat-users> # 粘贴在此行之上
2.为了能够刚才配置的用户登录到 Tomcat,还需注释掉以下配置
[root@localhost conf]# vim /usr/local/apache-tomcat-9.0.45/webapps/manager/META-INF/context.xml
# 默认是只允许当前127本机访问,我们需要让它需要所有机器都能访问(此处)
# 在公司里,是需要配成只允许jenkins内网来访问的~
# 注释格式: <!-- 内容 -->
<!-- <Valve className="org.apache.catalina.valves.RemoteAddrValve"
allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" /> -->
3.重启tomcat
[root@k8s-master1 ~]# /usr/local/apache-tomcat-9.0.45/bin/shutdown.sh
[root@k8s-master1 ~]# /usr/local/apache-tomcat-9.0.45/bin/startup.sh
Using CATALINA_BASE: /usr/local/apache-tomcat-9.0.45
Using CATALINA_HOME: /usr/local/apache-tomcat-9.0.45
Using CATALINA_TMPDIR: /usr/local/apache-tomcat-9.0.45/temp
Using JRE_HOME: /usr
Using CLASSPATH: /usr/local/apache-tomcat-9.0.45/bin/bootstrap.jar:/usr/local/apache-tomcat-9.0.45/bin/tomcat-juli.jar
Using CATALINA_OPTS:
Tomcat started.
- 配置添加凭据
- 再次访问manager webapp则弹出登录页面,登录后访问成功!
# 此时路径下生成的目录与文件会与网页一致
[root@k8s-master1 apache-tomcat-9.0.45]# ll /usr/local/apache-tomcat-9.0.45/webapps/
total 4
drwxr-x--- 15 root root 4096 Apr 18 16:51 docs
drwxr-x--- 7 root root 99 Apr 18 16:51 examples
drwxr-x--- 6 root root 79 Apr 18 16:51 host-manager
drwxr-x--- 6 root root 114 Apr 18 16:51 manager
drwxr-x--- 3 root root 223 Apr 18 16:51 ROOT
# 新建文件内容测试访问
[root@k8s-master1 webapps]# echo 'Hello Tom Cat!' > examples/damo.html
3.配置Jenkins操作tomcat
- 下载部署插件
- 勾选安装完成自动重启(若一分钟左右一直pending状态,或许需要手动刷网页)