Jenkins pipeline在docker执行测试代码

背景

开发提交代码后,由Jenkins去自动执行测试代码,如果同时有多个提交那么会开启多个任务,就会导致基础数据构造出问题(公用同一个数据库),因此考虑用docker环境进行数据隔离,以达到同时运行多个任务

环境

服务器已安装Jenkins+docker
注意:

  1. Jenkins用户需要添加到docker组usermod -aG docker jenkins
  2. 已安装docker pipeline插件,不然会出现docker命令无法执行的错误

pipeline配置

  • 镜像已安装好数据库mysql
  • 需要注意启动docker时用户的设置(args '-u 0:0'),-u 0:0 表示使用root用户
  • 配置信息如下
pipeline {
    agent {
                docker {
                    image 'image-name'
                    args '-u 0:0 --ipc=host'
                }
            }
    stages {
        stage('check out') {
            steps {
                checkout([$class: 'GitSCM', branches: [[name: "*/${params.Branch}" ]], extensions: [], userRemoteConfigs: [[credentialsId: '*********', url: 'https://url.git']]])
            }
         }
        stage('build') {
            steps {
                sh '/etc/init.d/mysql start'
                sh '服务器上执行的命令,构建服务器,并启动'
            }
        }
        stage('test') {
            steps {
              dir('dirname'){
                sh 'dir到指定目录下执行对应命令'
                }
               sh '执行测试代码命令'
            }
        }
    }
    post {
            always {
                // One or more steps need to be included within each condition's block.
                publishHTML([allowMissing: false, alwaysLinkToLastBuild: true, keepAll: true, reportDir: './spec/mochawesome-report', reportFiles: 'merge_report.html', reportName: 'CYPRESS Report', reportTitles: 'cypress_report'])
            }
            failure {
                emailext(
                subject: "subject", 
                    to: 'useremail', 
                    mimeType: 'text/html',
                    body: "bodycontent"
                    
                )
            }
        }
}

Jenkins pipeline在docker执行测试代码_第1张图片结合webhook,完美实现提交pull request,自动构建环境执行测试代码

你可能感兴趣的:(jenkins,docker,CI/CD,pipeline,Jenkins,docker)