#########################################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管理
我需要實現的目標是
-
因為是內部團隊使用,所以取消注冊功能
image.png -
新用戶缺省不允許創建Project
image.png -
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
下載下來的目錄不是源代碼,而是類似以下的目錄結構:

最后在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