当前位置:   article > 正文

Jenkins 学习笔记_jenkins 24

jenkins 24

一、安装部署

  • 1、Jenkins 在开发过程中所属位置

  • 2、Jenkins + Maven + Git 持续集成基础使用

  • 3、安装硬件环境和知识储备

一、准备三台服务器

* Jenkins 主服务器  Jenkins 测试服务器 GitLab 代码托管服务器 

二、GitLab 安装

  • 1、GitLab 介绍及安装准备
  • 2、SSH 下安装
  • 3、 Docker 下安装

GitLab 常用命令

  1. gitlab-ctl start # 启动所有 gitlab 组件;
  2. gitlab-ctl stop # 停止所有 gitlab 组件;
  3. gitlab-ctl restart # 重启所有 gitlab 组件;
  4. gitlab-ctl status # 查看服务状态;
  5. gitlab-ctl reconfigure # 启动服务;
  6. vi /etc/gitlab/gitlab.rb # 修改默认的配置文件;
  7. gitlab-ctl tail # 查看日志;

1、SSH 安装

官方文档:GitLab最新版下载安装_GitLab中文免费版-极狐GitLab中文官方网站

2、Docker 安装

1、安装Docker:CentOS8 安装 Docker repo ‘appstream‘ 下载元数据失败_仍怀热爱的博客-CSDN博客

2、 安装GitLab 至少 6G 内存

  1. docker run --detach \
  2. --hostname 192.168.1.8 \
  3. --publish 443:443 --publish 80:80 \
  4. --name gitlab \
  5. --restart always \
  6. --volume $GITLAB_HOME/config:/etc/gitlab:Z \
  7. --volume $GITLAB_HOME/logs:/var/log/gitlab:Z \
  8. --volume $GITLAB_HOME/data:/var/opt/gitlab:Z \
  9. --shm-size 256m \
  10. registry.gitlab.cn/omnibus/gitlab-jh:latest

 3、进入 GitLab 容器

docker exec -it  gitlab /bin/bash

4、访问 GitLab 服务器地址 192.168.1.8

当首次运行出现502错误的时候排查两个原因

  1. 虚拟机内存至少需要4g
  2. 稍微再等等刷新一下可能就好了

5、管理员账号登录

用户名:root

密码存在下面文件中,登录后需要改密码不然24小时之后会失效

cat /etc/gitlab/initial_root_password

密码是 Password 后面的字符串

  1. root@192:/# cat /etc/gitlab/initial_root_password
  2. # WARNING: This value is valid only in the following conditions
  3. # 1. If provided manually (either via `GITLAB_ROOT_PASSWORD` environment variable or via `gitlab_rails['initial_root_password']` setting in `gitlab.rb`, it was provided before database was seeded for the first time (usually, the first reconfigure run).
  4. # 2. Password hasn't been changed manually, either via UI or via command line.
  5. #
  6. # If the password shown here doesn't work, you must reset the admin password following https://docs.gitlab.com/ee/security/reset_user_password.html#reset-your-root-password.
  7. Password: JLDZaPOGsPVONhL7BBZ4mgrcO6GxJuHlfReZGldE4qQ=
  8. # NOTE: This file will be automatically deleted in the first reconfigure run after 24 hours.

6、提交一份代码到GitLab

  三、Jenkins 安装

Jenkins 官方文档:Jenkins

 1、安装 Jenkins

docker pull jenkins/jenkins:latest

2、创建 Jenkins 目录

mkdir -p /root/docker/jenkins

3、安装启动 Jenkins

  1. docker run -d \
  2. -u root \
  3. --name jenkins \
  4. -p 9302:8080 \
  5. -v /root/docker/jenkins:/var/jenkins_home \
  6. -v /var/run/docker.sock:/var/run/docker.sock \
  7. jenkins/jenkins

4、查看 Jenkins 容器ID && 进入容器

  1. docker ps
  2. docker exec -u 0 -it 容器ID /bin/bash

5、查看 Jenkins 解锁密码

  1. [root@localhost ~]# cat /root/docker/jenkins/secrets/initialAdminPassword
  2. 5661515b79f24386a039659438c8c008

6、安装 Jenkins 插件 

 7、安装完成

二、安装jdk、maven、git

三、安装 jenkins 插件

 四、添加任务

 1、创建一个 jenkins 任务

2、配置仓库地址 

3、指定要拉取的分支

4、配置Maven 

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!--
  3. Licensed to the Apache Software Foundation (ASF) under one
  4. or more contributor license agreements. See the NOTICE file
  5. distributed with this work for additional information
  6. regarding copyright ownership. The ASF licenses this file
  7. to you under the Apache License, Version 2.0 (the
  8. "License"); you may not use this file except in compliance
  9. with the License. You may obtain a copy of the License at
  10. http://www.apache.org/licenses/LICENSE-2.0
  11. Unless required by applicable law or agreed to in writing,
  12. software distributed under the License is distributed on an
  13. "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. KIND, either express or implied. See the License for the
  15. specific language governing permissions and limitations
  16. under the License.
  17. -->
  18. <!--
  19. | This is the configuration file for Maven. It can be specified at two levels:
  20. |
  21. | 1. User Level. This settings.xml file provides configuration for a single user,
  22. | and is normally provided in ${user.home}/.m2/settings.xml.
  23. |
  24. | NOTE: This location can be overridden with the CLI option:
  25. |
  26. | -s /path/to/user/settings.xml
  27. |
  28. | 2. Global Level. This settings.xml file provides configuration for all Maven
  29. | users on a machine (assuming they're all using the same Maven
  30. | installation). It's normally provided in
  31. | ${maven.conf}/settings.xml.
  32. |
  33. | NOTE: This location can be overridden with the CLI option:
  34. |
  35. | -gs /path/to/global/settings.xml
  36. |
  37. | The sections in this sample file are intended to give you a running start at
  38. | getting the most out of your Maven installation. Where appropriate, the default
  39. | values (values used when the setting is not specified) are provided.
  40. |
  41. |-->
  42. <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
  43. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  44. xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
  45. <!-- localRepository
  46. | The path to the local repository maven will use to store artifacts.
  47. |
  48. | Default: ${user.home}/.m2/repository
  49. <localRepository>/path/to/local/repo</localRepository>
  50. -->
  51. <localRepository>${user.home}/.m2/repository</localRepository>
  52. <!-- interactiveMode
  53. | This will determine whether maven prompts you when it needs input. If set to false,
  54. | maven will use a sensible default value, perhaps based on some other setting, for
  55. | the parameter in question.
  56. |
  57. | Default: true
  58. <interactiveMode>true</interactiveMode>
  59. -->
  60. <!-- offline
  61. | Determines whether maven should attempt to connect to the network when executing a build.
  62. | This will have an effect on artifact downloads, artifact deployment, and others.
  63. |
  64. | Default: false
  65. <offline>false</offline>
  66. -->
  67. <!-- pluginGroups
  68. | This is a list of additional group identifiers that will be searched when resolving plugins by their prefix, i.e.
  69. | when invoking a command line like "mvn prefix:goal". Maven will automatically add the group identifiers
  70. | "org.apache.maven.plugins" and "org.codehaus.mojo" if these are not already contained in the list.
  71. |-->
  72. <pluginGroups>
  73. <!-- pluginGroup
  74. | Specifies a further group identifier to use for plugin lookup.
  75. <pluginGroup>com.your.plugins</pluginGroup>
  76. -->
  77. <pluginGroup>org.mortbay.jetty</pluginGroup>
  78. </pluginGroups>
  79. <!-- proxies
  80. | This is a list of proxies which can be used on this machine to connect to the network.
  81. | Unless otherwise specified (by system property or command-line switch), the first proxy
  82. | specification in this list marked as active will be used.
  83. |-->
  84. <proxies>
  85. <!-- proxy
  86. | Specification for one proxy, to be used in connecting to the network.
  87. |
  88. <proxy>
  89. <id>optional</id>
  90. <active>true</active>
  91. <protocol>http</protocol>
  92. <username>proxyuser</username>
  93. <password>proxypass</password>
  94. <host>proxy.host.net</host>
  95. <port>80</port>
  96. <nonProxyHosts>local.net|some.host.com</nonProxyHosts>
  97. </proxy>
  98. -->
  99. </proxies>
  100. <!-- servers
  101. | This is a list of authentication profiles, keyed by the server-id used within the system.
  102. | Authentication profiles can be used whenever maven must make a connection to a remote server.
  103. |-->
  104. <servers>
  105. <!-- server
  106. | Specifies the authentication information to use when connecting to a particular server, identified by
  107. | a unique name within the system (referred to by the 'id' attribute below).
  108. |
  109. | NOTE: You should either specify username/password OR privateKey/passphrase, since these pairings are
  110. | used together.
  111. |
  112. <server>
  113. <id>deploymentRepo</id>
  114. <username>repouser</username>
  115. <password>repopwd</password>
  116. </server>
  117. -->
  118. <!-- Another sample, using keys to authenticate.
  119. <server>
  120. <id>siteServer</id>
  121. <privateKey>/path/to/private/key</privateKey>
  122. <passphrase>optional; leave empty if not used.</passphrase>
  123. </server>
  124. -->
  125. <server>
  126. <id>releases</id>
  127. <username>ali</username>
  128. <password>ali</password>
  129. </server>
  130. <server>
  131. <id>Snapshots</id>
  132. <username>ali</username>
  133. <password>ali</password>
  134. </server>
  135. </servers>
  136. <!-- mirrors
  137. | This is a list of mirrors to be used in downloading artifacts from remote repositories.
  138. |
  139. | It works like this: a POM may declare a repository to use in resolving certain artifacts.
  140. | However, this repository may have problems with heavy traffic at times, so people have mirrored
  141. | it to several places.
  142. |
  143. | That repository definition will have a unique id, so we can create a mirror reference for that
  144. | repository, to be used as an alternate download site. The mirror site will be the preferred
  145. | server for that repository.
  146. |-->
  147. <mirrors>
  148. <!-- mirror
  149. | Specifies a repository mirror site to use instead of a given repository. The repository that
  150. | this mirror serves has an ID that matches the mirrorOf element of this mirror. IDs are used
  151. | for inheritance and direct lookup purposes, and must be unique across the set of mirrors.
  152. |
  153. <mirror>
  154. <id>mirrorId</id>
  155. <mirrorOf>repositoryId</mirrorOf>
  156. <name>Human Readable Name for this Mirror.</name>
  157. <url>http://my.repository.com/repo/path</url>
  158. </mirror>
  159. -->
  160. <mirror>
  161. <!--This sends everything else to /public -->
  162. <id>nexus</id>
  163. <mirrorOf>*</mirrorOf>
  164. <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
  165. </mirror>
  166. <mirror>
  167. <!--This is used to direct the public snapshots repo in the
  168. profile below over to a different nexus group -->
  169. <id>nexus-public-snapshots</id>
  170. <mirrorOf>public-snapshots</mirrorOf>
  171. <url>http://maven.aliyun.com/nexus/content/repositories/snapshots/</url>
  172. </mirror>
  173. <mirror>
  174. <!--This is used to direct the public snapshots repo in the
  175. profile below over to a different nexus group -->
  176. <id>nexus-public-snapshots1</id>
  177. <mirrorOf>public-snapshots1</mirrorOf>
  178. <url>https://artifacts.alfresco.com/nexus/content/repositories/public/</url>
  179. </mirror>
  180. </mirrors>
  181. <!-- profiles
  182. | This is a list of profiles which can be activated in a variety of ways, and which can modify
  183. | the build process. Profiles provided in the settings.xml are intended to provide local machine-
  184. | specific paths and repository locations which allow the build to work in the local environment.
  185. |
  186. | For example, if you have an integration testing plugin - like cactus - that needs to know where
  187. | your Tomcat instance is installed, you can provide a variable here such that the variable is
  188. | dereferenced during the build process to configure the cactus plugin.
  189. |
  190. | As noted above, profiles can be activated in a variety of ways. One way - the activeProfiles
  191. | section of this document (settings.xml) - will be discussed later. Another way essentially
  192. | relies on the detection of a system property, either matching a particular value for the property,
  193. | or merely testing its existence. Profiles can also be activated by JDK version prefix, where a
  194. | value of '1.4' might activate a profile when the build is executed on a JDK version of '1.4.2_07'.
  195. | Finally, the list of active profiles can be specified directly from the command line.
  196. |
  197. | NOTE: For profiles defined in the settings.xml, you are restricted to specifying only artifact
  198. | repositories, plugin repositories, and free-form properties to be used as configuration
  199. | variables for plugins in the POM.
  200. |
  201. |-->
  202. <profiles>
  203. <profile>
  204. <id>development</id>
  205. <repositories>
  206. <repository>
  207. <id>central</id>
  208. <url>http://central</url>
  209. <releases><enabled>true</enabled><updatePolicy>always</updatePolicy></releases>
  210. <snapshots><enabled>true</enabled><updatePolicy>always</updatePolicy></snapshots>
  211. </repository>
  212. </repositories>
  213. <pluginRepositories>
  214. <pluginRepository>
  215. <id>central</id>
  216. <url>http://central</url>
  217. <releases><enabled>true</enabled><updatePolicy>always</updatePolicy></releases>
  218. <snapshots><enabled>true</enabled><updatePolicy>always</updatePolicy></snapshots>
  219. </pluginRepository>
  220. </pluginRepositories>
  221. </profile>
  222. <profile>
  223. <!--this profile will allow snapshots to be searched when activated-->
  224. <id>public-snapshots</id>
  225. <repositories>
  226. <repository>
  227. <id>public-snapshots</id>
  228. <url>http://public-snapshots</url>
  229. <releases><enabled>false</enabled></releases>
  230. <snapshots><enabled>true</enabled><updatePolicy>always</updatePolicy></snapshots>
  231. </repository>
  232. </repositories>
  233. <pluginRepositories>
  234. <pluginRepository>
  235. <id>public-snapshots</id>
  236. <url>http://public-snapshots</url>
  237. <releases><enabled>false</enabled></releases>
  238. <snapshots><enabled>true</enabled><updatePolicy>always</updatePolicy></snapshots>
  239. </pluginRepository>
  240. </pluginRepositories>
  241. </profile>
  242. </profiles>
  243. <activeProfiles>
  244. <activeProfile>development</activeProfile>
  245. <activeProfile>public-snapshots</activeProfile>
  246. </activeProfiles>
  247. <!-- activeProfiles
  248. | List of profiles that are active for all builds.
  249. |
  250. <activeProfiles>
  251. <activeProfile>alwaysActiveProfile</activeProfile>
  252. <activeProfile>anotherAlwaysActiveProfile</activeProfile>
  253. </activeProfiles>
  254. -->
  255. </settings>

 

 

我没有把maven挂载到 Jenkins 所以有报错 重新配置一遍 

  1. docker stop 容器ID
  2. docker run --name zyljenkins \
  3. -d -p 9302:8080 \
  4. -p 50000:50000 \
  5. -v /var/jenkins_home:/var/jenkins_home \
  6. -v /usr/local/maven:/usr/local/maven \
  7. -v /usr/local/allure-2.17.2:/usr/local/allure-2.17.2 \
  8. --env JAVA_OPTS="-Xms256m -Xmx512m -Xss1024K -XX:PermSize=128m -XX:MaxPermSize=256m" jenkins/jenkins

 5、重新配置之后再次启动

6、点进去查看控制台日志 

 7、构建成功

 

五、安装远程服务器插件 Publish Over SSH

1、安装插件 

2、配置远程服务器 

3、勾选密码

 4、测试连接

5、我这里踩了一个坑把 remove 看成 remote 了 一直报错

 6、测试

 7、去测试服务器看一下 jar 包 在不在

 四、配置测试服务器

一、配置测试服务器

1、安装JRE

yum install java-1.8.0-openjdk

2、查看 JRE 版本 验证是否安装成功

  

3、我们看到文件夹特别长

 4、在配置中修改 

5、把之前的目录 rm -rf 并且在 jenkins 重新执行 

6、配置最后执行语句

 7、重新执行

8、查看测试服务器 是否运行成功 

 二、超时机制

1、优化指令

2、再次优化 简化 

3、之前的配错了!!挪过来!!

4、配置编译前执行命令

5、编写 shell 脚本

  1. #!/bin/bash
  2. # 删除历史数据
  3. rm -rf zyl
  4. #获取jar包pid
  5. pid=`ps -ef | grep 8088 | grep 'java -jar' | awk '{printf $2}'`
  6. echo $pid
  7. kill -9 $pid

 6、优化 shell 脚本 将端口号传入

7、再次优化 shell 脚本

  1. # 删除历史数据
  2. rm -rf zyl
  3. appprot=$1
  4. #获取jar包pid
  5. pid=`ps -ef | grep $1 | grep 'java -jar' | awk '{printf $2}'`
  6. echo $pid
  7. # 如果 pid 为空 提示一下 否侧运行 kill
  8. if [ -z $pid ];
  9. # 如果是空的 -z 空值判断
  10. then
  11. echo "$appprot not started"
  12. else
  13. kill -9 $pid
  14. echo "$appport stoping..."
  15. fi

8、传值

 三、提交代码测试

一、提交前

1、查看网页输出的

 2、修改 IDE 代码并提交

 3、重新构建 jenkins item

二、提交后 

1、查看网页输出的

 三、自动构建( 不适用 )

1、修改 jenkins item 配置 

2、 添加令牌

3、生成 URL :http://192.168.1.12:9302/job/first/build?token=weilekaixin

4、如果对这个 URL 发起一次 GET 请求 则 jenkins 就会帮助我们构建一次项目
5、配置插件 如果不配 GitLab 则无权访问

 6、再次生成 URL : http://192.168.1.12:9302/buildByToken/build?job=first&token=weilekaixin

 7、配置GitLab

 

 8、不支持本地请求

 9、切换管理员身份

10、选择网络 

 

 11、退回项目 重新配置

 12、配置成功

 13、发送 Push 测试一下

14、测试成功 

15、提交代码测试一下 

一、提交前

1、看提交前接口返回

 2、提交代码

二、提交后

1、jenkins 触发了事件

 2、我们跟踪一下 发现已经打包成功

 3、看提交后接口返回

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

闽ICP备14008679号