赞
踩
1、Jenkins 在开发过程中所属位置
2、Jenkins + Maven + Git 持续集成基础使用
3、安装硬件环境和知识储备
* Jenkins 主服务器 Jenkins 测试服务器 GitLab 代码托管服务器
GitLab 常用命令
- gitlab-ctl start # 启动所有 gitlab 组件;
- gitlab-ctl stop # 停止所有 gitlab 组件;
- gitlab-ctl restart # 重启所有 gitlab 组件;
- gitlab-ctl status # 查看服务状态;
- gitlab-ctl reconfigure # 启动服务;
- vi /etc/gitlab/gitlab.rb # 修改默认的配置文件;
- gitlab-ctl tail # 查看日志;
官方文档:GitLab最新版下载安装_GitLab中文免费版-极狐GitLab中文官方网站
1、安装Docker:CentOS8 安装 Docker repo ‘appstream‘ 下载元数据失败_仍怀热爱的博客-CSDN博客
2、 安装GitLab 至少 6G 内存
- docker run --detach \
- --hostname 192.168.1.8 \
- --publish 443:443 --publish 80:80 \
- --name gitlab \
- --restart always \
- --volume $GITLAB_HOME/config:/etc/gitlab:Z \
- --volume $GITLAB_HOME/logs:/var/log/gitlab:Z \
- --volume $GITLAB_HOME/data:/var/opt/gitlab:Z \
- --shm-size 256m \
- registry.gitlab.cn/omnibus/gitlab-jh:latest
3、进入 GitLab 容器
docker exec -it gitlab /bin/bash
4、访问 GitLab 服务器地址 192.168.1.8
当首次运行出现502错误的时候排查两个原因
5、管理员账号登录
用户名:root
密码存在下面文件中,登录后需要改密码不然24小时之后会失效
cat /etc/gitlab/initial_root_password
密码是 Password 后面的字符串
- root@192:/# cat /etc/gitlab/initial_root_password
- # WARNING: This value is valid only in the following conditions
- # 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).
- # 2. Password hasn't been changed manually, either via UI or via command line.
- #
- # 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.
-
- Password: JLDZaPOGsPVONhL7BBZ4mgrcO6GxJuHlfReZGldE4qQ=
-
- # NOTE: This file will be automatically deleted in the first reconfigure run after 24 hours.
6、提交一份代码到GitLab
Jenkins 官方文档:Jenkins
1、安装 Jenkins
docker pull jenkins/jenkins:latest
2、创建 Jenkins 目录
mkdir -p /root/docker/jenkins
3、安装启动 Jenkins
- docker run -d \
- -u root \
- --name jenkins \
- -p 9302:8080 \
- -v /root/docker/jenkins:/var/jenkins_home \
- -v /var/run/docker.sock:/var/run/docker.sock \
- jenkins/jenkins
4、查看 Jenkins 容器ID && 进入容器
- docker ps
- docker exec -u 0 -it 容器ID /bin/bash
5、查看 Jenkins 解锁密码
- [root@localhost ~]# cat /root/docker/jenkins/secrets/initialAdminPassword
- 5661515b79f24386a039659438c8c008
6、安装 Jenkins 插件
7、安装完成
二、安装jdk、maven、git
三、安装 jenkins 插件
四、添加任务
1、创建一个 jenkins 任务
2、配置仓库地址
3、指定要拉取的分支
4、配置Maven
- <?xml version="1.0" encoding="UTF-8"?>
-
- <!--
- Licensed to the Apache Software Foundation (ASF) under one
- or more contributor license agreements. See the NOTICE file
- distributed with this work for additional information
- regarding copyright ownership. The ASF licenses this file
- to you under the Apache License, Version 2.0 (the
- "License"); you may not use this file except in compliance
- with the License. You may obtain a copy of the License at
- http://www.apache.org/licenses/LICENSE-2.0
- Unless required by applicable law or agreed to in writing,
- software distributed under the License is distributed on an
- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- KIND, either express or implied. See the License for the
- specific language governing permissions and limitations
- under the License.
- -->
-
- <!--
- | This is the configuration file for Maven. It can be specified at two levels:
- |
- | 1. User Level. This settings.xml file provides configuration for a single user,
- | and is normally provided in ${user.home}/.m2/settings.xml.
- |
- | NOTE: This location can be overridden with the CLI option:
- |
- | -s /path/to/user/settings.xml
- |
- | 2. Global Level. This settings.xml file provides configuration for all Maven
- | users on a machine (assuming they're all using the same Maven
- | installation). It's normally provided in
- | ${maven.conf}/settings.xml.
- |
- | NOTE: This location can be overridden with the CLI option:
- |
- | -gs /path/to/global/settings.xml
- |
- | The sections in this sample file are intended to give you a running start at
- | getting the most out of your Maven installation. Where appropriate, the default
- | values (values used when the setting is not specified) are provided.
- |
- |-->
- <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
- <!-- localRepository
- | The path to the local repository maven will use to store artifacts.
- |
- | Default: ${user.home}/.m2/repository
- <localRepository>/path/to/local/repo</localRepository>
- -->
- <localRepository>${user.home}/.m2/repository</localRepository>
- <!-- interactiveMode
- | This will determine whether maven prompts you when it needs input. If set to false,
- | maven will use a sensible default value, perhaps based on some other setting, for
- | the parameter in question.
- |
- | Default: true
- <interactiveMode>true</interactiveMode>
- -->
-
- <!-- offline
- | Determines whether maven should attempt to connect to the network when executing a build.
- | This will have an effect on artifact downloads, artifact deployment, and others.
- |
- | Default: false
- <offline>false</offline>
- -->
-
- <!-- pluginGroups
- | This is a list of additional group identifiers that will be searched when resolving plugins by their prefix, i.e.
- | when invoking a command line like "mvn prefix:goal". Maven will automatically add the group identifiers
- | "org.apache.maven.plugins" and "org.codehaus.mojo" if these are not already contained in the list.
- |-->
- <pluginGroups>
- <!-- pluginGroup
- | Specifies a further group identifier to use for plugin lookup.
- <pluginGroup>com.your.plugins</pluginGroup>
- -->
- <pluginGroup>org.mortbay.jetty</pluginGroup>
- </pluginGroups>
-
- <!-- proxies
- | This is a list of proxies which can be used on this machine to connect to the network.
- | Unless otherwise specified (by system property or command-line switch), the first proxy
- | specification in this list marked as active will be used.
- |-->
- <proxies>
- <!-- proxy
- | Specification for one proxy, to be used in connecting to the network.
- |
- <proxy>
- <id>optional</id>
- <active>true</active>
- <protocol>http</protocol>
- <username>proxyuser</username>
- <password>proxypass</password>
- <host>proxy.host.net</host>
- <port>80</port>
- <nonProxyHosts>local.net|some.host.com</nonProxyHosts>
- </proxy>
- -->
- </proxies>
-
- <!-- servers
- | This is a list of authentication profiles, keyed by the server-id used within the system.
- | Authentication profiles can be used whenever maven must make a connection to a remote server.
- |-->
- <servers>
- <!-- server
- | Specifies the authentication information to use when connecting to a particular server, identified by
- | a unique name within the system (referred to by the 'id' attribute below).
- |
- | NOTE: You should either specify username/password OR privateKey/passphrase, since these pairings are
- | used together.
- |
- <server>
- <id>deploymentRepo</id>
- <username>repouser</username>
- <password>repopwd</password>
- </server>
- -->
-
- <!-- Another sample, using keys to authenticate.
- <server>
- <id>siteServer</id>
- <privateKey>/path/to/private/key</privateKey>
- <passphrase>optional; leave empty if not used.</passphrase>
- </server>
- -->
- <server>
- <id>releases</id>
- <username>ali</username>
- <password>ali</password>
- </server>
- <server>
- <id>Snapshots</id>
- <username>ali</username>
- <password>ali</password>
- </server>
- </servers>
-
- <!-- mirrors
- | This is a list of mirrors to be used in downloading artifacts from remote repositories.
- |
- | It works like this: a POM may declare a repository to use in resolving certain artifacts.
- | However, this repository may have problems with heavy traffic at times, so people have mirrored
- | it to several places.
- |
- | That repository definition will have a unique id, so we can create a mirror reference for that
- | repository, to be used as an alternate download site. The mirror site will be the preferred
- | server for that repository.
- |-->
- <mirrors>
- <!-- mirror
- | Specifies a repository mirror site to use instead of a given repository. The repository that
- | this mirror serves has an ID that matches the mirrorOf element of this mirror. IDs are used
- | for inheritance and direct lookup purposes, and must be unique across the set of mirrors.
- |
- <mirror>
- <id>mirrorId</id>
- <mirrorOf>repositoryId</mirrorOf>
- <name>Human Readable Name for this Mirror.</name>
- <url>http://my.repository.com/repo/path</url>
- </mirror>
- -->
- <mirror>
- <!--This sends everything else to /public -->
- <id>nexus</id>
- <mirrorOf>*</mirrorOf>
- <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
- </mirror>
- <mirror>
- <!--This is used to direct the public snapshots repo in the
- profile below over to a different nexus group -->
- <id>nexus-public-snapshots</id>
- <mirrorOf>public-snapshots</mirrorOf>
- <url>http://maven.aliyun.com/nexus/content/repositories/snapshots/</url>
- </mirror>
- <mirror>
- <!--This is used to direct the public snapshots repo in the
- profile below over to a different nexus group -->
- <id>nexus-public-snapshots1</id>
- <mirrorOf>public-snapshots1</mirrorOf>
- <url>https://artifacts.alfresco.com/nexus/content/repositories/public/</url>
- </mirror>
- </mirrors>
-
- <!-- profiles
- | This is a list of profiles which can be activated in a variety of ways, and which can modify
- | the build process. Profiles provided in the settings.xml are intended to provide local machine-
- | specific paths and repository locations which allow the build to work in the local environment.
- |
- | For example, if you have an integration testing plugin - like cactus - that needs to know where
- | your Tomcat instance is installed, you can provide a variable here such that the variable is
- | dereferenced during the build process to configure the cactus plugin.
- |
- | As noted above, profiles can be activated in a variety of ways. One way - the activeProfiles
- | section of this document (settings.xml) - will be discussed later. Another way essentially
- | relies on the detection of a system property, either matching a particular value for the property,
- | or merely testing its existence. Profiles can also be activated by JDK version prefix, where a
- | value of '1.4' might activate a profile when the build is executed on a JDK version of '1.4.2_07'.
- | Finally, the list of active profiles can be specified directly from the command line.
- |
- | NOTE: For profiles defined in the settings.xml, you are restricted to specifying only artifact
- | repositories, plugin repositories, and free-form properties to be used as configuration
- | variables for plugins in the POM.
- |
- |-->
- <profiles>
- <profile>
- <id>development</id>
- <repositories>
- <repository>
- <id>central</id>
- <url>http://central</url>
- <releases><enabled>true</enabled><updatePolicy>always</updatePolicy></releases>
- <snapshots><enabled>true</enabled><updatePolicy>always</updatePolicy></snapshots>
- </repository>
- </repositories>
- <pluginRepositories>
- <pluginRepository>
- <id>central</id>
- <url>http://central</url>
- <releases><enabled>true</enabled><updatePolicy>always</updatePolicy></releases>
- <snapshots><enabled>true</enabled><updatePolicy>always</updatePolicy></snapshots>
- </pluginRepository>
- </pluginRepositories>
- </profile>
- <profile>
- <!--this profile will allow snapshots to be searched when activated-->
- <id>public-snapshots</id>
- <repositories>
- <repository>
- <id>public-snapshots</id>
- <url>http://public-snapshots</url>
- <releases><enabled>false</enabled></releases>
- <snapshots><enabled>true</enabled><updatePolicy>always</updatePolicy></snapshots>
- </repository>
- </repositories>
- <pluginRepositories>
- <pluginRepository>
- <id>public-snapshots</id>
- <url>http://public-snapshots</url>
- <releases><enabled>false</enabled></releases>
- <snapshots><enabled>true</enabled><updatePolicy>always</updatePolicy></snapshots>
- </pluginRepository>
- </pluginRepositories>
- </profile>
- </profiles>
-
- <activeProfiles>
- <activeProfile>development</activeProfile>
- <activeProfile>public-snapshots</activeProfile>
- </activeProfiles>
-
- <!-- activeProfiles
- | List of profiles that are active for all builds.
- |
- <activeProfiles>
- <activeProfile>alwaysActiveProfile</activeProfile>
- <activeProfile>anotherAlwaysActiveProfile</activeProfile>
- </activeProfiles>
- -->
- </settings>
我没有把maven挂载到 Jenkins 所以有报错 重新配置一遍
- docker stop 容器ID
- docker run --name zyljenkins \
- -d -p 9302:8080 \
- -p 50000:50000 \
- -v /var/jenkins_home:/var/jenkins_home \
- -v /usr/local/maven:/usr/local/maven \
- -v /usr/local/allure-2.17.2:/usr/local/allure-2.17.2 \
- --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 脚本
- #!/bin/bash
-
- # 删除历史数据
- rm -rf zyl
- #获取jar包pid
- pid=`ps -ef | grep 8088 | grep 'java -jar' | awk '{printf $2}'`
-
- echo $pid
-
- kill -9 $pid
6、优化 shell 脚本 将端口号传入
7、再次优化 shell 脚本
# 删除历史数据 rm -rf zyl appprot=$1 #获取jar包pid pid=`ps -ef | grep $1 | grep 'java -jar' | awk '{printf $2}'` echo $pid # 如果 pid 为空 提示一下 否侧运行 kill if [ -z $pid ]; # 如果是空的 -z 空值判断 then echo "$appprot not started" else kill -9 $pid echo "$appport stoping..." 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、看提交后接口返回
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。