#########################################jenkins部署#################################3

一、jenkins是什么?

Jenkins 是一个可扩展的持续集成引擎。

主要用于:

  • l 持续、自动地构建/测试软件项目。
  • l 监控一些定时执行的任务。

Jenkins拥有的特性包括:

  • l 易于安装-只要把jenkins.war部署到servlet容器,不需要数据库支持。
  • l 易于配置-所有配置都是通过其提供的web界面实现。
  • l 集成RSS/E-mail通过RSS发布构建结果或当构建完成时通过e-mail通知。
  • l 生成JUnit/TestNG测试报告。
  • l 分布式构建支持Jenkins能够让多台计算机一起构建/测试。
  • l 文件识别:Jenkins能够跟踪哪次构建生成哪些jar,哪次构建使用哪个版本的jar等。
  • l 插件支持:支持扩展插件,你可以开发适合自己团队使用的工具

部署规划

环境部署

jenkins安装:

登陆172.16.204.130

安装JDK

脚本安装:

JDK.sh (JDK安装路径需要知道,后期jenkins后台配置会用到)

#!/bin/bash
###一键安装jdk1.###
#安装jdk
mkdir -p /usr/local/java
cat <<EOF >> /etc/profile
export JAVA_HOME=/usr/local/java/jdk1..0_171
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export PATH=$PATH:/usr/local/java/jdk1..0_171/bin
EOF
tar -zxvf jdk-8u171-linux-x64.tar.gz -C /usr/local/java
source /etc/profile
java -version
if [ $? -ne ]; then
echo "安装失败并退出"
exit
else
echo "***jdk安装完成***"
fi
source /etc/profile

GIT安装

yum -y install git

maven安装

cd /opt

wget http://mirrors.tuna.tsinghua.edu.cn/apache/maven/maven-3/3.6.1/binaries/apache-maven-3.6.1-bin.tar.gz

tar -zxvf  apache-maven-3.6.1-bin.tar.gz

cd /opt/apache-maven-3.6.1/conf

vi /etc/profile

MAVEN_HOME=/opt/apache-maven-3.6.1

vim  settings.xml

<?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:
|
| . 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
|
| . 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>/opt/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>
-->
</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></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>
-->
</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>
<id>nexus repository</id>
<name>intranet public groups</name>
<url>http://172.16.66.34:8081/nexus/content/groups/public/</url>
<mirrorOf>central</mirrorOf>
</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
| Specifies a set of introductions to the build process, to be activated using one or more of the
| mechanisms described above. For inheritance purposes, and to activate profiles via <activatedProfiles/>
| or the command line, profiles have to have an ID that is unique.
|
| An encouraged best practice for profile identification is to use a consistent naming convention
| for profiles, such as 'env-dev', 'env-test', 'env-production', 'user-jdcasey', 'user-brett', etc.
| This will make it more intuitive to understand what the set of introduced profiles is attempting
| to accomplish, particularly when you only have a list of profile id's for debug.
|
| This profile example uses the JDK version to trigger activation, and provides a JDK-specific repo.
<profile>
<id>jdk-1.4</id> <activation>
<jdk>1.4</jdk>
</activation> <repositories>
<repository>
<id>jdk14</id>
<name>Repository for JDK 1.4 builds</name>
<url>http://www.myhost.com/maven/jdk14</url>
<layout>default</layout>
<snapshotPolicy>always</snapshotPolicy>
</repository>
</repositories>
</profile>
--> <!--
| Here is another profile, activated by the system property 'target-env' with a value of 'dev',
| which provides a specific path to the Tomcat instance. To use this, your plugin configuration
| might hypothetically look like:
|
| ...
| <plugin>
| <groupId>org.myco.myplugins</groupId>
| <artifactId>myplugin</artifactId>
|
| <configuration>
| <tomcatLocation>${tomcatPath}</tomcatLocation>
| </configuration>
| </plugin>
| ...
|
| NOTE: If you just wanted to inject this configuration whenever someone set 'target-env' to
| anything, you could just leave off the <value/> inside the activation-property.
|
<profile>
<id>env-dev</id> <activation>
<property>
<name>target-env</name>
<value>dev</value>
</property>
</activation> <properties>
<tomcatPath>/path/to/tomcat/instance</tomcatPath>
</properties>
</profile>
-->
</profiles> <!-- activeProfiles
| List of profiles that are active for all builds.
|
<activeProfiles>
<activeProfile>alwaysActiveProfile</activeProfile>
<activeProfile>anotherAlwaysActiveProfile</activeProfile>
</activeProfiles>
-->
</settings>

配置文件需要添加的部分

nexus相关配置

本地库路径

jenkins服务部署

安装tomcat

下载tomcat安装包

tar -zxvf apache-tomcat-8.5.31.tar.gz -C /opt

mv apache-tomcat-8.5.31 tomcat

cd /opt/tomcat/webapps

rm -rf *

将jenkins.war放到webapps中,jenkins安装包去官网自己下载

mv jenkins.war /opt/tomcat/webapps

启动tomcat服务

cd /opt/tomcat/bin

./startup.sh

##关闭selinux和firewalld防火墙

这个时候就可以通过浏览器访问jenkins页面了

http://172.16.204.130/jenkins/

第一次进入Jenkins会让你走几个步骤 
- 输入管理员密码,密码可以从页面提示的文件中看到 
- 下载默认插件,点击官方推荐的按钮继续往下走 
- 设置账号密码和邮箱地址 
- 登入

一顿操作,我们就来到了Jenkins的Dashboard页面

########################################Jenkins部署完成#######################################################

Gitlab安装配置及旧git项目的导入

172.16.204.131部署gitlab

官网centos7 gitlab的安装方法:https://about.gitlab.com/install/#centos-7

gitlab使用方法:https://www.centos.bz/2018/05/gitlab%E6%90%AD%E5%BB%BA%E4%B8%8E%E5%9F%BA%E6%9C%AC%E4%BD%BF%E7%94%A8/

1. 安装GitLab

有的教程还会安装postfix来发送通知邮件,不过这得需要你有服务器的外部DNS名称,我试了3次都没设置成功,所有建议你别安装了,等后面再配置外部SMTP服务器,这个操作成功率比较高。

1.1 添加GitLab包库

这里屏幕上也会输出一堆东西,最后看到 The repository is setup! You can now install packages. 就说明成功了

curl https://packages.gitlab.com/install/repositories/gitlab/gitlab-ee/script.rpm.sh | sudo bash
 
 

1.2 安装GitLab

这里也会输出一堆东西,你会看到有下载进度,和安装进度,最后安装完成后屏幕上会输出 GitLab 的 logo

sudo yum install -y gitlab-ee
 
 

3. 配置并启动GitLab

如果服务器配置垃圾的话,这里会花费大量时间,可能会等半个小时左右吧,所以耐心等待,不要以为它坏了。最后输出 gitlab Reconfigured! 就代表成功了。

sudo gitlab-ctl reconfigure
 
 

4. 可以使用啦

打开一个浏览器,输入你安装GitLab的服务器IP地址,就会打开一个网页。

第一次进入会让你设置密码

3. Gitlab管理

我需要实现的目标是

  1. 因为是内部团队使用,所以取消注册功能

     
    image.png
  2. 新用户缺省不允许创建Project

     
    image.png
  3. API创建用户,
    可能需要创建的用户比较多,同时需要取消邮件确认和创建group权限。可以考虑通过API来创建,命令如下:

//如果在win10的powershell下运行,需要先运行这句
Remove-item alias:curl
curl --header "PRIVATE-TOKEN: XtgossbudeEerKMbhy" --data "password=badfe@12312&email=xxx@qq.com&username=cengh&name=%E7%8E%8B%E8%80%81%E4%BA%8C&can_create_group=false&skip_confirmation=true" "http://外网ip:8888/api/v4/users"
  • PRIVATE-TOKEN参数是指有权限创建用户的管理员账号的一个token,在自己的账号setting上能创建和获取到。

     
    image.png
  • password最好复杂一下,不能太简单。否则会有错误
  • name如果是中文,需要转码,否则也有错误

     
    image.png
  • can_create_group=false:表示禁止创建组
  • skip_confirmation=true:表示跳过邮件确认

更多参数可以参考Gitlab的API文档

4. 导出旧项目然后导入到Gitlab

如果只把源码导出再导入,那么所有分支包括分支的所有历史记录都没有了。可以通过以下命令来实现把项目respository完全镜像clone下来,包括所有分支和记录。

git clone --bare git://github.com/username/project.git

下载下来的目录不是源代码,而是类似以下的目录结构:

 
image.png

最后在gitlbab上先创建一个新的respository,然后命令行进入到以上的目录下,执行git push命令把旧的镜像推送到新的gitlab上的respository里。

cd project.git
git push --mirror git@你的gitlab的ip/newproject.git

作者:voxer
链接:https://www.jianshu.com/p/0cd62d826114
来源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

##########################################gitlab安装完成##########################################################
tomcat部署

172.16.204.131安装tomcat

下载tomcat安装包

tar -zxvf apache-tomcat-8.5.31.tar.gz -C /opt

mv apache-tomcat-8.5.31 tomcat

cd /opt/tomcat/webapps

rm -rf *

将jenkins.war放到webapps中,jenkins安装包去官网自己下载

mv jenkins.war /opt/tomcat/webapps

启动tomcat服务

cd /opt/tomcat/bin

./startup.sh

##关闭selinux和firewalld防火墙

##################################tomcat部署完成###############################

基础环境已就位,开始配置jenkins页面喽

jenkins部署项目到远程服务器

配置jenkins服务器到远程服务器的免秘钥登陆,这里可以网上看下,so easy!

配置jenkins页面:

系统管理---插件管理(第一次登陆的时候可以默认安装所有插件,根据需求在安装相应的插件)

系统管理 ---系统设置

系统管理---全局工具配置

基础配置完成:开始构建一个新的java项目并远程发布到服务器

ok  基本部署完成,测试部署

注意:例如有多套环境,测试 开发  生产,他们对应的中间件和数据库有可能不是同一个,这时候就需要在部署的时候修改配置文件,修改方法:可以将配置文件保存到某个目录,

使用脚本jenkins放到远程服务器的jar包或者war包解压,将配置文件覆盖,最后发包部署来实现配置文件的修改

粗糙脚本参考:(根据自己的实际情况来修改脚本)

#!/bin/bash
export JAVA_HOME=/usr/local/java/jdk1..0_171
BaseDir=/opt/tomcat/webapps
FaBan=/opt/tomcat/faban
Remotedirectory=/opt/dianelian
PackageName=purchase_mall.war
BackUp="/opt/backup/`date +'%Y%m%d%H%M%S'`"
PID=`ps -ef |grep tomcat |grep -v grep |awk '{print $2}'`
num=`ps -ef |grep tomcat |grep -v grep |awk '{print $2}'|wc -l`
if [ $num -ge "" ]
then
kill - $PID
fi
if [ ! -d "$BackUp" ]
then
mkdir -p "$BackUp"
fi
mv $BaseDir/* $BackUp
mv $FaBan/* $BaseDir
sh /opt/tomcat/bin/startup.sh
 

jenkins部署java项目的更多相关文章

  1. jenkins部署java项目,脚本文件放在远程仓库中 和jar一起打包(六)

    jenkins部署java项目到远程linux上,脚本文件和项目一起上传到gogs上,直接执行gogs上的脚本文件来执行项目 (1)新建maven项目 pom.xml的配置 <project x ...

  2. 通过Jenkins部署java项目

    部署java项目-创建私有仓库 Jenkins大多数情况下都是用来部署Java项目,Java项目有一个特点是需要编译和打包的,一般情况下编译和打包都是用maven完成,所以系统环境中需要安装maven ...

  3. git jenkins 部署java项目

    1.Java项目部署基本概述:  1.什么是Java项目?  2.为什么Java项目需要使用Maven编译?  3.手动实现Java项目构建?  4.手动实现Java项目架构图? 源码包   jar包 ...

  4. Win10搭建Jenkins部署Java项目(本机和远程Win10部署)

    目录 一.前言&背景 二.环境准备 三.插件安装 四.全局配置 Maven JDK Git Maven 五.新建项目并配置 新建项目 配置 1.Discard old builds 2.Thi ...

  5. Docker使用jenkins部署java项目到远程linux(三)

    实现功能:从本地提交代码到gogs上,本地的代码会被检测一遍 如果检测通过才能commit成功 然后可以继续执行push命令 .push后在gogs上使用web钩子自动推送到jenkins触发构建,j ...

  6. jenkins部署java项目到远程linux(四)

    (1)新建java项目(maven构建) pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:x ...

  7. jenkins部署java项目在本地(三)

    (1)新建maven构建的java项目 pom.xml的配置 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns ...

  8. jenkins部署java项目(五)

    一.web server安装jdk+tomcat其中jdk可以为openjdk,版本1.8 1.1 安装jdk环境 方式一:直接使用yum安装openjdk # * 方式二:本地安装在oracle官网 ...

  9. jenkins部署web项目到webogic

    注: Docker容器里安装的jenkins,Docker容易里安装的weblogic 在jenkins里面直接将打包好的 war 包通过 Publish over SSH插件 放入 weblogic ...

随机推荐

  1. 干货分享!Oracle 的入门到精通 ~

    Oracle Database,又名Oracle RDBMS,或简称Oracle.是甲骨文公司的一款关系数据库管理系统.它是在数据库领域一直处于领先地位的产品.可以说Oracle数据库系统是目前世界上 ...

  2. Vue实例:vue2.0+ElementUI框架开发pc项目

    开发前准备 vue.js2.0中文,项目所使用的js框架 vue-router,vue.js配套路由 vuex,状态管理 Element,UI框架 1,根据官方指引,构建项目框架 安装vue npm ...

  3. python time,calendar,datetime

    time sleep:休眠指定的秒数(可以是小数) localtime:将一个时间戳转换为time.struct_time类型的对象(类似于元组) # 将一个时间戳转换为一个类似于元组的对象,不指定时 ...

  4. 基于Zabbix 3.2.6版本的low-level-discover(lld)

    个人使用理解:      1.使用一个返回值是JSON的KEY,在Templates或者Hosts中创建一个Discovery规则.该key的返回值类似于: 索引key -- value 类型     ...

  5. Ubuntu与centos的区别小用法

    给root设置密码 更新软件下载的地址 安装指令apt 使用ssh登录Ubuntu 使用ssh登录Ubuntu必须注意的地方,要先配置 sudo vi /etc/ssh/sshd_config 找到: ...

  6. hdu1864最大报销额 01

    先把小数乘100变成整数然后处理每个发票里面可以报销的 最后自底向上DP #include <stdio.h> #include <algorithm> #include &l ...

  7. ffmpeg函数05__vcodec_decode_video2()

    vcodec_decode_video2()的作用是解码一帧视频数据

  8. 如何获取到app的包名

    相信很多朋友在刚开始接触测试app的时候都不清楚app的包名是什么,接下来给大家介绍几种方法去获取. 一.手机设备已连接到电脑,点击进入app中,前提是电脑上装备了android-SDK,tools文 ...

  9. 更优雅地关闭资源 - try-with-resource

    https://www.cnblogs.com/hihtml5/p/6505317.html

  10. HDU - 6386 Age of Moyu (双端队列+bfs)

    题目链接 双端队列跑边,颜色相同的边之间的花费为0,放进队首:不同的花费为1,放进队尾. 用Dijkstra+常数优化也能过 #include<bits/stdc++.h> using n ...