jenkins部署java项目
#########################################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.7### #安装jdk mkdir -p /usr/local/java cat <<EOF >> /etc/profile export JAVA_HOME=/usr/local/java/jdk1.8.0_171 export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar export PATH=$PATH:/usr/local/java/jdk1.8.0_171/bin EOF tar -zxvf jdk-8u171-linux-x64.tar.gz -C /usr/local/java source /etc/profile java -version if [ $? -ne 0 ]; 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: | | 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>/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>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> --> </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管理
我需要实现的目标是
-
因为是内部团队使用,所以取消注册功能
-
新用户缺省不允许创建Project
-
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上能创建和获取到。
- password最好复杂一下,不能太简单。否则会有错误
-
name如果是中文,需要转码,否则也有错误
- can_create_group=false:表示禁止创建组
- skip_confirmation=true:表示跳过邮件确认
更多参数可以参考Gitlab的API文档
4. 导出旧项目然后导入到Gitlab
如果只把源码导出再导入,那么所有分支包括分支的所有历史记录都没有了。可以通过以下命令来实现把项目respository完全镜像clone下来,包括所有分支和记录。
git clone --bare git://github.com/username/project.git
下载下来的目录不是源代码,而是类似以下的目录结构:
最后在gitlbab上先创建一个新的respository,然后命令行进入到以上的目录下,执行git push命令把旧的镜像推送到新的gitlab上的respository里。
cd project.git
git push --mirror git@你的gitlab的ip/newproject.git
链接:https://www.jianshu.com/p/0cd62d826114
来源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。
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.8.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 "1" ] then kill -9 $PID fi if [ ! -d "$BackUp" ] then mkdir -p "$BackUp" fi mv $BaseDir/* $BackUp mv $FaBan/* $BaseDir sh /opt/tomcat/bin/startup.sh