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

相關文章
相關標籤/搜索