DevOps

jenkins-Pipeline

2021-11-16  本文已影响0人  小李飞刀_lql

Pipeline

安装pipeline插件

pipeline

HelloWord

001 新建流水线项目

1634890212168.png

002 流水线配置

1634890300288.png

pipepline构建

001 整体框架

pipeline {
  agent any
  stages {
    stage('拉取代码') {
      steps {
        echo 'git clone....'
      }
    }
    stage('编译构建') {
      steps {
        echo 'mvn....'
      }
    }
    stage('测试') {
      steps {
        echo 'test....'
      }
    }
    stage('部署') {
      steps {
        echo 'deploy....'
      }
    }
  }
}

002 生成拉取代码脚本

流水线语法
片段生成器

003 生成git脚本

1634893985726.png
checkout([$class: 'GitSCM', branches: [[name: '*/master']], extensions: [], userRemoteConfigs: [[credentialsId: '711e362f-2255-4c8c-bb4a-a81353e2ee29', url: 'http://192.168.153.18/test/demo.git']]])

004 生成shell脚本

1634961877210.png
sh 'ls'

sh 'pwd'

005 相关脚本完善

pipeline {
  agent any
  stages {
    stage('拉取代码') {
      steps {
        checkout([$class: 'GitSCM', branches: [[name: '*/master']], extensions: [], userRemoteConfigs: [[credentialsId: '711e362f-2255-4c8c-bb4a-a81353e2ee29', url: 'http://192.168.153.18/test/demo.git']]])
        sh 'ls'
        sh 'pwd'  
      }
    }
    stage('编译构建') {
      steps {
        echo 'mvn....'
      }
    }
    stage('测试') {
      steps {
        echo 'test....'
      }
    }
    stage('部署') {
      steps {
        echo 'deploy....'
      }
    }
  }
}

Pipeline指令

001 agent 指定在某个节点上执行

  agent {
      label 'web1'
  }
  steps {
        checkout([$class: 'GitSCM', branches: [[name: '*/master']], extensions: [], userRemoteConfigs: [[credentialsId: '711e362f-2255-4c8c-bb4a-a81353e2ee29', url: 'http://192.168.153.18/test/demo.git']]])
        sh 'ls'
        sh 'ip addr'  
      }

002 options(主要针对超时)

针对所有阶段的超时

如果任何一个阶段,执行超过1小时,终止当前流水线

pipeline {
    agent any
    options {
        timeout(time: 1, unit: 'HOURS') 
    }
    stages {
        stage('Example') {
            steps {
                echo 'Hello World'
            }
        }
    }
}

针对某个阶段设置超时

pipeline {
    agent any
    stages {
        stage('Example') {
            options {
                timeout(time: 1, unit: 'HOURS') 
            }
            steps {
                echo 'Hello World'
            }
        }
    }
}

003 environment

引用变量要使用双引号

pipeline {
    agent any
    environment { 
        branch = 'master'
    }
    stages {
        stage('Example') {
            steps {
                sh 'ip addr'
                sh "echo $branch"
            }
        }
    }
}
1634962728656.png
parameters {
  choice choices: ['dev', 'test', 'prod'], description: '请选择要发布的环境', name: 'ENV'
}

005 综合脚本

pipeline {
  agent {
    label 'web1'
  }
  options {
    timeout(time: 1, unit: 'HOURS') 
  }
  environment { 
    branch = 'master'
  }
  parameters {
    choice choices: ['dev', 'test', 'prod'], description: '请选择要发布的环境', name: 'ENV'
  }
  stages {
    stage('拉取代码') {
      steps {
        checkout([$class: 'GitSCM', branches: [[name: '*/master']], extensions: [], userRemoteConfigs: [[credentialsId: '711e362f-2255-4c8c-bb4a-a81353e2ee29', url: 'http://192.168.153.18/test/demo.git']]])
        sh 'ls'
        sh 'pwd'  
        sh 'ip addr'
        sh "echo $branch"
      }
    }
    stage('编译构建') {
      steps {
        echo 'mvn....'
      }
    }
    stage('测试') {
      steps {
        echo 'test....'
      }
    }
    stage('部署') {
      steps {
        echo 'deploy....'
      }
    }
  }
}

与Gitlab一起管理

001 项目中添加Jenkinsfile文件

1634967336633.png

002 绑定项目

1634967555479.png 1634967592300.png
上一篇 下一篇

猜你喜欢

热点阅读