jenkins自动化构建docker镜像并上传至harbor仓库

news2025/1/21 0:50:08

1、插件下载

在这里插入图片描述
首先进入jenkins之后需要现在“Maven”、“GitLab”、“Jdk”、“SSH”、“Git”的相关插件,这里不再赘述,需要什么插件直接安装即可
在这里插入图片描述
搜索对应插件后选择直接安装即可

2、系统全局配置

2.1 Maven配置

在这里插入图片描述
配置maven安装的相应的setting文件
在这里插入图片描述
不知道具体目录的可以通过“mvn -v”之后的Maven home查看

vim /usr.local/apache-maven-3.9.6/conf/setting.xml

修改mirrors下的源地址,因为后面在jenkins中进行jar包打包的时候需要通过maven下载对应的依赖,使用默认maven仓库地址可能会导致失败

    <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>alimaven</id>
        <mirrorOf>central</mirrorOf>
        <name>aliyun maven</name>
        <url>http://maven.aliyun.com/nexus/content/repositories/central/</url>
    </mirror>
 
    <!-- 中央仓库1 -->
    <mirror>
        <id>repo1</id>
        <mirrorOf>central</mirrorOf>
        <name>Human Readable Name for this Mirror.</name>
        <url>http://repo1.maven.org/maven2/</url>
    </mirror>
 
    <!-- 中央仓库2 -->
    <mirror>
        <id>repo2</id>
        <mirrorOf>central</mirrorOf>
        <name>Human Readable Name for this Mirror.</name>
        <url>http://repo2.maven.org/maven2/</url>
    </mirror>

修改maven打包后jar包的java编译编译工具版本,原来setting.xml中的默认版本为jdk1.4,修改为17

    <profile>
      <id>jdk-17</id>

      <activation>
        <jdk>17</jdk>
      </activation>

      <repositories>
        <repository>
          <id>jdk17</id>
          <name>Repository for JDK 17 builds</name>
          <url>http://www.myhost.com/maven/jdk17</url>
          <layout>default</layout>
          <snapshotPolicy>always</snapshotPolicy>
        </repository>
      </repositories>
    </profile>

我的完整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.2.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.2.0 https://maven.apache.org/xsd/settings-1.2.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>
  -->

  <!-- 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>

  <!-- TODO Since when can proxies be selected as depicted? -->
  <!-- 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>alimaven</id>
        <mirrorOf>central</mirrorOf>
        <name>aliyun maven</name>
        <url>http://maven.aliyun.com/nexus/content/repositories/central/</url>
    </mirror>
 
    <!-- 中央仓库1 -->
    <mirror>
        <id>repo1</id>
        <mirrorOf>central</mirrorOf>
        <name>Human Readable Name for this Mirror.</name>
        <url>http://repo1.maven.org/maven2/</url>
    </mirror>
 
    <!-- 中央仓库2 -->
    <mirror>
        <id>repo2</id>
        <mirrorOf>central</mirrorOf>
        <name>Human Readable Name for this Mirror.</name>
        <url>http://repo2.maven.org/maven2/</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 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-17</id>

      <activation>
        <jdk>17</jdk>
      </activation>

      <repositories>
        <repository>
          <id>jdk17</id>
          <name>Repository for JDK 17 builds</name>
          <url>http://www.myhost.com/maven/jdk17</url>
          <layout>default</layout>
          <snapshotPolicy>always</snapshotPolicy>
        </repository>
      </repositories>
    </profile>
    -->

    <!--
     | Here is another profile, activated by the 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>

2.2 git下载

在jenkins中自动构建项目的时候,我们的第一步是从gitlab仓库中拉取我们的代码,所以必不可少需要进行git的安装
在这里插入图片描述
如果自动安装失败,需要修改jenkins的镜像文件

#进入配置文件
vim /etc/docker/daemon.json
# 在文件在最后,增添如下源地址
"registry-mirrors": [
    "https://docker.m.daocloud.io",
    "https://dockerhub.icu",
    "https://registry.aliyuncs.com",
    "https://docker.nju.edu.cn"
  ]
# 保存后退出
# 重启加载dokcer配置
systemctl reload daemon.json
# 重启docker容器使之生效
systemctl restart docker

如果还是安装失败,可自行百度“centos7系统安装git的教程”,安装过程较为简单,这里不再赘述

3、maven任务构建

我这里在gitlab中创建了一个非常基础的maven项目进行测试,所以构建了maven项目
在这里插入图片描述
如果你的界面中没有这个maven任务构建的选项,则说明你没有安装对应的maven插件,可在安装完对应maven插件后再继续进行。
构建任务的命名按自己的意向命名即可
在这里插入图片描述
构建完成任务后进入配置界面
在这里插入图片描述

3.1 重要配置描述

git配置

在这里插入图片描述
URL需要到安装的虚拟机中的gitlab项目中进行复制
在这里插入图片描述
第二步需要需要添加gitlab凭证,就是你登录部署gitlab这台虚拟机的账户和密码
在这里插入图片描述
在配置完全局凭证之后,就可以在任务中的凭证栏选择你刚才配置的凭证了
在这里插入图片描述
触发器默认选第一项即可
在这里插入图片描述
build栏目如下

build post的配置

在我们进行完成jar包的构建之后,需要将构建好的jar包文件打包成docker镜像,并上传到harbor仓库之中,选择第二个选项的意思是在完成jar的打包之后才进行下一步,然后输入需要执行的shell脚本自行自动化部署
在这里插入图片描述

# 进入jenkins打包完成的地址,下面有构建完成的jar包,你们打包完成后如果没有进行目录的修改,前缀应该和我的相同
cd /var/lib/jenkins/workspace/test-bjw/
# 进行docker镜像的构建,注意命名不可以用大写,否则会报错,还有就是这条命令最后有一个‘.’不要忽略,它表示在该目录下执行
docker build -t jenkins_bjw-1.0-snapshot .
# 连接远程harbor仓库
docker login -u admin -p Harbor12345 192.168.1.5:80
# 对docker镜像进行tag标签处理
docker tag jenkins_bjw-1.0-snapshot 192.168.1.5:80/jenkins/jenkins-bjw:v0
# 上传到harbor仓库之中
docker push 192.168.1.5:80/jenkins/jenkins-bjw:v0

构建不成功的可看下一节“可能遇到的问题”

4、 可能遇到的问题

(1)maven打包jar包失败

maven默认情况下,下载依赖的仓库地址非常慢,如果jenkins在进行jar包构建的时候依赖下载失败,请重新检查是否更换了mirror地址,具体操作在本文开始环节

(2)docker构建镜像的时候找不到Dockerfile文件

docker构建项目的时候需要安装Dockerfile的配置进行,但是有时候我们的目录下并没有该Dockerfile文件,所以需要进行Dockerfile文件的创建
1、从idea中打开对应项目,并复制Dockerfile下的内容
在这里插入图片描述
2、创建Dockerfile文件

vim Dockerfile

插入刚才复制的内容,如果你的项目中没有,可以按照我的文件进行修改

(3)docker中jenkins用户组不存在或者权限不足

我们知道jenkins在进行任务构建的时候需要进行dokcer命令的执行,若权限不足会导致失败

# 在docker中添加jenkins用户组
sudo usermod -aG docker jenkins
# 若还是失败,可以重启Jenkins和docker使该命令生效
(4)虚拟机中配置insecure-registries

不配置该条目将无法访问harbor容器

# 进入配置文件
vim /etc/docker/daemon.json
# 增添对应配置,对应上面的截图进行修改
"insecure-registries": ["harbor.domain.io"]
# 重启docker服务使之生效
service docker restart

效果如下,为harbor的虚拟机ip地址以及对应的服务端口号

在这里插入图片描述

(5)上传harbor容器的时候可能会因为命令输入错误而失败,对应的操作详见我的另一篇博客“虚拟机(CentOS7)虚拟机安装harbor及简单使用”

5、 成功部署

jenkins控制台输出如下
在这里插入图片描述
harbor容器中docker镜像如下
在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1978613.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

How does age change how you learn?(2)年龄如何影响学习能力?(二)

Do different people experience decline differently? 不同人经历的认知衰退会有不同吗? Do all people experience cognitive decline uniformly?Or do some people’s minds slip while others stay sharp much longer? 所有人经历的认知衰退都是一样的吗?还是有些人…

计时器(Python)

代码 import time from tkinter import ttk import threading from tkinter import scrolledtext import tkinter as tkclass TimerApp:def __init__(self, root):self.root rootself.root.title("计时器")self.screen_w, self.screen_h self.root.winfo_screenwi…

Conditional Detr

encoder和detr相同&#xff0c;只修改了decoder部分。

(STM32笔记)九、RCC时钟树与时钟 第一部分

我用的是正点的STM32F103来进行学习&#xff0c;板子和教程是野火的指南者。 之后的这个系列笔记开头未标明的话&#xff0c;用的也是这个板子和教程。 九、RCC时钟树与时钟 九、RCC时钟树与时钟1、时钟树HSE时钟HSI时钟锁相环时钟系统时钟HCLK时钟PCLK1时钟PCLK2时钟RTC时钟独…

iPhone苹果手机Safari浏览器怎么收藏网页?

iPhone苹果手机Safari浏览器怎么收藏网页? 1、iPhone苹果手机上找到并打开Safari浏览器&#xff0c;并访问要收藏的网页&#xff1b; 2、打开网页后&#xff0c;点击导航上的更多功能&#xff1b; 3、在更多里&#xff0c;找到并点击添加到个人收藏&#xff0c;完成储存即可添…

2023华为od机试C卷【分配土地】Python实现

思路&#xff1a; 我们可以用哈希结构&#xff0c;此题适合用字典&#xff0c;键为出现的非零数字&#xff0c;值为列表&#xff0c;列表为【minrow,maxrow,minrol,maxrol] import sys def main():m,n map(int,input().split())rects {}for i in range(m):nums [int(i) f…

EFK之filebeat用法进阶

接上一章节&#xff1a;https://blog.csdn.net/weixin_46546303/article/details/140279197?spm1001.2014.3001.5501 一、filebeat module 输入流 1.filebeat module作用 Filebeat 模块的主要作用是简化日志数据的收集和处理过程。通过使用模块&#xff0c;你可以快速地配置…

java计算机毕设课设—网上招聘系统(附源码、文章、相关截图、部署视频)

这是什么系统&#xff1f; java计算机毕设课设—网上招聘系统&#xff08;附源码、文章、相关截图、部署视频&#xff09; 网上招聘系统的设计与实现旨在提供一个便捷、高效的在线平台&#xff0c;使个人求职者和公司能够有效地进行职位申请和人才招聘。该系统特别设计了两种…

Golang | Leetcode Golang题解之第318题最大单词长度乘积

题目&#xff1a; 题解&#xff1a; func maxProduct(words []string) (ans int) {masks : map[int]int{}for _, word : range words {mask : 0for _, ch : range word {mask | 1 << (ch - a)}if len(word) > masks[mask] {masks[mask] len(word)}}for x, lenX : ra…

狮子鱼 CMS ApiController.class.php SQL 注入漏洞

本章提供的所有内容仅供学习、交流和分享用途&#xff0c;只供参考。本站资源禁止并谢绝未经本站许可的使用&#xff0c;如若欲转载&#xff0c;请署名以及注明出处&#xff0c;请务必以文字链接的形式标明或保留文章原始出处和作者的信息。本站(原创)文章、资源、图片等所有内…

嵌入式全栈开发学习笔记---数据结构(顺序表)

目录 顺序结构 顺序表初始化 顺序表插入 顺序表遍历操作 顺序表前驱和后继 顺序表查找操作 顺序表删除操作 顺序表清空销毁 顺序表销毁操作 完整代码 List.c List.h Main.c 顺序表优缺点&#xff08;面试可能会考&#xff09; 上一节我们介绍了数据结构的一般概念…

【ModelSim】仿真问题记录

1、波形出不全&#xff1a; 1、甚至连clk波形都出不来 2、个别波形只有到仿真结束的时候才出现 解决办法&#xff1a; 1、添加波形需要是实例中的net 2、排查是否存在声明与示例的位宽不一致的信号 3、观察是否存在未初始化的变量寄存器 4、缩短整个仿真的步长 2、Instance列…

【HBZ分享】spring启动时自动装配的位置

自动装配流程 springboot启动时&#xff0c;自动装配逻辑在SpringBootApplication这个符合注解中的EnableAutoConfiguration新版springboot3会扫描META-INF的spring文件夹下的org.springframework.boot,autoconfigure,AutoConfiguration.imports文件&#xff0c;会把这里所有写…

FSMC--灵活的静态存储控制器

&#xff08;1&#xff09;在STM32F1系列&#xff08;及F407&#xff09;的芯片上封装了FSMC外设&#xff0c;支持拓展SARM作为RAM。 &#xff08;2&#xff09;SRAM和SDRAM的区别&#xff1a; 存储结构&#xff1a;SRAM使用锁存器、SDRAM使用电容通讯方式&#xff1a;SRAM多…

Leaf分布式ID

文章目录 系统对Id号的要求UUIDsnowflakeLeafLeaf-snowflakeLeaf-segmentMySQL自增主键segment双buffer 系统对Id号的要求 1、业务 1&#xff09;全局唯一性&#xff1a;不能出现重复的ID号&#xff0c;既然是唯一标识&#xff0c;这是最基本的要求 2&#xff09;趋势递增&a…

使用LLM(Large Language Model)进行主题建模

随着互联网技术的快速发展&#xff0c;自然语言处理&#xff08;NLP&#xff09;领域取得了显著的进步。其中&#xff0c;大型语言模型&#xff08;LLM&#xff09;在文本生成任务中表现尤为抢眼。本文旨在探讨LLM在主题建模方面的优势&#xff0c;以及如何将其应用于文本生成任…

深度图进行运算检测物体表面粗糙度

文章目录 应用场景算法原理核心代码应用场景 不同程度凹凸的零件表面粗糙度对零件的磨损产生影响,很大程度上关系到产品性能和使用寿命。不同于单独使用的产品,零件需要在装配中与其他零件相连,密封性和磨损量是厂商需要考虑的一大加工要素;其次,产品外观和触感也会影响到…

数据结构链表2(常考习题1)(C语言)

移除链表元素&#xff1a; . - 力扣&#xff08;LeetCode&#xff09; 题目&#xff1a; 给你一个链表的头节点 head 和一个整数 val &#xff0c;请你删除链表中所有满足 Node.val val 的节点&#xff0c;并返回 新的头节点 。 解题思路&#xff1a; 情况1&#xff1a; 情…

【vluhub】skywalking

SkyWalking是一个开源监控平台&#xff0c;用于从服务和云原生基础设施收集、分析、聚合和可视化数据 低版本存在sql注入漏洞 访问地址 http://192.168.203.12:8080/graphql burpsuite抓数据包 替换 {"query":"query queryLogs($condition: LogQueryConditi…

安装STM32开发工具:STM32CubeMX、STM32CubeIDE、STM32CubeCLT

ST官网地址&#xff1a;https://www.st.com/content/st_com/en.html ST官网因为链接原因下载会比较慢&#xff0c;推荐使用STMCU中文官网&#xff0c;下载很通畅&#xff1a;STMCU中文官网 安装Java 在安装STM32开发软件STM32CubeMX和STM32CubeIDE、STM32CubeCTL之前&#x…