文章目录
1.Jenkins简介:
1、Jenkins是一个开源软件项目,起源于Hudson(Hudson是商用的),是基于Java开发的一种持续集成工具,用于监控持续重复的工作,旨在提供一个开放易用的软件平台,使软件项目可以进行持续集成
2、可在Tomcat等流行的servlet容器中运行,也可独立运行。
3、提供友好操作界面的持续集成(CI)工具
4、它用于实时测试和报告较大代码库中的孤立更改。 Jenkins软件使开发人员能够快速找到并解决代码库中的缺陷,并自动进行构建测试。
2.常见的打包工具:
2.1maven
Maven 是一款基于 Java 平台的项目管理和整合工具,它将项目的开发和管理过程抽象成一个项目对象模型(POM)。 开发人员只需要做一些简单的配置,Maven 就可以自动完成项目的编译、测试、打包、发布以及部署等工作。 Maven 是使用 Java 语言编写的,因此它和 Java 一样具有跨平台性,这意味着无论是在 Windows ,还是在 Linux 或者 Mac OS 上,都可以使用相同的命令进行操作。
Maven 能够帮助开发者完成以下任务:
- 构建项目
- 生成文档
- 创建报告
- 维护依赖
- 软件配置管理
- 发布
- 部署
总而言之,Maven 简化并标准化了项目构建过程。它将项目的编译,生成文档,创建报告,发布,部署等任务无缝衔接,构建成一套完整的生命周期。
2.2 ant
ant 是一个将软件编译、测试、部署等步骤联系在一起加以自动化的一个工具,大多用于Java环境中的软件开发。在实际软件开发中,有很多地方可以用到ant
ant 是Apache软件基金会JAKARTA目录中的一个子项目,它有以下的优点:
- 1、跨平台性:Ant是纯Java语言编写的,所以具有很好的跨平台性。
- 2、操作简单:Ant是由一个内置任务和可选任务组成的,用ant任务就像是在dos中写命令行一样。Ant运行时需要一个XML文件(构建文件)。 Ant通过调用target树,就可以执行各种task。每个task实现了特定接口对象。
- 3、维护简单、可读性好、集成简单:由于Ant构建文件 时XML格式的文件,所以很容易维护和书写,而且结构很清晰。Ant可以集成到开发环境中。由于Ant的跨平台性和操作简单的特点,它很容易集成到一些开发环境中去。
2.3 gradle
Gradle属于结合以上两个的优点,它继承了Ant的灵活和Maven的生命周期管理,它实际上是Maven+Ant的一套更加用户友好的界面。不考虑IDE辅助的情况下,基于Groovy的DSL相比XML配置文件可以少写不少代码。此外因为是基于Groovy的DSL,可以更细致地控制编译过程。比如,Ant要做if-else条件选择特别麻烦,换做gradle的话可以直接套用java的编程风格 if(){}else{}。
总之,gralde 简单,高效,灵活
3.准备工作:
服务器类型 | IP | 应用 | 系统 |
---|---|---|---|
目标主机 | 192.168.106.16 | tomca | centos8/redhat8 |
源主机 | 192.168.106.17 | Jenkins maven | centos8/redhat8 |
4. tomcat项目部署
4.1 java环境安装
//配置阿里云的yun源
[root@node1 ~]# rm -rf /etc/yum.repos.d/*
[root@node1 ~]# wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
[root@node1 ~]# sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
[root@node1 ~]# yum clean all
[root@node1 ~]# yum makecache
Failed to set locale, defaulting to C.UTF-8
CentOS-8.5.2111 - Base - mirrors.aliyun.com 602 kB/s | 4.6 MB 00:07
CentOS-8.5.2111 - Extras - mirrors.aliyun.com 34 kB/s | 10 kB 00:00
CentOS-8.5.2111 - AppStream - mirrors.aliyun.com 410 kB/s | 8.4 MB 00:21
Metadata cache created.
[root@node1 ~]# dnf -y install java-17-openjdk* //下载jdk包
官网下载tomcat
[root@node1 ~]# cd /usr/src/
[root@node1 src]# wget https://archive.apache.org/dist/tomcat/tomcat-9/v9.0.65/bin/apache-tomcat-9.0.65.tar.gz
[root@node1 src]# ls
apache-tomcat-9.0.65.tar.gz debug kernels
[root@node1 src]# cd /usr/local/
[root@node1 local]# mv apache-tomcat-9.0.65 tomcat
[root@node1 local]# ls
bin etc games include lib lib64 libexec sbin share src tomcat
[root@node1 local]# cd tomcat/
[root@node1 tomcat]# ls
bin conf lib logs README.md RUNNING.txt webapps
BUILDING.txt CONTRIBUTING.md LICENSE NOTICE RELEASE-NOTES temp work
//写一个环境变量
[root@node1 ~]# echo 'export PATH=/usr/local/tomcat/bin:$PATH' > /etc/profile.d/tomcat.sh
[root@node1 ~]# source /etc/profile.d/tomcat.sh
//启动tomcat
[root@node1 ~]# catalina.sh start
Using CATALINA_BASE: /usr/local/tomcat
Using CATALINA_HOME: /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME: /usr
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Using CATALINA_OPTS:
Tomcat started.
[root@node1 ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 0.0.0.0:111 0.0.0.0:*
LISTEN 0 32 192.168.122.1:53 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 5 127.0.0.1:631 0.0.0.0:*
LISTEN 0 1 [::ffff:127.0.0.1]:8005 *:*
LISTEN 0 128 [::]:111 [::]:*
LISTEN 0 100 *:8080 *:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 5 [::1]:631 [::]:*
//关闭防火墙和selinux
[root@node1 ~]# systemctl disable --now firewalld.service
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@node1 ~]# setenforce 0
[root@node1 ~]# vim /etc/selinux/config
[root@node1 ~]# cat /etc/selinux/config
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
SELINUX=disabled //修改为disabled
# SELINUXTYPE= can take one of these three values:
# targeted - Targeted processes are protected,
# minimum - Modification of targeted policy. Only selected processes are protected.
# mls - Multi Level Security protection.
SELINUXTYPE=targeted
在浏览器上浏览网页
这里ip的后面要加上8080才能访问到
5.部署Jenkins
按照同样的方法给node2也配一个tomcat
[root@node2 ~]# rm -rf /etc/yum.repos.d/*
[root@node2 ~]# wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
[root@node2 ~]# sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
[root@node2 ~]# yum clean all
[root@node2 ~]# yum makecache
Failed to set locale, defaulting to C.UTF-8
CentOS-8.5.2111 - Base - mirrors.aliyun.com 602 kB/s | 4.6 MB 00:07
CentOS-8.5.2111 - Extras - mirrors.aliyun.com 34 kB/s | 10 kB 00:00
CentOS-8.5.2111 - AppStream - mirrors.aliyun.com 410 kB/s | 8.4 MB 00:21
Metadata cache created.
[root@node2 ~]# dnf -y install java-17-openjdk* //下载jdk包
//下载tomcat包
[root@node2 ~]# cd /usr/src/
[root@node2 src]# wget https://archive.apache.org/dist/tomcat/tomcat-9/v9.0.65/bin/apache-tomcat-9.0.65.tar.gz
[root@node2 src]# ls
apache-tomcat-9.0.65.tar.gz debug kernels
[root@node2 src]# cd /usr/local/
[root@node2 local]# mv apache-tomcat-9.0.65 tomcat
[root@node2 local]# ls
bin etc games include lib lib64 libexec sbin share src tomcat
[root@node2 local]# cd tomcat/
[root@node2 tomcat]# ls
bin conf lib logs README.md RUNNING.txt webapps
BUILDING.txt CONTRIBUTING.md LICENSE NOTICE RELEASE-NOTES temp work
//写一个环境变量
[root@node2 ~]# echo 'export PATH=/usr/local/tomcat/bin:$PATH' > /etc/profile.d/tomcat.sh
[root@node2 ~]# source /etc/profile.d/tomcat.sh
//启动tomcat
[root@node2 ~]# catalina.sh start
Using CATALINA_BASE: /usr/local/tomcat
Using CATALINA_HOME: /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME: /usr
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Using CATALINA_OPTS:
Tomcat started.
[root@node2 ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 0.0.0.0:111 0.0.0.0:*
LISTEN 0 32 192.168.122.1:53 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 5 127.0.0.1:631 0.0.0.0:*
LISTEN 0 1 [::ffff:127.0.0.1]:8005 *:*
LISTEN 0 128 [::]:111 [::]:*
LISTEN 0 100 *:8080 *:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 5 [::1]:631 [::]:*
//关闭防火墙和selinux
[root@node2 ~]# systemctl disable --now firewalld.service
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@node2 ~]# setenforce 0
[root@node2 ~]# vim /etc/selinux/config
[root@node2 ~]# cat /etc/selinux/config
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
SELINUX=disabled //修改为disabled
# SELINUXTYPE= can take one of these three values:
# targeted - Targeted processes are protected,
# minimum - Modification of targeted policy. Only selected processes are protected.
# mls - Multi Level Security protection.
SELINUXTYPE=targeted
在网页上访问node2(后面添加:8080)
5.1.下载Jenkins安装包
//刚才下载好的包放到webapps里面来
[root@localhost host-manager]# cd ..
[root@localhost webapps]# mv * /opt/ //把里面的东西先移走
[root@localhost webapps]# ls
[root@localhost webapps]# ls
jenkins jenkins.war //放进来会自动解压,如果没有自动解压,重启一下tomcat就好了
访问网页(ip地址的后面需要加上Jenkins)。然后就是等待
输入后进入下一个界面,可能会卡一会
升级站点
下载命令行接口
然后放到你的主机里面
[root@node2 ~]# wget http://192.168.106.17:8080/jenkins/jnlpJars/jenkins-cli.jar
--2022-08-18 08:07:22-- http://192.168.106.17:8080/jenkins/jnlpJars/jenkins-cli.jar
正在连接 192.168.106.17:8080... 已连接。
已发出 HTTP 请求,正在等待回应... 200
长度:3571391 (3.4M) [application/java-archive]
正在保存至: “jenkins-cli.jar”
jenkins-cli.jar 100%[===============>] 3.41M --.-KB/s 用时 0.01s
2022-08-18 08:07:22 (346 MB/s) - 已保存 “jenkins-cli.jar” [3571391/3571391])
//使用改包来执行命令
[root@node2 ~]# java -jar jenkins-cli.jar -s http://192.168.106.17:8080/jenkins/ help //查看帮助文档
ERROR: You must authenticate to access this Jenkins.
Jenkins CLI
Usage: java -jar jenkins-cli.jar [-s URL] command [opts...] args...
Options:
-s URL : the server URL (defaults to the JENKINS_URL env var)
-http : use a plain CLI protocol over HTTP(S) (the default;
//省略一部分
[root@node2 ~]# dnf -y install git maven //安装这两个包
//安装过程省略...
配置免密登录
[root@node2 workspace]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:6xvuCWhxBqhXp7zGiI+3EjUvcUnIMBhdwHabuxKXkJs root@node2
The key's randomart image is:
+---[RSA 3072]----+
|=*.+. |
|..*.o |
| ..+o+. |
| .*o=+ |
|...O+oo S |
|.oEo=* . |
|...+*.. o |
|.ooo. + o |
|.ooo .*. |
+----[SHA256]-----+
[root@node2 workspace]# ssh-copy-id root@192.168.106.16 //生成密钥
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.106.17 (192.168.106.17)' can't be established.
ECDSA key fingerprint is SHA256:poWk0FDJ6F9B66E87Bm77w7aOFrchvdBwwvIxKszYSw.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.106.17's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'root@192.168.106.16'"
and check to make sure that only the key(s) you wanted were added.
[root@node2 workspace]# ssh root@192.168.106.16 //登录验证
Activate the web console with: systemctl enable --now cockpit.socket
This system is not registered to Red Hat Insights. See https://cloud.redhat.com/
To register this system, run: insights-client --register
Last login: Thu Aug 18 07:02:03 2022 from 192.168.106.16
[root@node1 ~]# exit
注销
Connection to 192.168.106.16 closed.
创建流水线
//进入到工作目录查看是否有一个.war的包,
[root@node2 workspace]# cd test/
db/ .git/ src/ target/
[root@node2 workspace]# cd test/target/
[root@node2 target]# ll
总用量 17840
drwxr-x--- 5 root root 95 8月 18 10:39 classes
drwxr-x--- 3 root root 25 8月 18 10:39 generated-sources
drwxr-x--- 4 root root 37 8月 18 10:39 ly-simple-tomcat-0.0.1-SNAPSHOT
-rw-r----- 1 root root 18265403 8月 18 10:39 ly-simple-tomcat-0.0.1-SNAPSHOT.war //已经打包好了
drwxr-x--- 2 root root 28 8月 18 10:39 maven-archiver
drwxr-x--- 3 root root 35 8月 18 10:39 maven-status
//查看在node1的webapps目录上是否有这个包
[root@node1 ~]# cd /usr/local/tomcat/webapps/
docs/ host-manager/ manager/
examples/ ly-simple-tomcat-0.0.1-SNAPSHOT/ ROOT/
[root@node1 ~]# cd /usr/local/tomcat/webapps/
[root@node1 webapps]# ls
docs examples host-manager ly-simple-tomcat-0.0.1-SNAPSHOT ly-simple-tomcat-0.0.1-SNAPSHOT.war manager ROOT //已经传过来了
[root@node1 webapps]#