Jenkinsfile 入门指南

1,527 阅读1分钟

本人也刚刚入门学习Jenkinsfile,本文给出简单的使用说明,不正确的步骤望大佬指教。

一、Blue Ocean 安装

在插件库安装

image.png

重启jenkins 二、工程创建

image.png 三、第一个Jenkinsfile

由于Jenkinsfile支持groovy语法,所以下面有用该方式写的一些工具文件

/**
 *
 */
return this

/**
 * 拉取代码
 * @param gitUrl
 * @param gitBranch
 * @param credentialsId
 * @return
 */
def gitClone(String gitUrl, String gitBranch, String credentialsId='11eb2bd6-050b-4853-9dd6-8118a8d3e370') {
    checkout([
        $class: 'GitSCM',
        branches: [[name: gitBranch]],
        doGenerateSubmoduleConfigurations: false,
        extensions: [[
            $class: 'SubmoduleOption',
            disableSubmodules: false,
            parentCredentials: true,
            recursiveSubmodules: true,
            reference: '',
            trackingSubmodules: false
        ]],
        submoduleCfg: [],
        userRemoteConfigs: [[credentialsId: credentialsId, url: gitUrl]]
    ])
}

/**
 * ssh发布 如果有文件上传,则会上传到对应用户的根目录下
 * @param ip
 * @param sourceFiles
 * @param remoteDirectory
 * @param removePrefix
 * @param execCommand       远程执行命令
 * @return
 */
def sshPublish(String ip,String sourceFiles,String remoteDirectory,String removePrefix,String execCommand){
    sshPublisher(
        publishers: [
            sshPublisherDesc(
                configName: '192.168.137.100',
                transfers: [
                    sshTransfer(
                        cleanRemote: false,
                        excludes: '',
                        execCommand: 'ls',
                        execTimeout: 120000,
                        flatten: false,
                        makeEmptyDirs: false,
                        noDefaultExcludes: false,
                        patternSeparator: '[, ]+',
                        remoteDirectory: '/temp/springboot-ci/',
                        remoteDirectorySDF: false,
                        removePrefix: 'target',
                        sourceFiles: 'target/*.jar'
                    )
                ],
                usePromotionTimestamp: false,
                useWorkspaceInPromotion: false,
                verbose: true
            )
        ]
    )
}
pipeline {
    agent any
    environment {
        APP_VERSION = ''
        APP_DIR = 'APP_WORKSPACE'
        APP_WORKSPACE = '${WORKSPACE}/${APP_DIR}'
        COMMIT_MSG = ''
        APP_CODE = """${sh(
                          returnStdout: true,
                          script: 'echo ${SYSTEM_NAME#*:}'
                  ).trim()}"""
        APP_QCBD = """${sh(
                          returnStdout: true,
                          script: 'echo ${QCBD#*:}'
                  ).trim()}"""
    }
    parameters {
        choice(name: 'SYSTEM_NAME', choices: ['门店系统:sms', '运维系统:cos'], description:'请选择服务')
        string(name: 'BRANCH', defaultValue: '', description: '分支名')
        choice(name: 'QCBD', choices: ['普通发布:NO', '快速发布:YES'], description:'发布类型')
    }
    post {
        always {
            pwd()
        }
    }
    stages {
        stage('Init') {
            steps {
                script {
                    utils = load "./script/utils.groovy"
                    utils.gitClone('http://192.168.137.200/CI/springboot-ci.git','*/${BRANCH}')
                }
            }
        }
        stage('Build') {
            steps {
                script {
                    echo 'Building..'
                    sh "pwd"
                    if(env.APP_QCBD == 'YES'){
                        echo "执行快速发布不打包,利用上次打包后的文件"
                    }else{
                        sh '/usr/local/maven/bin/mvn clean package -Dmaven.test.skip=true'
                    }
                }
            }
        }
        stage('Deploy') {
            steps {
                echo 'Deploying....'
                script{
                    utils = load "./script/utils.groovy"
                    utils.sshPublish('192.168.137.100','target/*.jar','/temp/springboot-ci/','target','ls')
                }
            }
        }
    }
}