jenkins部署(最新版)

 jenkins中文社区

https://www.jenkins.io/zh/doc/book/installing/

1、系统管理

 

 

 

 

 

 

 

   #邮件内容模板文档:https://blog.csdn.net/J_____Q/article/details/118518554

 

   

 

   #进行邮件测试,看是否可以收到邮件

 

 

  #邮箱收件

 

2、全局工具配置

2.1、maven配置

(1)下载jdk1.8包,自行下载

(2)下载apache-maven-3.6.3包,官网地址:https://maven.apache.org/download.cgi

(3)maven配置文件修改

 

 

(4)设置环境变量

cat /etc/profile
#最后添加,安装目录自定义
export JAVA_HOME=/data/jdk1.8.0_152
export MAVEN_HOME=/data/apache-maven-3.6.3
export PATH=$JAVA_HOME/bin:$PATH:$MAVEN_HOME/bin
#刷新环境变量
source /etc/profile

(5)版本查看

 

 (6)jenkins页面配置

#jdk

 

 

 #git

 

 #maven

 

 3、全局安全配置(主要是用户访问项目控制)

 

 

4、创建任务

(1)首先创建文件夹,区分环境

 

 

(2)在test分支下新建任务

 

 

 #安装插件Gitlab

 

#使用gitlab插件,监听gitlab分支时间

 #生成secret token密钥

 #gitlab中进行配置

 

#保存之后报错

 

#问题处理(使用管理员登陆)

 

#使用刚刚的用户登陆gitlab,重新添加url和token

 

 

 

 

 

pipeline {
    agent any
    //全局变量
    environment{
      BRANCH = "ruoyi-dev"
    }
    
    stages {
        //获取代码
        stage('git pull') {
            steps {
                git branch: 'devops', credentialsId: 'dc2de64a-9639-4b92-abde-dd163b326691', url: 'git@172.16.4.53:lipc/my-ruoyi.git'
                
            }
        }
        //java 打包
        stage('mvn build') {
            steps {
                sh '''mvn clean
                mvn package -Dmaven.test.skip=true'''
            }
        }
        //打镜像
        stage('image build') {
            steps {
                sh '''VERSION=$(cat version.txt | egrep -iw "version|[0-9]$" | head -1 | awk -F\'-\' \'{print $NF}\' | awk -F\'@\' \'{print $1}\')
                cp -f ./ruoyi-admin/target/ruoyi-admin.jar ./
                docker login -u admin -p 111111 172.16.4.17:8090
                docker build . -t 172.16.4.17:8090/${BRANCH}/ruoyi-server:${VERSION}
                docker push  172.16.4.17:8090/${BRANCH}/ruoyi-server:${VERSION}
                sed -i "s#hosts:.*#hosts: ${BRANCH}#g" ./ry.yml
                sed -i "s#branch:.*#branch: ${BRANCH}#g" ./ry.yml
                sed -i "s#version:.*#version: ${VERSION}#g" ./ry.yml
                '''
            }
        }
        //部署服务
        stage('devops service') {
            steps {
                sh '''ansible-playbook -i /etc/ansible/hosts ./ry.yml
                '''
            }
        }
    }
    post {
        success {
            // 构建成功后发送邮件
            emailext(
                subject: "构建成功",
                body: "构建已成功完成,详情请查看构建日志",
                to: "359008179@qq.com",
            )
        }
        failure {
            // 构建失败后发送邮件
            emailext(
                subject: "构建失败",
                body: "构建失败,请查看构建日志以获取更多信息",
                to: "3590022179@qq.com",
            )
        }
    }
}

 #说明,两份代码区别是发送邮件部分,上边代码是通过当前代码直接通知用户,下边代码是调用jenkins系统配置中的邮件内容模板发送邮件,

pipeline {
    agent any
    //全局变量
    environment{
      BRANCH = "ruoyi-dev"
    }
    
    stages {
        //获取代码
        stage('git pull') {
            steps {
                git branch: 'devops', credentialsId: 'dc2de64a-9639-4b92-abde-dd163b326691', url: 'git@172.16.4.53:lipc/my-ruoyi.git'
                
            }
        }
        //java 打包
        stage('mvn build') {
            steps {
                sh '''mvn clean
                mvn package -Dmaven.test.skip=true'''
            }
        }
        //打镜像
        stage('image build') {
            steps {
                sh '''VERSION=$(cat version.txt | egrep -iw "version|[0-9]$" | head -1 | awk -F\'-\' \'{print $NF}\' | awk -F\'@\' \'{print $1}\')
                cp -f ./ruoyi-admin/target/ruoyi-admin.jar ./
                docker login -u admin -p ytx@1234 172.16.4.17:8090
                docker build . -t 172.16.4.17:8090/${BRANCH}/ruoyi-server:${VERSION}
                docker push  172.16.4.17:8090/${BRANCH}/ruoyi-server:${VERSION}
                sed -i "s#hosts:.*#hosts: ${BRANCH}#g" ./ry.yml
                sed -i "s#branch:.*#branch: ${BRANCH}#g" ./ry.yml
                sed -i "s#version:.*#version: ${VERSION}#g" ./ry.yml
                '''
            }
        }
        //部署服务
        stage('devops service') {
            steps {
                sh '''ansible-playbook -i /etc/ansible/hosts ./ry.yml
                '''
            }
        }
        //发送邮件
        stage('Send email') {
            steps {
                emailext (
                    subject: "Jenkins Build Notification",
                    to: "3590081229@qq.com",
                    body: '${DEFAULT_CONTENT}'
                )
            }
        }
    }
   
}

 

#系统配置邮件模板

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>${PROJECT_NAME}-第${BUILD_NUMBER}次构建日志</title>
</head>
 
<body leftmargin="8" marginwidth="0" topmargin="8" marginheight="4"
    offset="0">
    <table width="95%" cellpadding="0" cellspacing="0"
        style="font-size: 11pt; font-family: Tahoma, Arial, Helvetica, sans-serif">
        <tr>
            <td>(本邮件是程序自动下发的,请勿回复!)<br/></td>
        </tr>
        <tr>
            <td><h2>
                    <font color="#0000FF">构建结果 - ${BUILD_STATUS}</font>
                </h2></td>
        </tr>
        <tr>
            <td><br />
            <b><font color="#0B610B">构建信息</font></b>
            <hr size="2" width="100%" align="center" /></td>
        </tr>
        <tr>
            <td>
                <ul>
                    <li>项目名称 : ${PROJECT_NAME}</li>
                    <li>构建编号 : 第${BUILD_NUMBER}次构建</li>
                    <li>触发原因: ${CAUSE}</li>
                    <li>构建日志: <a href="${BUILD_URL}console">${BUILD_URL}console</a></li>
                    <li>构建  Url : <a href="${BUILD_URL}">${BUILD_URL}</a></li>
                    <li>工作目录 : <a href="${PROJECT_URL}ws">${PROJECT_URL}ws</a></li>
                    <li>项目  Url : <a href="${PROJECT_URL}">${PROJECT_URL}</a></li>
                </ul>
            </td>
        </tr>
        <tr>
            <td><b><font color="#0B610B">Changes Since Last Successful Build:</font></b>
            <hr size="2" width="100%" align="center" /></td>
        </tr>
        <tr>
            <td>
                <ul>
                    <li>历史变更记录 : <a href="${PROJECT_URL}changes">${PROJECT_URL}changes</a></li>
                </ul> ${CHANGES_SINCE_LAST_SUCCESS,reverse=true, format="Changes for Build #%n:<br />%c<br />",showPaths=true,changesFormat="<pre>[%a]<br />%m</pre>",pathFormat="    %p"}
            </td>
        </tr>
        <tr>
            <td> <hr size="2" width="100%" align="center" /></td>
        </tr>
        <tr>
            <td></td>
        </tr>
        <tr>
            <td><b><font color="#0B610B">构建情况总览:</font></b>${TEST_COUNTS,var="fail"}<br/>
            <hr size="2" width="100%" align="center" /></td>
        </tr>
        <tr>
            <td><textarea cols="80" rows="30" readonly="readonly"
                    style="font-family: Courier New">${BUILD_LOG,maxLines=23}</textarea>
            </td>
        </tr>
    </table>
</body>
</html>

 

 

#自动触发,构建测试,在gitlab对应的项目上随便修改一个文件,或者在本地重新推送一次代码,就可以出发自动构建。

 #邮件信息

 

到此jenkins基本使用就完成了,后边继续更新多分支构建

posted @ 2023-04-21 18:17  Leonardo-li  阅读(638)  评论(0编辑  收藏  举报