Jenkins2 学习系列27 -- pipeline 中 Docker 操做

构建任务在指定Docker镜像中进行

Jenkins的agent用来指定跑构建的机器或Docker镜像。因为Docker拥有良好的隔离性,可以保证每次的构建是全新的。 以下面例子,经过agent {docker {image : 'xxx'}} 首先pull一个我打包好的基于ubuntu的node镜像,这个镜像里面已经包含了nodejs10, wget, zip, curl, python,chrome,firefox, aws-cli 等经常使用工具,能够方便的在里面执行npm install,npm run test 启动浏览器跑测试等。node

pipeline {
  agent {
    docker {
       image 'finleyma/circleci-nodejs-browser-awscli'
    }
  }
  stage('Checkout') {
       steps {
          git branch: 'develop', credentialsId: 'github-private-key', url: 'git@github.com:your-name/angular-web.git'
     }
  }
  stage('Node modules') {
     steps {
        sh 'npm install'
     }
   }
  stage('Code Lint') {
     steps {
        sh 'npm run lint'
     }
  }
  stage('Unit Test') {
    steps {
      sh 'npm run test'
    }
  }
  // .... build, delpoy
}
复制代码

pipeline 中操做镜像

须要安装 Jenkins docker workflow 插件, 下面的例子展现了:python

  • 链接远程Docker主机
  • 登陆私有Docker 仓库(阿里云镜像服务)
  • 根据代码中的 Dockerfile 构建镜像并push
  • 删除Docker远程主机中构建好的镜像,不占用空间
  • 不包含目标主机中部署镜像 其实就说上篇文章中的pipeline版本
#!groovy

pipeline {
    agent any
    
    environment {
        // PATH="/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin"
        _docker_remote_server='tcp://192.100.155.155:2375'
        _aliyun_registry='https://registry.cn-zhangjiakou.aliyuncs.com'
    }

    stages {
        stage('debug')  {
            steps {
                script {
                    sh "printenv"
                }
            }
        }

        stage('connect remote docker') {
            steps {
                // 注意 代码是先拉到了Jenkins主机上,可是构建镜像在Docker远程
                git 'https://github.com/mafeifan/docker-express-demo.git'

                script {
                    docker.withServer("${env._docker_remote_server}") {
                         // 第一个参数是私有仓库地址,注意要带http(s),第二个参数是帐号密码登陆凭证,须要提早建立
                        docker.withRegistry("${env._aliyun_registry}", 'aliyun-docker-registry') {
                            // 使用 ${GIT_PREVIOUS_COMMIT} 取不到 commint_id
                            // https://stackoverflow.com/questions/35554983/git-variables-in-jenkins-workflow-plugin
                            git_commit = sh(returnStdout: true, script: "git rev-parse HEAD").trim()
                            echo git_commit
                            def customImage = docker.build("fineyma/node-demo:${env.BUILD_NUMBER}-${git_commit}")
                            /* Push the container to the custom Registry */
                            customImage.push()
                            // 能够优化,用匹配搜索并删除
                            sh "docker rmi fineyma/node-demo:${env.BUILD_NUMBER}-${git_commit}"
                        }
                    }
                }

                // clean workspace
                cleanWs()
            }
        }
    }
}
复制代码

这里 customImage.push() 貌似有个bug,构建以后的镜像有两个同样的,一个带registry name一个不带git

关于 docker.build, docker.withRegistry 等是Jenkins docker workflow 插件提供的, 能够看源码,实际上是封装了docker build, docker login,你彻底能够写原生的 docker 命令github

关于远程容器部署

既然镜像已经成功上传到阿里云的镜像服务,理论上任何装有Docker的主机只要docker run就能够完成部署了(须要网络通)。 实现方法我想到有几种:web

  1. 阿里云的镜像服务提供触发器,即每当push新的镜像上去,能够发送一个post请求到配置的地址,这样能够完成容器部署操做。Jenkins能够添加一个job,暴露一个触发地址给阿里云镜像服务的触发器。
  2. 在pipeline中添加ssh登陆目标主机,而后添加 docker run --rm fineyma/node-demo:${env.BUILD_NUMBER}-${git_commit} step 步骤
  3. 目标主机也开放dockerd,这样连登陆都不须要了,直接docker client 操做远程Docker完成部署。

参考

jenkins.io/doc/pipelin… jenkins.io/doc/book/pi…chrome

相关文章
相关标签/搜索