Skip to content

前端项目构建发布

创建一个流水线任务

  • 前面的配置无所谓,贴上我的配置

流水线基础配置

  • 先贴上流水线脚本
Groovy
def lastCommitMessage
def gitUrl = 'git@xxxxxxxx.git'
def branch = 'develop'
def buildScript = 'pnpm build'
def privateKey = 'xxxxxxxxxxxxxx'
def remote = [:]
remote.name = 'xxxxxxxxx'
remote.host = '192.168.1.1'
remote.user = 'root'
remote.allowAnyHosts = true
def distPath = './dist/'
def remotePath = '/usr/share/nginx/html/xxx/'

pipeline {
    agent any

    stages {
        stage('拉取代码') {
            steps {
                git branch: branch, credentialsId: privateKey, url: gitUrl
            }
        }
        stage('获取最后一次提交的消息') {
            steps {            
                script {
                    lastCommitMessage = sh(returnStdout: true, script: 'git log -1 --pretty=%B').trim()
                    echo "Last Commit Message: ${lastCommitMessage}"
                }
            }
        }
        stage('安装依赖') {
            steps {
                nodejs('node18') {
                    sh 'pnpm i -r --no-frozen-lockfile'
                }
            }
        }
        stage('构建') {
            steps {
                nodejs('node18') {
                    sh buildScript
                }
                dir(distPath) {
                    echo pwd()
                    zip defaultExcludes: false, dir: '', exclude: '', glob: '', zipFile: 'dist.zip'
                }
            }
        }
        stage('上传到服务器') {
            steps {
                withCredentials([sshUserPrivateKey(credentialsId: privateKey, keyFileVariable: 'identity')]) {
                    script {
                        remote.identityFile = identity
                        sshCommand remote: remote, command: "rm -rf ${remotePath}*"
                        dir(distPath) {
                            sshPut remote: remote, from: 'dist.zip', into: remotePath
                        }
                        sshCommand remote: remote, command: "cd ${remotePath} && unzip dist.zip && rm dist.zip"
                    }
                }
                
            }
        }
        
    }
    post {
        success {
            wxwork(
                robot: 'CODING',
                type: 'markdown',
                text: [
                    "# [**${JOB_NAME}**](${JOB_URL})",
                    "> 发布结果:<font color='info'>**成功**</font>",
                    "> 构建时长:<font color='comment'>**${currentBuild.durationString}**</font>",
                    "> 本地更新:${lastCommitMessage}",
                    "> 构建ID:[${BUILD_DISPLAY_NAME}](${BUILD_URL})"
                ]
            )
        }
        failure {
            wxwork(
                robot: 'CODING',
                type: 'markdown',
                text: [
                    "# [**${JOB_NAME}**](${JOB_URL})",
                    "> 发布结果:<font color='warning'>**失败**</font>",
                    "> 构建时长:<font color='comment'>**${currentBuild.durationString}**</font>",
                    "> 本地更新:${lastCommitMessage}",
                    "> 构建ID:[${BUILD_DISPLAY_NAME}](${BUILD_URL})"
                ]
            )
        }
    }
}

TIP

至此已发布完成