Jenkins2 學習系列27 -- pipeline 中 Docker 操作

TODO 待補充流程圖

構(gòu)建任務在指定Docker鏡像中進行

如下面例子,首先pull一個我打包好的基于ubuntu的node鏡像,這個鏡像里面已經(jīng)包含了nodejs10, wget, zip, curl, python,chrome,firefox, aws-cli 等常用工具,可以方便的在里面執(zhí)行npm install,npm run test 啟動瀏覽器跑測試等。

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 插件,
下面的例子展示了:

  • 連接遠程Docker主機
  • 登錄私有Docker 倉庫(阿里云鏡像服務)
  • 根據(jù)代碼中的 Dockerfile 構(gòu)建鏡像并push
  • 刪除Docker遠程主機中構(gòu)建好的鏡像,不占用空間
  • 不包含目標主機中部署鏡像
    其實就說上篇文章中的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主機上,但是構(gòu)建鏡像在Docker遠程
                git 'https://github.com/mafeifan/docker-express-demo.git'

                script {
                    docker.withServer("${env._docker_remote_server}") {
                         // 第一個參數(shù)是私有倉庫地址,注意要帶http(s),第二個參數(shù)是賬號密碼登錄憑證,需要提前創(chuàng)建
                        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()
                            // 可以優(yōu)化,用匹配搜索并刪除
                            sh "docker rmi fineyma/node-demo:${env.BUILD_NUMBER}-${git_commit}"
                        }
                    }
                }

                // clean workspace
                cleanWs()
            }
        }
    }
}

這里 customImage.push() 貌似有個bug,構(gòu)建之后的鏡像有兩個一樣的,一個帶registry name一個不帶

關于 docker.build, docker.withRegistry 等是Jenkins docker workflow 插件提供的, 可以看源碼,其實是封裝了docker build, docker login,你完全可以寫原生的docker 命令

關于遠程容器部署

既然鏡像已經(jīng)成功上傳到阿里云的鏡像服務,理論上任何裝有Docker的主機只要docker run就可以完成部署了(需要網(wǎng)絡通)。
實現(xiàn)方法我想到有幾種:

  1. 阿里云的鏡像服務提供觸發(fā)器,即每當push新的鏡像上去,可以發(fā)送一個post請求到配置的地址,這樣可以完成容器部署操作。Jenkins可以添加一個job,暴露一個觸發(fā)地址給阿里云鏡像服務的觸發(fā)器。
  2. 在pipeline中添加ssh登錄目標主機,然后添加 docker run --rm fineyma/node-demo:${env.BUILD_NUMBER}-${git_commit} step 步驟
  3. 目標主機也開放dockerd,這樣連登錄都不需要了,直接docker client 操作遠程Docker完成部署。

參考

https://jenkins.io/doc/pipeline/steps/docker-workflow
https://jenkins.io/doc/book/pipeline/docker

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內(nèi)容

  • Docker 學習目標: 掌握Docker基礎知識,能夠理解Docker鏡像與容器的概念 完成Docker安裝與啟...
    執(zhí)筆夢一場閱讀 3,297評論 2 10
  • docker基本概念 1. Image Definition 鏡像 Image 就是一堆只讀層 read-only...
    慢清塵閱讀 8,812評論 1 21
  • 1、傳統(tǒng)我們的項目開發(fā)模式是產(chǎn)品調(diào)研提出需求,開發(fā)團隊研究決定開發(fā)方案選型。然后開始一個周期的開發(fā),模塊開發(fā)完成之...
    張熙閱讀 12,419評論 2 42
  • 《Docker從入門到實踐》閱讀筆記 原書地址: https://yeasy.gitbooks.io/docker...
    GuoYuebo閱讀 11,421評論 1 39
  • 五、Docker 端口映射 無論如何,這些 ip 是基于本地系統(tǒng)的并且容器的端口非本地主機是訪問不到的。此外,除了...
    R_X閱讀 1,792評論 0 7