目 录CONTENT

文章目录

Jenkins Pipline 脚本示例

BKUN
2024-06-18 / 0 评论 / 0 点赞 / 509 阅读 / 332 字
// 获取当前日期
def current_date = new Date().format('yyyyMMdd')
// 获取当前构建号
def build_number = env.BUILD_NUMBER.toInteger()
// 服务器集合
def server_list = []
// 所有的脚本命令放在pipeline中
pipeline {
    // 指定任务在哪个集群节点中执行,any表示任意节点
    agent any

    parameters {
        string(description: '代码分支', name: 'CODE_BRANCH_PARAM', defaultValue: 'dev', trim: true)
    }

    tools {
        git 'Default'
    }

    // 声明全局变量,方便后面修改使用
    environment {
        CODE_ADDRESS = "http://git-address/xxx/xxx.git"
        // jenkins中创建的代码仓库密钥id
        CREDENTIALS_ID = 'xxx'
        DEFAULT_BUILD_TAG = "${current_date}-${build_number}"
    }

    stages {
        stage('拉取代码') {
            steps {
                // BRANCH为构建分支参数
                git branch: "${CODE_BRANCH_PARAM}", credentialsId: "${CREDENTIALS_ID}", url: "${CODE_ADDRESS}"
            }
        }
        stage('maven构建') {
            steps {
                powershell """
                  mvn -s /xxx/settings.xml clean install
                """
            }
        }
        stage('Jar归档') {
            steps {
                archive 'target/*.jar'
            }
        }
        stage('上传至服务器') {
            steps {
                sshPublisher(publishers: [sshPublisherDesc(configName: 'xxx', transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: 'ls', execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: '/app', remoteDirectorySDF: false, removePrefix: 'target', sourceFiles: 'target/xxx.jar')], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)])
            }
        }
    }

    // 通知内容
    post {
        always {
            echo 'This will always run'
        }
        success {
            echo 'This will run only if successful'
        }
        failure {
            echo 'This will run only if failed'
        }
        unstable {
            echo 'This will run only if unstable'
        }
        aborted {
            echo 'This will run only if aborted'
        }
    }
}
0

评论区