devops-在jenkins-slave(k8s)中集成maven使用

本文涉及的产品
容器服务 Serverless 版 ACK Serverless,952元额度 多规格
容器服务 Serverless 版 ACK Serverless,317元额度 多规格
简介: 在jenkins-slave(k8s)中集成maven使用


环境准备


系统环境:centos7


k8s集群:master 和 slave

gitlab

jenkins



制作maven3.6-jdk-11镜像



Dockfile配置


FROM maven:3.6-jdk-11
COPY settings.xml /usr/share/maven/conf/


COPY settings.xml /usr/share/maven/conf/:替换容器中的settings.xml文件,使用更改ali源的settings.xml文件





maven的settings文件配置


配置阿里镜像仓库源



<?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>/root/.m2/repository</localRepository>
    <!-- Apache Maven 配置 -->
    <pluginGroups/>
    <proxies/>
    <!-- 私服发布的用户名密码 -->
    <!-- <servers>
        <server>
            <id>releases</id>
            <username>deployment</username>
            <password>He2019</password>
        </server>
        <server>
            <id>snapshots</id>
            <username>deployment</username>
            <password>He2019</password>
        </server>
    </servers>
    -->
    <!-- 阿里云镜像 -->
    <mirrors>
        <mirror>
            <id>alimaven</id>
            <name>aliyun maven</name>
            <!-- https://maven.aliyun.com/repository/public/ -->
            <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
            <mirrorOf>central</mirrorOf>
        </mirror>
    </mirrors>
    <!-- 配置: java8, 先从阿里云下载, 没有再去私服下载  -->
    <!-- 20190929 hepengju 测试结果: 影响下载顺序的是profiles标签的配置顺序(后面配置的ali仓库先下载), 而不是activeProfiles的顺序 -->
    <profiles>
        <!-- 全局JDK11配置 -->
        <profile>
            <id>jdk11</id>
            <activation>
                <activeByDefault>true</activeByDefault>
                <jdk>11</jdk>
            </activation>
            <properties>
                <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
                <maven.compiler.source>11</maven.compiler.source>
                <maven.compiler.target>11</maven.compiler.target>
                <maven.compiler.compilerVersion>11</maven.compiler.compilerVersion>
            </properties>
        </profile>
        <!-- Nexus私服配置: 第三方jar包下载, 比如oracle的jdbc驱动等 -->
        <!--
        <profile>
            <id>dev</id>
            <repositories>
                <repository>
                    <id>nexus</id>
                    <url>http://nexus.hepengju.cn:8081/nexus/content/groups/public/</url>
                    <releases>
                        <enabled>true</enabled>
                    </releases>
                    <snapshots>
                        <enabled>true</enabled>
                    </snapshots>
                </repository>
            </repositories>
            <pluginRepositories>
                <pluginRepository>
                    <id>public</id>
                    <name>Public Repositories</name>
                    <url>http://nexus.hepengju.cn:8081/nexus/content/groups/public/</url>
                </pluginRepository>
            </pluginRepositories>
        </profile>
        -->
        <!-- 阿里云配置: 提高国内的jar包下载速度 -->
        <profile>
            <id>ali</id>
            <repositories>
                <repository>
                    <id>alimaven</id>
                    <name>aliyun maven</name>
                    <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
                    <releases>
                        <enabled>true</enabled>
                    </releases>
                    <snapshots>
                        <enabled>true</enabled>
                    </snapshots>
                </repository>
            </repositories>
            <pluginRepositories>
                <pluginRepository>
                    <id>alimaven</id>
                    <name>aliyun maven</name>
                    <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
                </pluginRepository>
            </pluginRepositories>
        </profile>
    </profiles>
    <!-- 激活配置 --> 
    <activeProfiles>
       <activeProfile>jdk11</activeProfile>
       <!--  <activeProfile>dev</activeProfile> -->
        <activeProfile>ali</activeProfile>
    </activeProfiles>
</settings>



构建新的镜像



╭─root@k201 ~/mk-maven 
╰─# ls
Dockerfile  settings.xml
╭─root@k201 ~/mk-maven 
╰─# docker build -t yfy/maven:3.6-jdk-11 . 




创建spring-boot微服务项目



创建spring-boot微服务,并由gitlab进行托管

image.png


修改pom.xml文件中的spring-boot-starter-parent 版本以及 jdk 的版本

image.png





jenkins创建流水线



image.png



脚本内容




pipeline {  agent {    kubernetes {      //yamlFile 'KubernetesPod.yaml'      cloud 'kubernetes'      yaml '''---apiVersion: v1
kind: Pod
metadata:  namespace: devops
spec:  nodeSelector:    kubernetes.io/os: linux
  restartPolicy: Never
  serviceAccountName: jenkins
  containers:    - name: "docker"      image: "docker:latest"      imagePullPolicy: "IfNotPresent"      resources:        limits: {}        requests: {}      command:      - cat
      tty: true      volumeMounts:        - mountPath: /var/run/docker.sock
          name: docker-sock
          readOnly: false    - name: "python3"      image: "python:3.8.6"      imagePullPolicy: "IfNotPresent"      resources:        limits: {}        requests: {}      command:      - cat
      tty: true      volumeMounts:        - mountPath: /var/run/docker.sock
          name: docker-sock
          readOnly: false    - name: "maven36"      image: "yfy/maven:3.6-jdk-11"      imagePullPolicy: "IfNotPresent"      resources:        limits: {}        requests: {}      command:      - cat
      tty: true      volumeMounts:        - mountPath: /root/.m2/repository
          name: maven-repo
          readOnly: false  volumes:    - hostPath:        path: /var/run/docker.sock
      name: docker-sock
    - hostPath:        path: /data/devops/jenkins/.m/repository
      name: maven-repo
'''}}  stages {    stage('checkout') {      steps {        checkout([$class: 'GitSCM',                  branches: [[name: '*/main']],                  extensions: [],                  userRemoteConfigs: [[credentialsId: 'gitlab-author', url: 'http://192.168.110.200:8085/yfy-dev/maven-k8s-spring-boot-demo.git']]])
}}    stage('maven3.6'){        steps{            container('maven36'){                sh "mvn -v"                sh "mvn clean package -DskipTests=true"}}}    // stage('build-allure-results') {    //     steps {    //         container('python3') {    //             sh """    //             python --version
    //             pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple
    //             python main.py ${allure_results_path}    //             """    //         }    //     }    // }    // stage('build-allure-export') {    //     steps {    //         // 通过jenkins的全局工具配置,下载allure工具
    //         allure includeProperties: false, jdk: '',  results: [[path: '${allure_results_path}']]    //     }    // }    // stage('docker build') {    //   steps {    //     container('docker') {    //       sh """    //         docker version
    //       """    //     }    //     container('python3') {    //       print('user mutil container...')
    //     }    //   }    // }}}



在配置文件配置,构建的yfy/maven:3.6-jdk-11的镜像


并设置maven仓库宿主机的存储目录


image.png


注意


      volumeMounts:        - mountPath: /root/.m2/repository
          name: maven-repo
          readOnly: false


mountPath: /root/.m2/repository: 容器中仓库的的地址,此目录可以在settings.xml

文件中进行配置


如图所示

image.png



设置maven仓库在宿主机中的目录



image.png


/data/devops/jenkins/.m/repository:  这个目录为jenkins在宿主机上的目录,

建议把maven的仓库目录映射到jenkins目录中,方便jenkins迁移,同时把maven仓库的进行统一迁移。



流水线构建



Started by user jenkins
[Pipeline] Start of Pipeline
[Pipeline] podTemplate
[Pipeline]{[Pipeline] node
Created Pod: kubernetes devops/maven-k8s-spring-boot-demo-11-8pnnw-fcdzv-mkxn4
Agent maven-k8s-spring-boot-demo-11-8pnnw-fcdzv-mkxn4 is provisioned from template maven-k8s-spring-boot-demo_11-8pnnw-fcdzv
---apiVersion: "v1"kind: "Pod"metadata:  annotations:    buildUrl: "http://192.168.10.201:30080/job/maven-k8s-spring-boot-demo/11/"    runUrl: "job/maven-k8s-spring-boot-demo/11/"  labels:    jenkins: "slave"    jenkins/label-digest: "a898832bd9447ff86f51e703caa04979f02f781b"    jenkins/label: "maven-k8s-spring-boot-demo_11-8pnnw"  name: "maven-k8s-spring-boot-demo-11-8pnnw-fcdzv-mkxn4"  namespace: "devops"spec:  containers:  - command:    - "cat"    image: "docker:latest"    imagePullPolicy: "IfNotPresent"    name: "docker"    resources:      limits: {}      requests: {}    tty: true    volumeMounts:    - mountPath: "/var/run/docker.sock"      name: "docker-sock"      readOnly: false    - mountPath: "/home/jenkins/agent"      name: "workspace-volume"      readOnly: false  - command:    - "cat"    image: "python:3.8.6"    imagePullPolicy: "IfNotPresent"    name: "python3"    resources:      limits: {}      requests: {}    tty: true    volumeMounts:    - mountPath: "/var/run/docker.sock"      name: "docker-sock"      readOnly: false    - mountPath: "/home/jenkins/agent"      name: "workspace-volume"      readOnly: false  - command:    - "cat"    image: "yfy/maven:3.6-jdk-11"    imagePullPolicy: "IfNotPresent"    name: "maven36"    resources:      limits: {}      requests: {}    tty: true    volumeMounts:    - mountPath: "/root/.m2/repository"      name: "maven-repo"      readOnly: false    - mountPath: "/home/jenkins/agent"      name: "workspace-volume"      readOnly: false  - env:    - name: "JENKINS_SECRET"      value: "********"    - name: "JENKINS_AGENT_NAME"      value: "maven-k8s-spring-boot-demo-11-8pnnw-fcdzv-mkxn4"    - name: "JENKINS_NAME"      value: "maven-k8s-spring-boot-demo-11-8pnnw-fcdzv-mkxn4"    - name: "JENKINS_AGENT_WORKDIR"      value: "/home/jenkins/agent"    - name: "JENKINS_URL"      value: "http://192.168.10.201:30080/"    image: "jenkins/inbound-agent:4.11-1-jdk11"    name: "jnlp"    resources:      limits: {}      requests:        memory: "256Mi"        cpu: "100m"    volumeMounts:    - mountPath: "/home/jenkins/agent"      name: "workspace-volume"      readOnly: false  nodeSelector:    kubernetes.io/os: "linux"  restartPolicy: "Never"  serviceAccountName: "jenkins"  volumes:  - hostPath:      path: "/var/run/docker.sock"    name: "docker-sock"  - hostPath:      path: "/data/devops/jenkins/.m/repository"    name: "maven-repo"  - emptyDir:      medium: ""    name: "workspace-volume"Running on maven-k8s-spring-boot-demo-11-8pnnw-fcdzv-mkxn4 in /home/jenkins/agent/workspace/maven-k8s-spring-boot-demo
[Pipeline]{[Pipeline] stage
[Pipeline]{ (checkout)
[Pipeline] checkout
The recommended git tool is: NONE
using credential gitlab-author
Cloning the remote Git repository
Cloning repository http://192.168.10.200:8085/yfy-dev/maven-k8s-spring-boot-demo.git
 > git init /home/jenkins/agent/workspace/maven-k8s-spring-boot-demo # timeout=10Fetching upstream changes from http://192.168.10.200:8085/yfy-dev/maven-k8s-spring-boot-demo.git
 > git --version # timeout=10 > git --version # 'git version 2.30.2'using GIT_ASKPASS to set credentials gitlab-author
 > git fetch --tags --force --progress -- http://192.168.10.200:8085/yfy-dev/maven-k8s-spring-boot-demo.git +refs/heads/*:refs/remotes/origin/* # timeout=10 > git config remote.origin.url http://192.168.10.200:8085/yfy-dev/maven-k8s-spring-boot-demo.git # timeout=10 > git config --add remote.origin.fetch +refs/heads/*:refs/remotes/origin/* # timeout=10Avoid second fetch
Checking out Revision 8ee4fae71eb7c9bec5b590ffa5c314301623c2d6 (refs/remotes/origin/main)
 > git rev-parse refs/remotes/origin/main^{commit}# timeout=10 > git config core.sparsecheckout # timeout=10 > git checkout -f 8ee4fae71eb7c9bec5b590ffa5c314301623c2d6 # timeout=10Commit message: "Update pom.xml"First time build. Skipping changelog.
[Pipeline]}[Pipeline] // stage
[Pipeline] stage
[Pipeline]{ (maven3.6)
[Pipeline] container
[Pipeline]{[Pipeline] sh
+ mvn -v
Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f)
Maven home: /usr/share/maven
Java version: 11.0.10, vendor: Oracle Corporation, runtime: /usr/local/openjdk-11
Default locale: en, platform encoding: UTF-8
OS name: "linux", version: "3.10.0-1160.71.1.el7.x86_64", arch: "amd64", family: "unix"[Pipeline] sh
+ mvn clean package -DskipTests=true
[INFO] Scanning for projects...
[INFO][INFO] --------------------------< com.example:demo >--------------------------
[INFO] Building demo 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO][INFO] --- maven-clean-plugin:3.1.0:clean (default-clean) @ demo ---
[INFO][INFO] --- maven-resources-plugin:3.2.0:resources (default-resources) @ demo ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Using 'UTF-8' encoding to copy filtered properties files.
[INFO] Copying 1 resource
[INFO] Copying 0 resource
[INFO][INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ demo ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to /home/jenkins/agent/workspace/maven-k8s-spring-boot-demo/target/classes
[INFO][INFO] --- maven-resources-plugin:3.2.0:testResources (default-testResources) @ demo ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Using 'UTF-8' encoding to copy filtered properties files.
[INFO] skip non existing resourceDirectory /home/jenkins/agent/workspace/maven-k8s-spring-boot-demo/src/test/resources
[INFO][INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-testCompile) @ demo ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to /home/jenkins/agent/workspace/maven-k8s-spring-boot-demo/target/test-classes
[INFO][INFO] --- maven-surefire-plugin:2.22.2:test (default-test) @ demo ---
[INFO] Tests are skipped.
[INFO][INFO] --- maven-jar-plugin:3.2.0:jar (default-jar) @ demo ---
[INFO] Building jar: /home/jenkins/agent/workspace/maven-k8s-spring-boot-demo/target/demo-0.0.1-SNAPSHOT.jar
[INFO][INFO] --- spring-boot-maven-plugin:2.4.5:repackage (repackage) @ demo ---
[INFO] Replacing main artifact with repackaged archive
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  5.688 s
[INFO] Finished at: 2022-12-17T09:54:34Z
[INFO] ------------------------------------------------------------------------
[Pipeline]}[Pipeline] // container
[Pipeline]}[Pipeline] // stage
[Pipeline]}[Pipeline] // node
[Pipeline]}[Pipeline] // podTemplate
[Pipeline] End of Pipeline
Finished: SUCCESS





























































相关实践学习
通过Ingress进行灰度发布
本场景您将运行一个简单的应用,部署一个新的应用用于新的发布,并通过Ingress能力实现灰度发布。
容器应用与集群管理
欢迎来到《容器应用与集群管理》课程,本课程是“云原生容器Clouder认证“系列中的第二阶段。课程将向您介绍与容器集群相关的概念和技术,这些概念和技术可以帮助您了解阿里云容器服务ACK/ACK Serverless的使用。同时,本课程也会向您介绍可以采取的工具、方法和可操作步骤,以帮助您了解如何基于容器服务ACK Serverless构建和管理企业级应用。 学习完本课程后,您将能够: 掌握容器集群、容器编排的基本概念 掌握Kubernetes的基础概念及核心思想 掌握阿里云容器服务ACK/ACK Serverless概念及使用方法 基于容器服务ACK Serverless搭建和管理企业级网站应用
相关文章
|
17天前
|
jenkins Devops Java
DevOps实践:Jenkins在持续集成与持续部署中的价值
【10月更文挑战第27天】在快速发展的软件开发领域,DevOps实践日益重要。Jenkins作为一款流行的开源自动化服务器,在持续集成(CI)和持续部署(CD)中扮演关键角色。本文通过案例分析,探讨Jenkins在Java项目中的应用,展示其自动化构建、测试和部署的能力,提高开发效率和软件质量。
39 2
|
5天前
|
运维 jenkins Java
Jenkins在持续集成与持续部署中的价值
Jenkins在持续集成与持续部署中的价值
|
9天前
|
存储 监控 Devops
DevOps实践:持续集成/持续部署(CI/CD)的实战指南
DevOps实践:持续集成/持续部署(CI/CD)的实战指南
|
18天前
|
jenkins Devops 测试技术
DevOps实践:Jenkins在持续集成与持续部署中的价值
【10月更文挑战第26天】随着DevOps理念的普及,Jenkins作为一款开源自动化服务器,在持续集成(CI)与持续部署(CD)中发挥重要作用。本文通过某中型互联网企业的实际案例,展示了Jenkins如何通过自动化构建、持续集成和持续部署,显著提升开发效率、代码质量和软件交付速度,帮助企业解决传统手工操作带来的低效和错误问题。
45 4
|
26天前
|
Kubernetes 持续交付 Docker
探索DevOps实践:利用Docker与Kubernetes实现微服务架构的自动化部署
【10月更文挑战第18天】探索DevOps实践:利用Docker与Kubernetes实现微服务架构的自动化部署
74 2
|
1月前
|
运维 监控 Devops
DevOps实践:持续集成与部署的自动化之旅
【10月更文挑战第7天】在软件开发领域,DevOps已成为提升效率、加速交付和确保质量的关键策略。本文将深入探讨如何通过实施持续集成(CI)和持续部署(CD)来自动化开发流程,从而优化运维工作。我们将从基础概念入手,逐步过渡到实际操作,包括工具选择、流程设计以及监控和反馈机制的建立。最终,我们不仅会展示如何实现这一自动化流程,还会讨论如何克服常见的挑战,以确保成功实施。
64 9
|
12天前
|
运维 Devops jenkins
DevOps实践之持续集成与持续交付
【10月更文挑战第32天】在软件开发的快节奏世界中,DevOps已经成为提升效率和质量的关键策略。通过将开发(Development)和运维(Operations)紧密结合,DevOps促进了更快速的软件发布和更高的可靠性。本文将深入探讨DevOps的核心组成部分——持续集成(CI)和持续交付(CD),并展示如何通过实际代码示例实现它们,以帮助团队构建更加高效和稳定的软件发布流程。
|
22天前
|
运维 安全 Devops
DevOps实践:持续集成与持续部署(CI/CD)的自动化之路
【10月更文挑战第22天】在软件交付的快速迭代中,DevOps文化和实践成为企业加速产品上市、保证质量和提升客户满意度的关键。本文将通过一个实际案例,深入探讨如何利用持续集成(Continuous Integration, CI)和持续部署(Continuous Deployment, CD)实现软件开发流程的高效自动化,包括工具选择、流程设计以及问题解决策略。我们将一起探索代码从编写到部署的全自动化旅程,揭示其对企业运维效率和产品质量所带来的深远影响。
|
9天前
|
Java Maven
maven项目的pom.xml文件常用标签使用介绍
第四届人文,智慧教育与服务管理国际学术会议(HWESM 2025) 2025 4th International Conference on Humanities, Wisdom Education and Service Management
62 8
|
7天前
|
Java 应用服务中间件 Maven
Maven的三种项目打包方式——pom,jar,war的区别
Maven 提供了多种打包方式,分别适用于不同类型的项目。pom 用于父项目或聚合项目,便于项目的结构和依赖管理;jar 用于Java类库或可执行的Java应用程序;war 则专用于Java Web应用程序的部署。理解这些打包方式的用途和特点,可以帮助开发者更好地配置和管理Maven项目,确保构建和部署过程的顺利进行。无论是单模块项目还是多模块项目,选择合适的打包方式对于项目的成功至关重要。
19 3