前期准备
在准备部署之前,需要安装好Linux系统,Linux系统安装好后,在服务器上安装环境所需要的服务,需要安装java8、docker、docker-compose、gitea、git、maven等服务
1、 linux系统安装(这里以centos7为例)
centos7安装这里不详细展开
2、 docker、docker-compose安装
2.1、docker卸载旧版本
yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine
2.2、docker安装
在安装前检查是否有安装gcc和gcc-c++
whereis gcc
whereis gcc-c++
或者gcc -version
安装软件包
yum install -y yum-utils
设置阿里云镜像仓库
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
更新软件包索引
yum makecache fast
安装docker
yum -y install docker-ce docker-ce-clid.io
启动docker
systemctl start docker
开机启动
systemctl enable docker
查看版本号
docker version
2.3、docker-compose安装
官网:https://docs.docker.com/compose/compose-file/compose-file-v3/
安装:
curl -L "https://github.com/docker/compose/releases/download/v2.10.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
分配权限
chmod +x /usr/local/bin/docker-compose
查看版本
docker-compose --version
3、 gitea远程仓库安装
使用docker-compose + mysql 部署gitea
version: "3"
services:
gitea-mysql:
_name: gitea-mysql
environment:
- TZ=Asia/Shanghai
- MYSQL_USER=gitea
- MYSQL_DATABASE=gitea
- MYSQL_ROOT_PASSWORD=gitea@123
hostname: gitea-mysql
image: mysql:8.0.30
networks:
gitea-network:
ipv4_address: 172.19.0.2
ports:
- 3310:3306
restart: always
privileged: true
volumes:
- ./mysql/mysql-files:/var/lib/mysql-files/
- ./mysql/data:/var/lib/mysql
- ./mysql/my.cnf:/etc/mysql/my.cnf
- ./mysql/initdb:/docker-entrypoint-initdb.d
gitea-service:
_name: gitea
image: gitea/gitea:1.17.3
environment:
- USER_UID=1000
- USER_GID=1000
- GITEA__database__DB_TYPE=mysql
- GITEA__database__HOST=gitea-mysql:3306
- GITEA__database__NAME=gitea
- GITEA__database__USER=gitea
- GITEA__database__PASSWD=gitea
restart: always
privileged: true
networks:
gitea-network:
ipv4_address: 172.19.0.2
volumes:
- ./gitea:/data
- /etc/timezone:/etc/timezone:ro
- /etc/localtime:/etc/localtime:ro
ports:
- "3000:3000"
- "222:22"
depends_on:
- gitea-mysql
networks:
gitea-network:
driver: bridge
ipam:
config:
- subnet: 172.19.0.0/24
name: gitea-network
3.1、 gitea初始化配置文件位置
初始化文件位置是你安装gitea目录中,图中是我安装gitea所在位置,如果在初始化页面上操作失误或者漏写,并且已经进行初始化后,出现错误提示,根据错误提示在app.ini配置文件排查或者修改配置
例如:域名更改或者写错等都可以在这里修改配置
**注意:**如果MySQL使用gitea用户,需要手动创建并授权
创建用户:
//低版本数据库
create user '用户民'@'%' identified by '密码';
//高版本数据库
create user 'gitea'@'%' identified with mysql_native_password by 'gitea@123';
给用户授权:
//指定数据库
grant all privileges on 想授权的数据库.* to '用户名'@'%';
//全部数据库
grant all privileges on *.* to 'gitea'@'%';
4、git安装
安装
yum install -y git
查看版本
git -version
5、 java8安装
java8这里也不详细展开
6、 maven安装
maven下载:https://maven.apache.org/download.cgi
下载后上传到服务器中进行解压
将maven解压至/usr/local/maven目录下,安装目录可以自定义
tar -zxvf apache-maven-3.8.6-bin.tar.gz -C /usr/local/maven
配置环境变量
vim /etc/profile
export MAVEN_HOME=/usr/local/maven
export PATH=$PATH:$MAVEN_HOME/bin
如果PATH有多个,在其后面添加,如:
export PATH=$PATH:$JAVA_HOME/bin:$MAVEN_HOME/bin
依次添加
source /etc/profile
验证安装是否成功
mvn -v
6.1、 maven配置
/usr/local/maven/conf/setting.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>${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>
7、 jenkins安装
使用docker-compose安装Jenkins
version: "3"
services:
jenkins:
_name: jenkins
image: jenkins/jenkins:lts
user: root
privileged: true
restart: always
ports:
- 8082:8080
- 50000:50000
volumes:
- ./jenkins-data:/var/jenkins_home
- /var/run/docker.sock:/var/run/docker.sock
- /usr/bin/docker:/usr/bin/docker
- /usr/lib/x86_64-linux-gnu/libltdl.so.7:/usr/lib/x86_64-linux-gnu/libltdl.so.7
networks:
gitea-network:
ipv4_address: 172.19.0.2
networks:
jenkins-network:
driver: bridge
ipam:
config:
- subnet: 172.19.0.0/24
name: jenkins-network
访问:可以直接使用ip访问或者域名,eg: http://192.168.226.129/8082
访问成功后,会提示需要输入密钥,密钥是放在/var/jenkins_home/secrets/initialAdminPassword文件中
输入下面命令获取密钥:
docker exec [container_id/container_name] cat /var/jenkins_home/secrets/initialAdminPassword
找到密钥输入,继续,在密钥输入并设置管理账号后initialAdminPassword文件会自动销毁
7.1、 安装Maven Integration plugin和Publish Over SSH插件
在可选插件页面中搜索,查找到后,勾选,然后点击install without restart按钮进行安装
在安装时忘记截图,这里给大家看一下,安装好的
到此!整个自动化部署环境搭建完成。
8、实战操作
8.1、创建jenkins任务
选择构建一个maven项目,确定
8.2、配置,刚创建的任务
8.2.1、配置远程git仓库
8.2.2、定时构建
8.2.3、配置远程服务器
在系统配置中找到Publish over SSH,在这配置远程服务器ip,用户密码
配置好后,保存即可执行,具体配置根据自己的实际项目进行配置。