Jenkins(超详细的Docker安装Jenkins教程!!!)

Jenkins

Jenkins,原名 Hudson,2011 年改为现在的名字。它是一个开源的实现持续集成的软件工具。

官方网站:https://www.jenkins.io/

中文文档:https://www.jenkins.io/zh/

为什么需要Jenkins?

我们以前写完代码,如果要将项目部署到服务器中,那么我们需要经过一系列的更改、打包、上传等繁杂的操作。这些操作很浪费我们的时间,并且基本没什么技术含量。那么jenkins应运而生。jenkins只需要我们将写好的项目上传到我们的代码管理仓库中,例如gitlab,然后jenkins会自动进行拉取,打包,上传一系列操作,这样就让我们的工作量大大减少,我们只需要专注于业务的操作即可

什么是CI/CD?

CI、CD 其实是三个概念,包含了一个 CI 和两个 CD,CI全称 Continuous Integration,表示持续集成,CD包含 Continuous Delivery和 Continuous Deployment,分别是持续交付和持续部署。这三个概念之间是有前后依赖关系 的。

CI/CD 并不是一个工具,它是一种软件开发实践,核心是通过引入自动化的手段来提高软件交付效率。CI/CD 最终目的:让工程师更快 & 更高质量 & 更简单的交付软件!

具体大家可以看这篇文章:什么是 CI/CD ?

安装Jenkins

环境需要:Linux虚拟机硬盘20G,内存4G以上

安装JDK

官网下载地址:https://www.oracle.com/cn/java/technologies/javase/javase8u211-later-archive-downloads.html

现在下载java8JDK需要注册Oracle账户,下载即可,或者联系博主可以给安装包

我们Linux下记得一定要下载类似这样的包:jdk-8u261-linux-x64.tar.gz

  1. 将下载好的tar包上传到Linux中/usr/java目录中,如果没有java文件夹,请创建一个

  2. 使用解压命令解压我们的tar包

    tar -zxvf jdk-8u261-linux-x64.tar.gz
    
  3. 创建一个挂载目录,用于存放jenkins数据

    //创建目录
    mkdir -p /usr/docker/jenkins_data
    
    mkdir /var/jenkins_home
    //授权权限
    chmod 777 jenkins_home
    
  4. 将我们刚才解压的jdk移动到jenkins_home目录下并且改一个比较简短的名字(也可以不改)

    mv /usr/java/jdk1.8.261 /var/jenkins_home/jdk1.8
    
  5. 配置JAVA_HOME环境变量

    // 打开profile文件
    vim /etc/profile
    
    // 配置java环境
    JAVA_HOME=/var/jenkins_home/jdk1.8
    CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
    PATH=$JAVA_HOME/bin:$PATH
    export JAVA_HOME CLASSPATH PATH
    :wq保存退出
    // 使用更新配置文件
    source /etc/profile
    
  6. 执行java -version命令,出现以下内容即成功

安装Maven

下载官网:https://maven.apache.org/download.cgi

  1. 将下载后的tar包一样上传到我们的Linux的/usr/local/中,这块的目录大家可以自己定,也可以为了省事和博主一样

  2. 解压我们的tar

    tar -zxvf apache-maven-3.9.6-bin.tar.gz
    
  3. 将解压后的文件夹移到jenkins_home目录下并且改一个比较简短的名字(也可以不改)

    mv /usr/local/apache-maven-3.9.6 /var/jenkins_home/apache-maven-3.9.6
    
    1. 进入到/var/jenkins_home/apache-maven-3.9.6/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>
      
  4. 配置maven环境

    // 打开profile文件
    vim /etc/profile
    
    // 配置java环境
    export MAVEN_HOME=/var/jenkins_home/apache-maven-3.9.6
    export PATH=${MAVEN_HOME}/bin:${PATH}
    :wq保存退出
    // 使用更新配置文件
    source /etc/profile
    
  5. 执行mvn -v命令,出现以下内容即成功

Docker下载安装Jenkins

首先我们需要安装Docker,这里不再进行赘述,关于Docker的安装我在之前的博客中多次提到,不会的同学可以去看之前的文章,有详细的Docker教程

  1. docker拉取镜像,注意指定版本号,否则拉取的是比较旧的版本

    docker pull jenkins/jenkins:2.426.2-lts
    
  2. 我们刚才已经创建了挂载目录,所以这里直接执行创建容器

    docker run -d -p 8082:8080 -p 50000:50000 -v /usr/docker/jenkins_data:/var/jenkins_home  -v /etc/localtime:/etc/localtime -v /usr/bin/docker:/usr/bin/docker     -v /var/run/docker.sock:/var/run/docker.sock   --restart=on-failure  -u 0 --name myjenkins jenkins/jenkins:2.426.2-lts
    

    指令解析:

    • -d :后台运行容器

    • -p:端口映射, 左边是本地端口,右边是docker容器端口 ,8080是Jenkins Web 界面的工作端口,50000是JNLP(Java Network Launch Protocol)工作端口。这个端口用于 Jenkins 节点和主控节点之间的通信。

    • -v :目录挂载,将主机上的 /usr/docker/jenkins_data 目录挂载到容器内的 /var/jenkins_home 目录,用于持久化 Jenkins 的数据。/etc/localtime:/etc/localtime:将本地主机上的时区信息文件挂载到容器内的 /etc/localtime 文件中,确保容器内的时间与主机上的时间一致

    • -v /usr/bin/docker:/usr/bin/docker: 将主机上的 /usr/bin/docker 文件挂载到容器中的 /usr/bin/docker,这样容器内的 Jenkins 可以直接使用宿主机上的 Docker 命令。在使用 GitLab/Jenkins 等 CI 软件的时候需要使用 Docker 命令来构建镜像,需要在容器中使用 Docker 命令;通过将宿主机的 Docker 共享给容器

    • -v /var/run/docker.sock:/var/run/docker.sock: 将主机上的 Docker socket 文件挂载到容器中的相同位置,这样容器内的 Jenkins 可以与宿主机上的 Docker 引擎进行通信。

    • --restart=on-failure:设置容器的重启策略为在容器以非零状态退出(异常退出)时重启。

    • -u 0:将容器内进程的用户身份设置为 root 用户,等同于-u root。

    • --name myjenkins:给容器指定一个名称为 myjenkins。

  3. 极其重要!!!!!!!!!!刚才我们将jdk、maven放到了jenkins_home下,这里我们需要将其放到我们的数据卷中,也就是刚才创建的/usr/docker/jenkins_data目录下

    cp -r /var/jenkins_home/jdk1.8 /usr/docker/jenkins_data
    cp -r /var/jenkins_home/apache-maven-3.9.6 /usr/docker/jenkins_data
    

    这是因为docker和我们的主机是有环境隔离的,我们要将其映射到docker中,这样jenkins才能找到jdkmaven

  4. 验证Jenkins容器是否启动成功

    docker ps
    
  5. 获取管理员密码

    查看日志

    docker logs myjenkins
    
  6. 开始使用

    在浏览器中输入192.168.48.133:8082这里的ip地址是和自己的虚拟机ip地址一样的,端口号是刚才创建容器时指定的端口号

    然后稍等一会,就会出现以下样式,然后输入进去我们的密码

    然后出现新手入门,选择推荐插件

    等一会基本就下载完毕了,随后让我们创建一个账户,正常创建即可,然后就下一步最后我们的jenkins页面就出来了

温馨提示

为什么不去官网下载jenkins.war包进行安装?

这里我们下载的还是比较旧的jenkins,这样会导致我们在下载插件的时候,很多插件由于版本的问题,下载不了,会报错。经过多方面尝试,实际上我们用docker部署jenkins更为方便快捷

jenkins页面一直没有出现,为什么?

有可能你没有关闭防火墙,或者可以不关闭,开放8080端口即可,或者也有可能你此时此刻某一个进程占用了8080端口

# 关闭
systemctl stop firewalld
# 禁止开机启动防火墙
systemctl disable firewalld

使用Jenkins

因为我们要构建的是一个Maven项目,jenkins默认插件没有maven,所以我们需要去下载

在这里搜索安装maven,选择第一个安装即可,这里因为我安装过了,所以无法再次安装

Git安装

yum install -y git

随后回到首页,点击新建item

输入一个名称,选择maven项目,点击确定即可

Git配置

如果你是gitlab,接下来需要更换你的分支名称,因为gitlab默认分支名称是main

Maven配置

JDK配置

Build配置

这里的Root POM中填写自己在代码仓库中项目中pom.xml文件在哪里,就填写哪里,我的就在java-project下,就直接pom.xml就可以了

注意

这里Git、Build配置都在创建的项目item中的配置中

Maven、JDK配置在系统管理中的全局工具配置中

结果

都准备好,我们点击构建

可以在控制台看到输出效果

这里可以看到是在/var/jenkins_home/workspace/first/target/下生成了一个jar包,但是这里这个目录是jenkins容器中的,实际上映射到了/usr/docker/jenkins_data/workspace/first/target

进入到jenkins容器中就可以看到日志上打印出来的结果

执行以下命令,在页面访问即可出现项目中的结果

java -jar SpringBootHelloWord-1.0-SNAPSHOT.jar
相关推荐
冬天vs不冷5 分钟前
Linux用户与权限管理详解
linux·运维·chrome
凯子坚持 c1 小时前
深入Linux权限体系:守护系统安全的第一道防线
linux·运维·系统安全
摸鱼也很难4 小时前
Docker 镜像加速和配置的分享 && 云服务器搭建beef-xss
运维·docker·容器
woshilys5 小时前
sql server 查询对象的修改时间
运维·数据库·sqlserver
疯狂飙车的蜗牛5 小时前
从零玩转CanMV-K230(4)-小核Linux驱动开发参考
linux·运维·驱动开发
恩爸编程6 小时前
探索 Nginx:Web 世界的幕后英雄
运维·nginx·nginx反向代理·nginx是什么·nginx静态资源服务器·nginx服务器·nginx解决哪些问题
鸠摩智首席音效师7 小时前
Docker 中如何限制CPU和内存的使用 ?
docker·容器
Michaelwubo7 小时前
Docker dockerfile镜像编码 centos7
运维·docker·容器
jingyu飞鸟7 小时前
centos-stream9系统安装docker
linux·docker·centos
好像是个likun8 小时前
使用docker拉取镜像很慢或者总是超时的问题
运维·docker·容器