开发者社区> 问答> 正文

Jenkins Kubernetes插件:如何从Dockerfile构建图像并在图像中运行步骤

k8s小能手 2019-02-22 16:05:51 646

我正在使用Jenkins kubernetes-plugin。是否可以从Dockerfile构建docker镜像,然后在创建的图像中运行步骤?该插件需要在pod模板中指定一个图像,所以我的第一次尝试是使用docker-in-docker,但步骤docker.image('jenkins/jnlp-slave').inside() {..}失败:

pipeline {
agent {

kubernetes {
  //cloud 'kubernetes'
  label 'mypod'
  yaml """

apiVersion: v1
kind: Pod
spec:
containers:

  • name: docker
    image: docker:1.11
    command: ['cat']
    tty: true
    volumeMounts:

    • name: dockersock
      mountPath: /var/run/docker.sock

    volumes:

  • name: dockersock
    hostPath:
    path: /var/run/docker.sock
    """
    }
    }

stages {

stage('Build Docker image') {
  steps {
    git 'https://github.com/jenkinsci/docker-jnlp-slave.git'
    container('docker') {
      sh "docker build -t jenkins/jnlp-slave ."
      docker.image('jenkins/jnlp-slave').inside() {
        sh "whoami"
      }
    }
  }
}

}
}
失败:

WorkflowScript: 31: Expected a symbol @ line 31, column 11.

         docker.image('jenkins/jnlp-slave').inside() {
Kubernetes jenkins Shell 持续交付 开发工具 git Docker 容器 Perl
分享到
取消 提交回答
全部回答(1)
  • k8s小能手
    2019-07-17 23:29:12

    pipeline {
    agent {

    kubernetes {
      //cloud 'kubernetes'
      label 'mypod'
      yaml """

    apiVersion: v1
    kind: Pod
    spec:
    containers:

    • name: docker
      image: docker:1.11
      command: ['cat']
      tty: true
      volumeMounts:

      • name: dockersock
        mountPath: /var/run/docker.sock

      volumes:

    • name: dockersock
      hostPath:
      path: /var/run/docker.sock
      """
      }
      }

    stages {

    stage('Build Docker image') {
      steps {
        git 'https://github.com/jenkinsci/docker-jnlp-slave.git'
        container('docker') {
          script {
            def image = docker.build('jenkins/jnlp-slave')
            image.inside() {
              sh "whoami"
            }
          }
        }
      }
    }

    }
    }

    1 0

集结各类场景实战经验,助你开发运维畅行无忧

推荐文章
相似问题