jenkins持续集成-基础

功能

是一个开源的实现持续集成的软件工具
官网:https://www.jenkins.io/
中文文档:https://www.jenkins.io/zh/doc/

准备

git服务器

内存至少4g,内核3.10以上

安装docker

  • 更新yum源:yum update
  • 安装相关依赖: yum install -y yum-utils device-mapper-persistent-data lvm2
  • 添加镜像:
//国外镜像
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
//阿里镜像
https://mirrors.aliyun.com/docker-ce/linux/centos/gpg
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
  • 查看源中可使用版本: yum list docker-ce --showduplicates | sort -r
  • 安装指定版本(版本取上面查询出来的,比如最新版):yum install docker-ce-24.0.2-1.el7
  • 配置开机启动项:
systemctl start docker
systemctl enable docker
docker version

使用容器安装gitlab

  • 添加容器(IP修改为自己的)
docker run --detach \
  --hostname 192.168.1.115 \
  --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
  • 启动容器:docker start gitlab
  • 查看已存在的容器:docker ps -a
  • 访问
# 地址:http://192.168.1.115,如果安装时修改了端口,则加上端口号
# 首次运行比较慢,可能需要等待较长时间,可能会出现502报错等
# 用户名:root
# 密码:进入容器后再指定文件夹查看
docker exec -it  gitlab /bin/bash
cat /etc/gitlab/initial_root_password

jenkins服务器

jdk与maven安装

  • 官网下载安装包:jdk-17_linux-x64_bin.tarapache-maven-3.9.2-bin.tar
  • 解压:tar -zxvf jdk-17_linux-x64_bin.tar -C /usr/local
  • 配置环境变量:
    /etc/profile.d下创建my_env.sh
#JAVA_HOME
export JAVA_HOME=/usr/local/java/jdk-17.0.2
export PATH=$PATH:$JAVA_HOME/bin
#MAVEN_HOME
export MAVEN_HOME=/usr/local/maven/apache-maven-3.9.2
export M2_HOME=/usr/local/maven/apache-maven-3.9.2
export PATH=$PATH:$MAVEN_HOME/bin

maven的settings.xml,修改镜像

<?xml version="1.0" encoding="UTF-8"?>
<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>${user.home}/.m2/repository</localRepository>
    <pluginGroup>org.mortbay.jetty</pluginGroup>
  </pluginGroups>
  <proxies>
  </proxies>
  <servers>
    <server>
        <id>releases</id>
        <username>ali</username>
        <password>ali</password>
      </server>
      <server>
        <id>Snapshots</id>
        <username>ali</username>
        <password>ali</password>
      </server>
  </servers>
  <mirrors>
    <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> 
    <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>
</settings>

jenkins安装

  • 首次启动war包会在/root/.jenkins生成配置文件
  • 待完全启动成功后 访问服务器8080端口完成配置
  • 选择安装推荐的插件
  • 启动后控制台可以看到admin对应的默认密码
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
  • 安装git客户端 yum install -y git
  • 安装插件:Maven Integration与Publish Over SSH
    Maven Integration用来创建maven项目,Publish Over SSH用来将jenkins服务器的jar,部署到测试服务器。
    左侧菜单Dashboard下Manage Jenkins,选择System Configurarion下的Manage Plugins
    插件管理.png
  • 配置maven
    Dashboard下Manage Jenkins,选择System Configurarion下的Global Tool Configuration,拉到最下面,设置Name与MAVEN_HOME
    maven配置.png
  • 配置测试服务器host
    Dashboard下Manage Jenkins,选择System Configurarion下的Configure System,拉到最下面,填写Name,Hostname,Username,点击高级,勾选Use password authentication, or use a different key
    SSH Server.png

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

  • 新建Item
    新建Item.png

  • 点击确定之后进入配置页面
    配置项列表.png

  • 源码管理,选择git,并配置仓库URL以及分支信息
    git配置.png

  • Build配置
    需要配置Root POM,这个是相对于git上的根目录的,如果是直接在项目下,直接输入pom.xml即可

  • POST STEPS
    POST STeps.png

  • SSH PUBLISHERS
    SSH Publishers.png

欢迎大家留言,以便于后面的人更快解决问题!另外亦欢迎大家可以关注我的微信公众号,方便利用零碎时间互相交流。共勉!

posted @ 2023-07-29 19:20  灵台悠步  阅读(10)  评论(0编辑  收藏  举报