【问题标题】:How to use docker agent in Jenkinsfile pipeline?如何在 Jenkinsfile 管道中使用 docker 代理?
【发布时间】:2021-09-09 16:06:06
【问题描述】:

我在 GitHub 上上传了一个 Django 项目,我需要将它与 jenkins 链接。 我在 Ubuntu 20.04 机器上安装了 Jenkins 和 Docker 服务。

我使用我的 repo 配置了 Jenkins 服务器,并安装了所有建议的 plggins + docker 管道插件。

之后,我创建了一个 Jenkinsfile,它使用 docker 代理在 python docker 容器中运行阶段,但我在控制台输出中得到“‘Jenkins’没有标签‘docker’”。我尝试在项目设置中添加标签docker但还是出现同样的错误!

这是我的 Jenkins 文件:

pipeline {
   agent any
       stages {


       stage("install pip dependencies") {
      agent { 
        docker {
           label "docker" 
            image "python:3.7"
           }
           }
       steps {
          withEnv(["HOME=${env.WORKSPACE}"]) {
              sh "pip install virtualenv"
              sh "virtualenv venv"
              sh "pip install -r requirements.txt "

         }
       }

     }

}}

我错过了什么?

谢谢!

【问题讨论】:

    标签: docker jenkins jenkins-pipeline jenkins-plugins


    【解决方案1】:

    该消息意味着您唯一可用的节点(恰好是您的 Jenkins 控制器)没有您在此块中的代理上需要的标签 docker

    agent {
        docker {
            label 'docker'
            image 'python:3.7'
        }
    }
    

    将标签 docker 添加到控制器,然后重新启动 Jenkins(识别标签更改所必需的,尽管这让我感到惊讶。这可能是控制器本身的标签,因为您应该避免安排作业运行如果可能的话)解决问题。

    预标签:

    Started by user admin
    Running in Durability level: MAX_SURVIVABILITY
    [Pipeline] Start of Pipeline
    [Pipeline] node
    Running on Jenkins in /var/jenkins_home/workspace/test
    [Pipeline] {
    [Pipeline] stage
    [Pipeline] { (install pip dependencies)
    [Pipeline] node
    Still waiting to schedule task
    ‘Jenkins’ doesn’t have label ‘docker’
    Aborted by admin
    [Pipeline] // node
    [Pipeline] }
    [Pipeline] // stage
    [Pipeline] }
    [Pipeline] // node
    [Pipeline] End of Pipeline
    Finished: ABORTED
    

    标签后,重启前:

    Started by user admin
    Running in Durability level: MAX_SURVIVABILITY
    [Pipeline] Start of Pipeline
    [Pipeline] node
    Running on Jenkins in /var/jenkins_home/workspace/test
    [Pipeline] {
    [Pipeline] stage
    [Pipeline] { (install pip dependencies)
    [Pipeline] node
    Still waiting to schedule task
    ‘Jenkins’ doesn’t have label ‘docker’
    Aborted by admin
    [Pipeline] // node
    [Pipeline] }
    [Pipeline] // stage
    [Pipeline] }
    [Pipeline] // node
    [Pipeline] End of Pipeline
    Finished: ABORTED
    

    重启后,突出显示我的控制器没有安装docker

    Started by user admin
    Running in Durability level: MAX_SURVIVABILITY
    [Pipeline] Start of Pipeline
    [Pipeline] node
    Running on Jenkins in /var/jenkins_home/workspace/test
    [Pipeline] {
    [Pipeline] stage
    [Pipeline] { (install pip dependencies)
    [Pipeline] node
    Running on Jenkins in /var/jenkins_home/workspace/test@2
    [Pipeline] {
    [Pipeline] isUnix
    [Pipeline] sh
    + docker inspect -f . python:3.7
    /var/jenkins_home/workspace/test@2@tmp/durable-4a9f38a7/script.sh: 1: /var/jenkins_home/workspace/test@2@tmp/durable-4a9f38a7/script.sh: docker: not found
    [Pipeline] isUnix
    [Pipeline] sh
    + docker pull python:3.7
    /var/jenkins_home/workspace/test@2@tmp/durable-58d19d02/script.sh: 1: /var/jenkins_home/workspace/test@2@tmp/durable-58d19d02/script.sh: docker: not found
    [Pipeline] }
    [Pipeline] // node
    [Pipeline] }
    [Pipeline] // stage
    [Pipeline] }
    [Pipeline] // node
    [Pipeline] End of Pipeline
    ERROR: script returned exit code 127
    Finished: FAILURE
    

    【讨论】:

    • 值得指出的是,除非有一些节点你不想想要接你的 docker 工作,你可以省略 @987654328 中的 label 标签Jenkinsfile 中的 @ 字段。
    【解决方案2】:

    您的管道将如下所示:

    pipeline {
      agent {
        label 'docker'
      }
      stages {
        stage('install pip dependencies') {
          steps {
            withEnv(["HOME=${env.WORKSPACE}"]) {
              sh'''
                pip install virtualenv
                virtualenv venv
                pip install -r requirements.txt
              '''
            }
          }
        }
      }
    }
    

    但在您需要按照以下步骤让 jenkins 将 docker 容器作为从站运行之前:

    • 在您的主机上安装 docker;
    • 将 jenkins 添加到 docker 组:sudo usermod -aG docker jenkins
    • 将文件/lib/systemd/system/docker.service中的ExecStart=/usr/bin/dockerd行修改为以下ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock -H fd:// -s overlay2 --containerd=/run/containerd/containerd.sock
    • 运行sudo systemctl daemon-reloadsudo systemctl restart docker
    • 在 Jenkins 中配置 docker 部分:manage Jenkins -> manage nodes and clouds -> configure clouds -> add a new cloud -> docker 在 Docker URL 字段中输入 tcp://127.0.0.1:2375 (or 4243)unix:///var/run/docker.sock。配置代理,设置任何标签并在管道中使用。

    您可能需要关闭 selinux。

    【讨论】:

    • 我按照这些步骤操作,但在添加 Docker URL 后,我单击了测试连接按钮并收到此错误:java.io.IOException: Connection reset by peer java.io.IOException: Connection reset by peer at java.base/sun.nio.ch.SocketDispatcher.read(SocketDispatcher.java:39) 在 java.base/sun.nio.ch.IOUtil 的 java.base/sun.nio.ch.FileDispatcherImpl.read0(Native Method) .readIntoNativeBuffer(IOUtil.java:276) 在 java.base/sun.nio.ch.IOUtil.read(IOUtil.java:233) 在 java.base/sun.nio.ch.IOUtil.read(IOUtil.java:223) ) 在
    • 而且我没有安装 SELinux 来禁用它可以吗?
    • 您使用的是哪个操作系统?
    • 我关注了这个博客karthi-net.medium.com/… 并且错误现在更改为“docker-00021ocb2higq on docker' is offline”你知道这是为什么吗?我正在使用 Ubuntu 20.04
    • 您的agent 声明缺少docker 节,这破坏了关于此的一切......
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-07-03
    • 1970-01-01
    • 2019-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多