如何在 Jenkinsfile 管道中使用 docker 代理?

时间:2021-06-26 17:46:56

标签: docker jenkins jenkins-pipeline jenkins-plugins

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

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

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

这是我的 Jenkinsfile:

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 "

         }
       }

     }

}}

我错过了什么?

谢谢!

2 个答案:

答案 0 :(得分:0)

该消息意味着您唯一可用的节点(恰好是您的 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

答案 1 :(得分:-1)

您的管道将如下所示:

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
  • 将文件ExecStart=/usr/bin/dockerd中的/lib/systemd/system/docker.service行修改为以下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。