跳至主要內容

Vue的Jenkins自动构建配置

pptg大约 2 分钟

1. 配置步骤

  1. 配置项目地址,这个地址用于在构建时选择需要构建项目分支,请填写真实的项目地址。
项目地址
项目地址
  1. 配置脚本地址,这里会从远程的jenkins-file中读取构建的过程,所以写jenkins-file的地址,Credentials写可以正常拉这个jenkins-file的git账户、密码 脚本地址

  2. 配置脚本路径,配置jenkins-file项目下的文件路径 脚本路径

2. 脚本内容

@Library('JenkinsLibrary@master') _

def tools = new org.devops.tools()
def build = new org.devops.build()

pipeline {
    agent {
        node {
            // 填写jenkins的节点
            label "192.168.55.8"

}        }
    }
    environment {
        GIT_URL = "xx" // 项目的git 地址
        GITLAB_ADMIN_USER = "xx" // 项目的git账号密码
        NEXUS_DOCKER_ADDR = "xx" //nexus登录地址, 用于向其推送镜像
        DOCKER_IMAGE_PRE = "xx" //推送docker镜像的前缀
        BUILD_TYPE = "npm" //构建类型
        SHELL_PATH = "/home/cop/web/deploy.sh" //部署脚本的路径
        SERVICE_NAME = "web-cop" //web服务名称
    }
    options {
        buildDiscarder logRotator(artifactDaysToKeepStr: '', artifactNumToKeepStr: '', daysToKeepStr: '', numToKeepStr: '5')
        timeout(time: 20, unit: 'MINUTES') //超时停止流水线
    }
    parameters {
        gitParameter name: 'BRANCH_TAG',
                    type: 'PT_BRANCH_TAG',
                    branchFilter: 'origin/(.*)',
                    defaultValue: 'origin/dev',
                    useRepository: '.*.git',
                    description: '选择分支或标签构建'
        booleanParam defaultValue: false, description: '是否跳过构建阶段,勾选表示不构建代码,直接使用镜像仓库的镜像直接部署到目标服务器', name: 'skipBuild'
        choice choices: ['192.168.55.3'], description: '部署服务器:3是工作手机前端演示服务器', name: 'publisherServer'
    }
    stages {
        stage("拉取代码") {
            steps {
                script {
                    tools.PrintMes("拉取代码开始", "green")
                    checkout([$class: 'GitSCM',
                            branches: [[name: "${params.BRANCH_TAG}"]],
                            doGenerateSubmoduleConfigurations: false,
                            extensions: [],
                            gitTool: 'Default',
                            submoduleCfg: [],
                            userRemoteConfigs: [[credentialsId: "${GITLAB_ADMIN_USER}", url: "${GIT_URL}"]]
                            ])
                    tools.PrintMes("拉取代码结束", "green")
                }
            }
        }
        stage("构建代码") {
            steps {
                script {
                    tools.PrintMes("构建代码开始", "green")
                    echo "skipBuild=========${skipBuild}"
                        if (skipBuild == 'false') {
                            build.Build(BUILD_TYPE, "install --legacy-peer-deps")
                            build.Build(BUILD_TYPE, "run build")
                        } else {
                            tools.PrintMes("已跳过npm构建", "red")
                        }
                    tools.PrintMes("构建代码结束", "green")
                }
            }
        }
        stage("构建并推送镜像") {
            steps {
                script {
                    tools.PrintMes("构建镜像开始", "green")

                    def props = readJSON file: './package.json'
                    version = props.version
                    echo "version=========${version}"
                    if (skipBuild == 'false') {
                        withCredentials([usernamePassword(credentialsId: 'nexus-docker-user', passwordVariable: 'password', usernameVariable:  'username')]) {
                            sh """
                                docker login -u ${username} -p ${password} ${NEXUS_DOCKER_ADDR}
                            """
                        }
                        dir("${env.WORKSPACE}") {
                            //sh "docker build -f ${env.WORKSPACE}/docker-build/web.Dockerfile -t ${DOCKER_IMAGE_PRE}/${SERVICE_NAME}:${version} ."
                            sh "docker buildx build --platform linux/arm64,linux/amd64 -f ${env.WORKSPACE}/docker/Dockerfile -t ${DOCKER_IMAGE_PRE}/${SERVICE_NAME}:${version} . --push"
                        }
                        //sh "docker rmi ${DOCKER_IMAGE_PRE}/${SERVICE_NAME}:${version}" //删除本地镜像
                    } else {
                        tools.PrintMes("已跳过镜像构建与推送", "red")
                    }
                    tools.PrintMes("构建镜像结束", "green")
                }
            }
        }
        stage("远程部署") {
            steps {
                script {
                    tools.PrintMes("远程部署开始", "green")
                    //目标服务器执行脚本
                     sshPublisher(
                        alwaysPublishFromMaster: true,
                        publishers: [
                            sshPublisherDesc(
                                configName: "${publisherServer}",
                                transfers: [
                                    sshTransfer(
                                        execCommand: "${SHELL_PATH} ${DOCKER_IMAGE_PRE} ${SERVICE_NAME} ${version}",
                                        execTimeout: 600000
                                    )
                                ],
                                verbose: true![Vue的Jenkins自动构建配置4.png](Vue的Jenkins自动构建配置4.png)
                            )
                        ]
                    )
                    tools.PrintMes("远程部署结束", "green")
                }
            }
        }
    }
    //构建后操作
    post {
        always { //总是执行脚本片段
            script {
                tools.PrintMes("本次构建操作结束", "red")
            }
        }
        success { //成功后执行
            script {
                currentBuild.description = "构建成功!"
            }
        }
        failure { //失败后执行
            script {
                currentBuild.description = "构建失败!"
            }
        }
        aborted { //取消后执行
            script {
                currentBuild.description = "构建取消!"
            }
        }
    }
}