pipeline学习

一、常用语法

1、拉取git仓库代码

checkout([
        $class: 'GitSCM',
        branches: [[name: 代码分支名称]],
        doGenerateSubmoduleConfigurations: false,
        userRemoteConfigs: [[credentialsId: 权限, url: 代码地址]]
])

2、cleanWs()或者deleteDir(), 删除工作目录

// 删除${WORKSPACE}目录
cleanWs()

// 判断目录是否存在
dir("${env.WORKSPACE}@tmp") {
    //删除${WORKSPACE}@tmp目录
    deleteDir()
}

3、withCredentials ,获取用户名和密码

steps{
    withCredentials([usernamePassword(credentialsId: 'user_for_openshift', passwordVariable: 'password', usernameVariable: 'username')]) {
    sh 'docker login -u $username -p $password registory.ctiwifi.cn:5000
    }
}

4、readJSON读取json文件

def readJsonFile(filePath) {
    def propMap = readJSON file: filePath
    return propMap
}

二、基础使用




  • pipeline代码
pipeline {
  agent any
  stages {
    stage('初始化') {
        steps {
            echo '初始化。。。'
        }
    }
    stage('打包') {
        steps {
            echo '打包。。。'
        }
    }
    stage('测试') {
        steps {
            echo '测试。。。'
        }
    }
    stage('发布') {
        steps {
            echo '发布。。。'
        }
    }
  }
}

三、使用 Groovy 沙盒






  • pipeline代码
pipeline {
  agent any
  stages {
    stage('初始化') {
        steps {
            echo '初始化。。。'
        }
    }
    stage('打包') {
        steps {
            echo 'git代码拉取。。。'
            checkout([$class: 'GitSCM', branches: [[name: '*/master']], extensions: [], userRemoteConfigs: [[credentialsId: 'a8db6793-cc2b-4d82-bd3d-c5beb1c5149e', url: 'http://192.168.3.11/root/rapid-screen.git']]])
            
            echo 'mvn打包。。。'
            sh "/usr/local/apache-maven-3.8.2/bin/mvn -U clean install"
        }
    }
    stage('测试') {
        steps {
            echo '测试。。。'
        }
    }
    stage('发布') {
        steps {
            echo '发布。。。'
        }
    }
  }
}

四、参数化构建过程


  • pipeline代码
pipeline {
  agent any
  parameters {
    booleanParam(name: 'ENABLE_BACKEND_BUILD', defaultValue: true, description: '开启后端构建')
    booleanParam(name: 'ENABLE_DEPLOY', defaultValue: true, description: '开启部署')
  }
  stages {
    stage('初始化') {
        steps {
            echo '初始化。。。'
        }
    }
    stage('打包') {
        when{
            expression{params.ENABLE_BACKEND_BUILD}
        }
        steps {
            echo '打包。。。'
            ws("backend_build"){
                dir("my_docker"){
                    echo 'git代码拉取。。。'
                    checkout([$class: 'GitSCM', branches: [[name: '*/master']], extensions: [], userRemoteConfigs: [[credentialsId: 'a8db6793-cc2b-4d82-bd3d-c5beb1c5149e', url: 'http://192.168.3.11/root/rapid-screen.git']]])
            
                    echo 'mvn打包。。。'
                    sh "/usr/local/apache-maven-3.8.2/bin/mvn -U clean install"
                }
                echo '打包完成。。。'
                // 删除backend_build工作目录
                cleanWs()
            }
        }
    }
    stage('测试') {
        steps {
            echo '测试。。。'
        }
    }
    stage('发布') {
        when{
            expression{params.ENABLE_DEPLOY}
        }
        steps {
            echo '发布。。。'
        }
    }
  }
}

五、pipeline script from SCM

1、新建一个docker-build项目并配置Jenkinsfile,如下

2、新建pipeline项目,选择pipeline script from SCM,git设置为上面新建的docker-build项目

六、参考

posted @ 2021-11-11 16:50  吕林光  阅读(773)  评论(0编辑  收藏  举报