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

vojdkbi0  于 2023-03-01  发布在  Jenkins
关注(0)|答案(2)|浏览(159)

我在GitHub上传了一个Django项目,我需要把它和Jenkins链接起来。我在Ubuntu 20.04机器上安装了Jenkins和Docker服务。
我用我的repo配置了Jenkins服务器,并且安装了所有的sugestd plggins + docker pipeline插件。
之后,我创建了一个Jenkinsfile,它使用Docker代理在python Docker容器中运行stages,但我在控制台输出中得到“'Jenkins' doesn 't have label 'docker'”。我尝试在项目设置中添加label 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 "

         }
       }

     }

}}

我错过了什么?
谢谢大家!

9rbhqvlz

9rbhqvlz1#

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

k4ymrczo2#

您的管道将如下所示:

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;
  • 添加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。

相关问题