Jenkins

无论你身处何境都是自己的选择

「百度云盘」
链接:https://pan.baidu.com/s/1caD8ss1nYqTt0PfOxiFqxQ
提取码:yyds

「阿里云盘」
https://www.aliyundrive.com/s/yHmwRFH8Gi4

安装GitLab

安装所需最小配置
内存至少4G

在ssh下安装

安装依赖

sudo yum install -y curl policycoreutils-python openssh-server perl
sudo systemctl enable sshd
sudo systemctl start sshd

配置镜像

 curl -fsSL https://packages.gitlab.cn/repository/raw/scripts/setup.sh |
/bin/bash

开始安装

 sudo EXTERNAL_URL="http://192.168.44.103" yum install -y gitlab-jh

除非您在安装过程中指定了自定义密码,否则将随机生成一个密码并存储在
/etc/gitlab/initial_root_password 文件中(出于安全原因,24 小时后,此文件会被第一次
gitlab-ctl reconfigure 自动删除,因此若使用随机密码登录,建议安装成功初始登录成功之后,立
即修改初始密码)。使用此密码和用户名 root 登录。
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 # 查看日志;

在docker下安装

安装所需最小配置

  • 内存至少4G
  • 系统内核至少在3.10以上 uname -r 命令可查看系统内核版本

安装docker

  1. 更新yum源
yum update
  1. 安装依赖
yum install -y yum-utils device-mapper-persistent-data lvm2
  1. 添加镜像
//国外镜像
yum-config-manager --add-repo https://download.docker.com/linux/centos/dockerce.repo
//阿里镜像
https://mirrors.aliyun.com/docker-ce/linux/centos/gpg
yum-config-manager --add-repo http://mirrors.aliyun.com/dockerce/linux/centos/docker-ce.repo
  1. 查看源中可使用版本
yum list docker-ce --showduplicates | sort -r
  1. 安装指定版本
yum install docker
  1. 配置开机启动项
systemctl start docker
systemctl enable docker
docker version

使用容器安装gitlab
1.添加容器

docker run --detach \
--hostname 192.168.111.102 \
--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

2.启动容器

docker start gitlab

3.查看已存在的容器

docker ps -a

4.进入容器

docker exec -it gitlab /bin/bash

访问(3分钟后)
http://192.168.111.102
当首次运行出现502错误的时候排查两个原因

  1. 虚拟机内存至少需要4g
  2. 稍微再等等刷新一下可能就好了

管理员账号登录
用户名:root
密码存在下面文件中,登录后需要改密码不然24小时之后会失效

cat /etc/gitlab/initial_root_password

在这里插入图片描述
在这里插入图片描述

创建Git仓库(空白项目)
在这里插入图片描述
在这里可以修改仓库的一些信息
在这里插入图片描述
可以创建master分支,避免每次都合并!!!
在这里插入图片描述

创建一个项目
在这里插入图片描述
完成基础代码的编写后,进行提交,再推送至Git Lab
在这里插入图片描述
出现类似问题,可以修改仓库可见性<私有、公开>,先进行pull ,然后再进行push
在这里插入图片描述

在这里插入图片描述

Jenkins安装

官方文档
安装需求

机器要求:
256 MB 内存,建议大于 512 MB
10 GB 的硬盘空间(用于 Jenkins 和 Docker 镜像)
需要安装以下软件:
Java 8 ( JRE 或者 JDK 都可以)
Docker (导航到网站顶部的Get Docker链接以访问适合您平台的Docker下载)

安装JDK

  1. 检索可用包
yum search java|grep jdk
  1. 安装
yum install java-1.8.0-openjdk
  1. 必须安装JDK
默认yum安装java的时候会显示安装的是openjdk1.8 实则实际上只安装了jre~
yum install -y java-devel

JAVA_HOME配置(可选)

export JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.322.b06-1.el7_9.x86_64
export JRE_HOME=$JAVA_HOME/jre
PATH=$PATH:$JAVA_HOME/bin:$JRE_HOME/bin
export JAVA_HOME JRE_HOME CLASS_PATH PATH
jdk
/etc/alternatives/jre_openjdk
source /etc/profile #立即生效

java -jar 启动war包
首次启动war包会在 /root/.jenkins 生成配置文件
待完全启动成功后 访问服务器8080端口完成配置
初始化后的密码:

Jenkins initial setup is required. An admin user has been created and a password
generated.
Please use the following password to proceed to installation:
4e67bbe261da476abdc63c5b51311646
This may also be found at: /root/.jenkins/secrets/initialAdminPassword

密码文件使用后会自动删除

登陆后一定要修改密码!

在这里插入图片描述
点击设置,往下拉
在这里插入图片描述

Jenkins密码忘记重置方法

删除Jenkins目录下config.xml文件中下面代码,并保存文件

<useSecurity>true</useSecurity>
<authorizationStrategy class="hudson.security.FullControlOnceLoggedInAuthorizationStrategy">
  <denyAnonymousReadAccess>true</denyAnonymousReadAccess>
</authorizationStrategy>
<securityRealm class="hudson.security.HudsonPrivateSecurityRealm">
  <disableSignup>true</disableSignup>
  <enableCaptcha>false</enableCaptcha>
</securityRealm>

选择推荐安装
在这里插入图片描述

Maven安装

官方网址

下载后复制到Jenkins所在服务器解压缩即可
Maven阿里云镜像

修改 /usr/local/maven/conf/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>${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>

Git安装

yum install -y git

空间不够

删除目录

cd /root/.jenkins/jobs/first

删除buildsmodules目录
安装插件
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

Jenkins + Git + Maven 自动化部署配置

安装Maven插件

在这里插入图片描述
在这里插入图片描述

配置Maven

在这里插入图片描述

在这里插入图片描述

新建一个任务
在这里插入图片描述
通用配置
在这里插入图片描述
太多任务需要配置直接批处理详见Jenkins安装-->空间不够

Git配置

在这里插入图片描述

凭证配置
在这里插入图片描述

Pom配置

配置参考下图
在这里插入图片描述

此时Pom文件就在分支下没有前缀,直接填pom.xml或者不填
在这里插入图片描述

发布到测试服务器(也可以是本机)

安装Publish over SSH

在这里插入图片描述

配置测试服务器

在这里插入图片描述
往下拉看到Publish over SSH,点击新增

在这里插入图片描述

添加服务器,然后选择高级使用密码登录

在这里插入图片描述

有代理机时选择
在这里插入图片描述
点击项目名称
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
a.sh

#!/bin/bash

#项目名
appname=$1

#获取正在运行的jar包pid
pid=`ps -ef | grep $1 | grep 'java -jar' | awk '{printf $2}'`

#如果pid为空,提示一下,否则,执行kill命令
if [ -z $pid ]
#使用-z 做空值判断
then
	echo "$appname not started"
else
	kill -9 $pid
	echo "$appname stoping...."
check=`ps -ef | grep -w $pid | grep java`
if [ -z $check ]
then
	echo "$appname pid:$pid is stop"
else
	echo "$appname stop failed"
fi

fi
nohup  java -jar /root/project/demo*.jar --server.port=8081  &>mylog.log  &
# 指定端口为8081,防止与Jenkins端口发生冲突

超时

在这里插入图片描述

  1. 传输文件过大,耗时比较长
    在这里插入图片描述
  2. 命令端口冲突,输出命令时一定要注意不要让窗口卡主,不然Jenkins会认为认为一直没完成
nohup  java -jar /root/project/demo*.jar  &>mylog.log  &

在这里插入图片描述

GitLab钩子自动构建

在这里插入图片描述
在这里插入图片描述
Build Authorization Token
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

Poll SCM源码变更构建

H * * * * 表示一小时内的任意时间
*/10 * * * * 每10分钟
H/10 * * * * 每10分钟,可能是7,17,27,起始时间hash,步长不变
45 3 * * 1-6 每个周一至周六,凌晨3点45 执行1次
45 3-5 * * 1-6 每个周一至周六,凌晨3点45 ,凌晨4点45,凌晨5点45 各执行1次
H(40-48) 3-5 * *1-6 在40~48之间取值 其他同上
45 3-5/2 * * 1-6 每个周一至周六,凌晨3点45 ,凌晨5点45 各执行1次
45 0-6/2 * * 1-6 1-6 0点开始,每间隔2小时执行一次 0:45、2:45、4:45

*/10表示每隔10分钟
1-6代表从1到6
在这里插入图片描述

万般努力,我只为出人头地

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值