赞
踩
Jenkins是一个开源的、提供友好操作界面的持续集成(CI)工具,起源于Hudson(Hudson是商用的),主要用于持续、自动的构建/测试软件项目、监控外部任务的运行(这个比较抽象,暂且写上,不做解释)。Jenkins用Java语言编写,可在Tomcat等流行的servlet容器中运行,也可独立运行。通常与版本管理工具(SCM)、构建工具结合使用。常用的版本控制工具有SVN、GIT,构建工具有Maven、Ant、Gradle。
CI(Continuous Integration,中文意思是持续集成)是一种软件开发时间。持续集成强调开发人员提交了新代码之后,立刻进行构建、(单元)测试。根据测试结果,我们可以确定新代码和原有代码能否正确地集成在一起。。
CD(Continuous Delivery, 中文意思持续交付)是在持续集成的基础上,将集成后的代码部署到更贴近真实运行环境(类生产环境)中。比如,我们完成单元测试后,可以把代码部署到连接数据库的Staging环境中更多的测试。如果代码没有问题,可以继续手动部署到生产环境。
在/usr/local/software/下创建maven文件夹
mkdir maven
我使用maven3.6.3版本,并创建子文件夹maven-repository存储依赖
[root@localhost apache-maven-3.6.3]# ll
总用量 36
drwxr-xr-x. 2 root root 97 11月 9 10:08 bin
drwxr-xr-x. 2 root root 76 11月 9 10:08 boot
drwxr-xr-x. 3 root root 92 11月 9 10:10 conf
drwxr-xr-x. 4 root root 4096 11月 9 10:08 lib
-rw-r--r--. 1 root root 17504 11月 9 10:08 LICENSE
drwxr-xr-x. 2 root root 6 11月 9 15:41 maven-repository
-rw-r--r--. 1 root root 5141 11月 9 10:08 NOTICE
-rw-r--r--. 1 root root 2612 11月 9 10:08 README.txt
进入/usr/local/software/maven/apache-maven-3.6.3/conf 修改settings.xml。 修改存储依赖的位置。
<localRepository>../maven-repository</localRepository>
编辑/etc/profile文件
M2_HOME=/usr/local/software/maven/apache-maven-3.6.3
export PATH=$PATH:${M2_HOME}/bin
让编辑生效
source /etc/profile
mvn -version
[root@localhost conf]# mvn -version
-bash: /usr/local/software/apache-maven-3.6.3/bin/mvn: 权限不够
解决方案: chmod a+x /usr/local/software/apache-maven-3.6.3/bin/mvn
[root@localhost conf]# chmod a+x /usr/local/software/apache-maven-3.6.3/bin/mvn
[root@localhost apache-maven-3.6.3]# mvn -version
Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f)
Maven home: /usr/local/software/maven/apache-maven-3.6.3
Java version: 1.8.0_341, vendor: Oracle Corporation, runtime: /usr/local/software/jdk/jdk1.8.0_341/jre
Default locale: zh_CN, platform encoding: UTF-8
OS name: "linux", version: "3.10.0-1160.99.1.el7.x86_64", arch: "amd64", family: "unix"
查询jenkins
docker search jenkinsci/blueocean
docker pull jenkinsci/blueocean
在jenkins文件夹中创建 data和jenkins_home
在jekins_home下创建docker.sock文件,并授权777
[root@localhost software]# mkdir jenkins
[root@localhost jenkins]# mkdir data jenkins_home
[root@localhost jenkins]# ls
data jenkins_home
[root@localhost jenkins]# cd jenkins_home/
[root@localhost jenkins_home]# touch docker.sock
[root@localhost jenkins_home]# chmod 777 docker.sock
[root@localhost jenkins_home]# tree
.
└── docker.sock
docker run -it \
-u root \
--name jenkins \
--privileged \
-p 9055:8080 \
-p 50000:50000 \
--network wn_docker_net \
--ip 172.18.12.60 \
-v /etc/localtime:/etc/localtime \
-v /usr/local/software/jenkins/data:/var/jenkins_home \
-v /usr/local/software/jenkins/jenkins_home/docker.sock:/var/run/docker.sock \
-v /usr/local/software/apache-maven-3.6.3:/usr/local/maven \
-d jenkinsci/blueocean
[root@localhost jenkins]# firewall-cmd --zone=public --add-port=8087/tcp --permanent
success
[root@localhost jenkins]# firewall-cmd --zone=public --add-port=50000/tcp --permanent
success
[root@localhost jenkins]# firewall-cmd --reload
success
输入请求地址:http://192.168.201.75:8087
data文件夹映射容器的 /home/jenkins_home
进入secrets文件夹,显示initialAdminPassword。
点击继续
后台手动重启
[root@localhost jenkins]# docker restart jenkins
重启jenkins
设置清华的镜像源: https://mirrors.tuna.tsinghua.edu.cn/jenkins/updates/update-center.json
重启jenkins容器
docker restart jenkins
配置用户凭证是为了将来gitlab访问jenkins。
选择stores from parent
创建一个springboot项目,使用jenkins+gitlab。
在pom.xml中修改
<groupId>com.wnhz.bms</groupId>
<artifactId>wnhz-bms</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>wnhz-bms</name>
<description>使用jenkins构建项目</description>
error: unable to read askpass response from ‘C:\Users\Administrator\AppData\Local\JetBrains\IntelliJIdea2021.3\tmp\intellij-git-askpass-local.sh’
bash: line 1: /dev/tty: No such device or address
解决方案:需要修改git的remote用户名密码
再次提交
gitlab
总览如下图:
点击#1进入构建详情
使用gitlab hook插件完成gitlab上有提交jenkins的自动构建过程。
取消认证
点击Admin,设置Network 的Outbound requests设置的选中。
退出管理员
普通用户登录(我的是test),点击项目->settings->Webhooks
由于项目采用多级目录,我们将打包后的结果统一输出到某个目录,暂时定为out。在项目打包插件中添加打包位置。
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<outputDirectory>../../out</outputDirectory>
</configuration>
</plugin>
</plugins>
</build>
点击项目设置post steps
linux服务器
[root@localhost projects]# nohup java -jar wnhz-bms-0.0.1-SNAPSHOT.jar & [1] 67855 [root@localhost projects]# nohup: 忽略输入并把输出追加到"nohup.out" [root@localhost projects]# ls nohup.out wnhz-bms-0.0.1-SNAPSHOT.jar [root@localhost projects]cat nohup.out /^\ /^\ { O} { O} \ / \ / // // _------_ // // ./~ ~-_ / ~----~/ / \ / : ./ _---_ ~- | \________) : /~ ~\ | | / | | :~~\ | | | | | | \___-~ | | \ __/`^\______\. ./ \ ~-______-~\. .| ~-_ /_________蜗牛杭州Jenkins测试__________~~ 2023-11-10 17:15:43.926 INFO 28907 --- [ main] com.wnhz.bms.BmsApp : Starting BmsApp v0.0.1-SNAPSHOT using Java 1.8.0_341 on localhost.localdomain with PID 28907 (/usr/local/projects/wnhz-bms-0.0.1-SNAPSHOT.jar started by root in /usr/local/projects) 2023-11-10 17:15:44.062 INFO 28907 --- [ main] com.wnhz.bms.BmsApp : The following 1 profile is active: "dev" 2023-11-10 17:15:47.587 INFO 28907 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 9090 (http) 2023-11-10 17:15:47.610 INFO 28907 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat] 2023-11-10 17:15:47.611 INFO 28907 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.68] 2023-11-10 17:15:47.860 INFO 28907 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2023-11-10 17:15:47.861 INFO 28907 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 3472 ms 2023-11-10 17:15:49.326 INFO 28907 --- [ main] pertySourcedRequestMappingHandlerMapping : Mapped URL path [/v2/api-docs] onto method [springfox.documentation.swagger2.web.Swagger2ControllerWebMvc#getDocumentation(String, HttpServletRequest)] 2023-11-10 17:15:49.475 INFO 28907 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 9090 (http) with context path '' 2023-11-10 17:15:49.477 INFO 28907 --- [ main] d.s.w.p.DocumentationPluginsBootstrapper : Documentation plugins bootstrapped 2023-11-10 17:15:49.486 INFO 28907 --- [ main] d.s.w.p.DocumentationPluginsBootstrapper : Found 1 custom documentation plugin(s) 2023-11-10 17:15:49.558 INFO 28907 --- [ main] s.d.s.w.s.ApiListingReferenceScanner : Scanning for api listing references 2023-11-10 17:15:49.887 INFO 28907 --- [ main] com.wnhz.bms.BmsApp : Started BmsApp in 8.873 seconds (JVM running for 10.226) 2023-11-10 17:16:19.245 INFO 28907 --- [nio-9090-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet' 2023-11-10 17:16:19.245 INFO 28907 --- [nio-9090-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet' 2023-11-10 17:16:19.247 INFO 28907 --- [nio-9090-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 1 ms
swagger测试
关闭应用
[root@localhost projects]# ps aux|grep 67855
root 67855 8.8 3.9 5856660 318416 pts/4 Sl 17:31 0:18 java -jar wnhz-bms-0.0.1-SNAPSHOT.jar
root 76563 0.0 0.0 112824 988 pts/4 S+ 17:34 0:00 grep --color=auto 67855
[root@localhost projects]# kill -9 67855
[root@localhost projects]# ps aux|grep 67855
root 79602 0.0 0.0 112824 984 pts/4 S+ 17:35 0:00 grep --color=auto 67855
[1]+ 已杀死 nohup java -jar wnhz-bms-0.0.1-SNAPSHOT.jar
g-TG9kGPpx-1713174351305)]
关闭应用
[外链图片转存中…(img-19K8gVWG-1713174351306)]
[root@localhost projects]# ps aux|grep 67855
root 67855 8.8 3.9 5856660 318416 pts/4 Sl 17:31 0:18 java -jar wnhz-bms-0.0.1-SNAPSHOT.jar
root 76563 0.0 0.0 112824 988 pts/4 S+ 17:34 0:00 grep --color=auto 67855
[root@localhost projects]# kill -9 67855
[root@localhost projects]# ps aux|grep 67855
root 79602 0.0 0.0 112824 984 pts/4 S+ 17:35 0:00 grep --color=auto 67855
[1]+ 已杀死 nohup java -jar wnhz-bms-0.0.1-SNAPSHOT.jar
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。