Pipeline

作者: 快去学习不然怎么去看aimer | 来源:发表于2019-11-19 20:52 被阅读0次

Pipeline(流水线):支持两种语法格式-------->声明式,脚本式流水线

声明式

基本格式:
pipeline {
    agent none
    stages {
        stage('Build') {
            agent any 
            steps {
                echo 'Building..'
            }
        }
        stage('Test') {
            agent {docker { 'python3:latest ' }}
            steps {
                echo 'Testing..'
                sh 'pwd'
            }
        }
    }
}

脚本式流水线

基本语法(SSHPUT):
node {
  def remote = [:]
  remote.name = 'test'
  remote.host = 'test.domain.com'
  remote.user = 'root'
  remote.password = 'password'
  remote.allowAnyHosts = true
  stage('Remote SSH') {
    writeFile file: 'abc.sh', text: 'ls -lrt'
    sshPut remote: remote, from: 'abc.sh', into: '.'
  }
}

TEST Pipeline:

README

新建任务 ---> 项目名,选择Pipeline项目类型 ---> 项目描述 --->在流水线块中填写相关内容

定义  ------> Pipeline script (在下方脚本处编写Pipeline)
       |
        ----> Pipeline script from SCM(选择git,指定Pipeline文件,当然该文件应当处于项目的根目录)

点击流水线语法,选择相应类型,生成流水线脚本,将该脚本添加到setups中,即可执行

agent 可以写在最外面指定全局,也可以写在每一步里单独指定

测试Pipeline

#git命令由流水线语法生成
pipeline {
   agent any

   stages {
      stage('build') {
         steps {
            echo 'Rou Rou'
         }
}
      stage('test') {
         steps {
            echo '这是第二步'
            git credentialsId: '71fbae7c-c1cb-4574-aa37-cd92d1f5c7ab', url: 'git@172.18.0.5:root/auto-cmdb.git'
         }
}
     stage('deploy') {
         steps {
            echo '这是第三步'
         }
      }
   }
}
这是一个最简单的Pipeline文件,构建该工程,可以查看到相应输出并在工作区看到clone的内容
测试Pipeline.png

指定资源拥有者(即为当前登陆用户)

#当前用户若为指定的alice或bob时,点击提交时才会继续执行。若为admin就直接执行,否则即为权限拒绝
pipeline {
    agent any
    stages {
        stage('Example') {
            input {
                message "Should we continue?"
                ok "Yes, we should."
                submitter "alice,bob"
                parameters {
                    string(name: 'PERSON', defaultValue: 'Mr Jenkins', description: 'Who should I say hello to?')
                }
            }
            steps {
                echo "Hello, ${PERSON}, nice to meet you."
            }
        }
    }
}
指定资源拥有者.png jenkins-erroruser.png

使用指定容器

#指定容器,若本地不存在该容器,则先pull该容器
pipeline {
    agent { docker {
               image 'maven:3-alpine'
     }
 } 
    stages {
        stage('输出版本信息') {
            steps {
                sh 'mvn -version'
            }
        }
    }
}
docker-Pipeline.png

Pipeline部署maven项目

pipeline {
    agent none
    stages {
        stage('git file') {
            agent any
            steps {
                git credentialsId: '71fbae7c-c1cb-4574-aa37-cd92d1f5c7ab', url: 'git@172.18.0.5:root/maventest.git'
            }
        }
        stage('build maven') {
            agent { docker {image 'maven:3-alpine'
                                         args '-v /root:/root'} } 
            steps {
                sh 'mvn --version'
            }
        }
        stage('copy file') {
            agent any
            steps {
                sh 'cp -r $WORKSPACE/* /root'
            }
        }
        stage('build mavens') {
            agent { docker {image 'maven:3-alpine'
                                         args '-v /root:/root'} } 
            steps {
                sh 'mvn -B -DskipTests clean package'
            }
        }
    }
}

当然,由于Pipeline的所做的操作位于WORKSPACE,因此,上述代码可以简写为

pipeline {
    agent none
    stages {
        stage('git file') {
            agent any
            steps {
                git credentialsId: '71fbae7c-c1cb-4574-aa37-cd92d1f5c7ab', url: 'git@172.18.0.5:root/maventest.git'
            }
        }
        stage('build mavens') {
            agent { docker {image 'maven:3-alpine'
                                         args '-v /root:/root'} } 
            steps {
                sh 'mvn -B -DskipTests clean package'
            }
        }
    }
}

相关文章

网友评论

      本文标题:Pipeline

      本文链接:https://www.haomeiwen.com/subject/pmugictx.html