jenkins
1.先说说CI/CD
生产环境下,提前需要备份,再操作
集成:个人研发的部分与软件整体部分交付的过程,代码集成流程:编译,测试,打包
CI(continuous Integration):持续集成,在持续集成环境中,开发人员将会频繁的提交代码到主干。这些新提交在最终合并到主线之前,都需要通过编译和自动化测试流进行验证。这样做是基于之前持续集成过程中很重视自动化测试验证结果,以保障所有的提交在合并主线之后的质量问题,对可能出现的一些问题进行预警。一般用于测试环境和开发环境。
CD:代码持续交互,持续交付就是讲我们的应用发布出去的过程。这个过程可以确保我们尽可能快的实现交付。这就意味着除了自动化测试,我们还需要有自动化的发布流,以及通过一个按键就可以随时随地实现应用的部署上线。
持续部署的过程要手动部署
而jenkins中提供的插件可以实现CI/CD功能
几种发布流程场景:
2.jenkins介绍
jenkins官网
jenkins是一个开源软件项目,是基于java开发的一种持续集成工具,用于监控持续重复的工作,提供一个开放易用的软件平台,使软件项目可以持续集成。
jenkins可以在每个人git代码后,自动检测到代码的变动,自动的打包,测试,提交,出现问题会以邮件的方式提醒。
3.jenkins包下载
4.jenkins部署
[root@jenkins ~]
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@jenkins ~]
[root@jenkins ~]
[root@jenkins ~]
[root@jenkins ~]
[root@jenkins ~]
[root@jenkins ~]
[root@jenkins ~]
[root@jenkins ~]
[root@jenkins ~]
[root@jenkins ~]
[root@jenkins webapps]
[root@jenkins webapps]
jenkins.war
[root@jenkins webapps]
Using CATALINA_BASE: /usr/local/jenkins
Using CATALINA_HOME: /usr/local/jenkins
Using CATALINA_TMPDIR: /usr/local/jenkins/temp
Using JRE_HOME: /usr
Using CLASSPATH: /usr/local/jenkins/bin/bootstrap.jar:/usr/local/jenkins/bin/tomcat-juli.jar
Using CATALINA_OPTS:
Tomcat started.
[root@jenkins webapps]
jenkins jenkins.war
进行jenkins访问
https://mirrors.tuna.tsinghua.edu.cn/jenkins/updates/update-center.json
全局工具内配置
maven中pom,jar,war 1.pom是maven依赖文件 2.jar是java普通项目打包文件 3.war是javaweb项目打包文件
系统配置
5.jenkins手动部署java项目
版本 | 服务 | ip |
---|
centos8 | jenkins | 192.168.136.129 | centos8 | tomcat | 192.168.136.233 |
发布java项目的步骤
1.拉取代码并且打包
git clone xx
mvn clean package
2.提前做好项目备份
3.war包传到目标服务器上的webapps目录中(做免密登录)
4.重启目标服务器的tomcat服务(会自动解压包)
5.修改项目的配置
6.重启tomcat服务
5.1jenkins服务开启
[root@jenkins ~]
Using CATALINA_BASE: /usr/local/jenkins
Using CATALINA_HOME: /usr/local/jenkins
Using CATALINA_TMPDIR: /usr/local/jenkins/temp
Using JRE_HOME: /usr
Using CLASSPATH: /usr/local/jenkins/bin/bootstrap.jar:/usr/local/jenkins/bin/tomcat-juli.jar
Using CATALINA_OPTS:
Tomcat started.
[root@jenkins ~]
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 5 127.0.0.1:631 0.0.0.0:*
LISTEN 0 128 0.0.0.0:111 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 5 [::1]:631 [::]:*
LISTEN 0 1 [::ffff:127.0.0.1]:8005 *:*
LISTEN 0 128 [::]:111 [::]:*
LISTEN 0 100 *:8080 *:*
LISTEN 0 128 [::]:22 [::]:*
浏览器登录到jenkins界面 先来模拟一个自带的hello world小项目 5.2java开发人员已经把代码放在了gitee仓库,我们第一步应该开始拉代码并且打包
[root@jenkins ~]
[root@jenkins src]
正克隆到 'tomcat-java-demo'...
remote: Enumerating objects: 558, done.
remote: Counting objects: 100% (558/558), done.
remote: Compressing objects: 100% (316/316), done.
remote: Total 558 (delta 217), reused 558 (delta 217), pack-reused 0
接收对象中: 100% (558/558), 5.08 MiB | 771.00 KiB/s, 完成.
处理 delta 中: 100% (217/217), 完成.
[root@jenkins src]
[root@jenkins tomcat-java-demo]
[root@jenkins tomcat-java-demo]
db deploy.yaml Dockerfile jenkinsfile LICENSE pom.xml README.md src target(包在此目录下)
[root@jenkins tomcat-java-demo]
[root@jenkins target]
classes ly-simple-tomcat-0.0.1-SNAPSHOT maven-archiver
generated-sources ly-simple-tomcat-0.0.1-SNAPSHOT.war maven-status
[root@jenkins target]
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Created directory '/root/.ssh'.
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:q0QOr+cNeRCkbnWDw8AuSG7txPMHh/SwQvgRqFOpz2M root@jenkins
The key's randomart image is:
+---[RSA 3072]----+
| o+o . |
| +oo.B . |
|++=.+ @ o |
|=o.O.= * . |
|.+o.B = S |
| Eo * + . |
| . . B o |
| o.= |
| .oo . |
+----[SHA256]-----+
[root@jenkins target]
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.136.233 (192.168.136.233)' can't be established.
ECDSA key fingerprint is SHA256:z1v69UX9NnT1/ikGZRDxB+Yeqgwi2YRfOMJ1t5ZTHtU.
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.136.233's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'root@192.168.136.233'"
and check to make sure that only the key(s) you wanted were added.
[root@jenkins ~]
ly-simple-tomcat-0.0.1-SNAPSHOT.war 100% 17MB 217.8MB/s 00:00
[root@tomcat ~]
[root@tomcat webapps]
docs examples host-manager ly-simple-tomcat-0.0.1-SNAPSHOT.war manager ROOT
[root@tomcat webapps]
[root@tomcat webapps]
[root@tomcat webapps]
clq clq.war docs examples host-manager manager ROOT
6.jenkins自动部署java项目
版本 | 服务 | ip |
---|
centos8 | jenkins | 192.168.136.129 | centos8 | tomcat | 192.168.136.233 |
配置jenkins中的tests项目 脚本修改为(公有仓库拉取进行部署)
pipeline {
agent any
stages {
stage('Build') {
steps {
// Get some code from a GitHub repository
git 'https://gitee.com/forgotten/tomcat-java-demo.git'
// Run Maven on a Unix agent.
sh "mvn -Dmaven.test.failure.ignore=true clean package"
// To run Maven on a Windows agent, use
// bat "mvn -Dmaven.test.failure.ignore=true clean package"
}
}
}
}
如果报Could not initialize class sun.nio.fs.LinuxNativeDispatcher错误 解决方法:重启jenkins服务,再次执行即可 最终脚本修改
pipeline {
agent any
stages {
stage('Build') {
steps {
// 此处git拉取的是不用输入用户和邮箱的java代码块
git 'https://gitee.com/forgotten/tomcat-java-demo.git'
// Run Maven on a Unix agent.
sh "mvn -Dmaven.test.failure.ignore=true clean package"
sh "mv target/ly-simple-tomcat-0.0.1-SNAPSHOT.war target/app.war"
// To run Maven on a Windows agent, use
// bat "mvn -Dmaven.test.failure.ignore=true clean package"
}
}
stage("publish"){
steps{
// 备份数据,第一次没有数据的时候可以先注释掉,之后打开就行
sh "ssh root@192.168.136.233 'tar -Jcf /opt/backup/app-\$(date +%Y%m%d).tar.xz /usr/local/tomcat/webapps/clq'"
//传war包到目标服务器上
sh "scp target/app.war root@192.168.136.233:/usr/local/tomcat/webapps/"
//重启 tomcat
sh "ssh root@192.168.136.233 '/usr/local/tomcat/bin/catalina.sh stop;sleep 3; /usr/local/tomcat/bin/catalina.sh start'"
}
}
}
}
构建运行
以上是拉取的公开的仓库,如果是要拉取私有的仓库呢
最后复制生成的连接粘贴到脚本中的git这一行命令就完工了
7.jenkins脚本语法扩展
流水线脚本扩展
简单结构语法:
stages{
stage(''){
steps{
commands
}
}
}
sh "mvn -Dmaven.test.failure.ignore=true clean package"
sh "mv target/ly-simple-tomcat-0.0.1-SNAPSHOT.war target/app.war"
sh '''
mvn -Dmaven.test.failure.ignore=true clean package
mv target/ly-simple-tomcat-0.0.1-SNAPSHOT.war target/app.war
'''
environment{
DISABLE_AUTH = 'true'
DB_ENGINE = 'sqlite'
}
stages{
stage('hello'){
steps{
echo DISABLE_AUTH
echo DB_ENGINE
}
}
}
post{
always{
deleteDir()
}
success{
echo '清理成功'
}
failure{
echo '清理失败'
}
}
post{
failure{
mail to: '主题',
subject: "Failed Pipeline: ${currentBuild.fullDisplayName}",
body: "XXX ${env.BUILD_URL}"
}
}
pipeline{
agent any
stages{
stage('Build'){
steps{
echo '命令1'
}
}
stage('Test'){
steps{
echo '命令2'
}
}
stage('Deploy') {
steps{
echo '命令3'
}
}
}
}
|