Jenkins声明:具有多个代理的Kubernetes插件

vulvrdjw  于 2023-01-25  发布在  Jenkins
关注(0)|答案(2)|浏览(138)

我试图设置一个Jenkins声明性管道,使其在执行过程中使用两个不同的代理。代理由Kubernetes插件动态生成。为了论证和简单起见,让我们假设我想这样做:
在代理1(云名称:"ubuntu"):

  • 运行apt-get和一些安装程序
  • 运行shell脚本
  • 其他步骤

在代理2(云名称:"软呢帽"):

  • 运行dnf和一些安装
  • 运行shell脚本
  • 其他步骤

我遇到的问题是,如果我使用全局代理声明:

pipeline {
  agent {
    kubernetes {
      cloud 'ubuntu'
      label "ubuntu-agent"
      containerTemplate {
        name 'support'
        image 'blenderfox/support'
        ttyEnabled true
        command 'cat'
      }
    }
  }
...
}

那么它会在所有阶段使用如果我不在每个阶段声明一个代理的话。
如果使用agent none

pipeline {
  agent none
  ...
}

然后,我必须为每个阶段声明一个代理规范,例如:

stage ("apt update") {
      agent {
        kubernetes {
          cloud 'ubuntu'
          label "ubuntu-agent"
          containerTemplate {
            name 'support'
            image 'blenderfox/support'
            ttyEnabled true
            command 'cat'
          }
        }
      }
      steps {
        sh """
        apt update
        """
      }
    }

虽然这对我来说很有效,因为我可以在每个阶段声明我想要哪个代理,但这个方法引起的问题是,它为 * 每个 * 阶段生成一个新代理,这意味着状态不在这两个阶段之间传递:

stage ("apt-update") {
      agent {
        ....
      }
      steps {
        sh """
        apt update
        """
      }
    }
    stage ("apt-install") {
      agent {
        ....
      }
      steps {
        sh """
        apt install -y ....
        """
      }
    }

我可以跨阶段重用同一个代理吗?例如,如下所示:

stage ("provision agent") {
  agent {
    ...
    label "ubuntu-agent"
    ...
  }
  steps {
    sh """
    echo "Provisioning agent"
    """
  }
}
stage ("apt-update") {
  agent {
    label "ubuntu-agent" //reuse agent from previous stage
  }
  steps {
    sh """
    apt update
    """
  }
}
stage ("apt-install") {
  agent {
    label "ubuntu-agent" //reuse agent from previous stage
  }
  steps {
    sh """
    apt install -y ....
    """
  }
}
vecaoik1

vecaoik11#

找到了一个解决办法。很老套但很管用:

pipeline {

  agent none

  stages {
    stage ("Provision dev agent") {
      agent {
        kubernetes {
          cloud 'dev-cloud'
          label "dev-agent-${env.BUILD_NUMBER}"
          slaveConnectTimeout 300
          idleMinutes 5
          yamlFile "jenkins-dev-agent.yaml"
        }
      }
      steps {
        sh """
        ## Do any agent init steps here
        """
      }

    }
    stage ("Do something on dev agent") {
      agent {
        kubernetes {
          label "dev-agent-${env.BUILD_NUMBER}"
        }
      }
      steps {
        sh """
        ## Do something here
        """
      }
    }

    stage ("Provision production agent") {
      agent {
        kubernetes {
          cloud 'prod-cloud'
          label "prod-agent-${env.BUILD_NUMBER}"
          slaveConnectTimeout 300
          idleMinutes 5
          yamlFile "jenkins-prod-agent.yaml"
        }
      }
      steps {
        sh """
        ## Do any agent init steps here
        """
      }
    }
    stage ("Do something on prod agent") {
      agent {
        kubernetes {
          label "prod-agent-${env.BUILD_NUMBER}"
        }
      }
      steps {
        sh """
        ## Do something here
        """
      }
    }
  }
}

代理程序的yamls各不相同,但您可以执行以下操作:

spec:
   containers:
   - name: docker
     image: docker:18.06.1
     command: ["tail", "-f", "/dev/null"]
     imagePullPolicy: Always
     volumeMounts:
       - name: docker
         mountPath: /var/run/docker.sock
   volumes:
   - hostPath:
      path: "/var/run/docker.sock"
     name: "docker"

然后像这样使用代理:

stage ("docker build") {
  agent {
    kubernetes {
      label "dev-agent-${env.BUILD_NUMBER}"
    }
  }
  steps {
    container('docker') {
      sh """
        ## docker build....
      """
    }
  }
}
iszxjhcz

iszxjhcz2#

使用sequential stages可以解决这个问题-您可以使用代理定义一个stage,然后可以在其中嵌套其他stage

pipeline {

  agent none

  stages {
    stage ("Provision dev agent") {
      agent {
        kubernetes {
          cloud 'dev-cloud'
          slaveConnectTimeout 300
          yamlFile "jenkins-dev-agent.yaml"
        }
      }

      stages {
        stage ("Do something on dev agent") {
          steps {
            sh """
            ## Do something here
            """
          }
        }

        stage ("Do something else on dev agent") {
          steps {
            sh """
            ## Do something here
            """
          }
        }
      }
    }

    stage ("Provision prod agent") {
      agent {
        kubernetes {
          cloud 'prod-cloud'
          slaveConnectTimeout 300
          yamlFile "jenkins-prod-agent.yaml"
        }
      }

      stages {
        stage ("Do something on prod agent") {
          steps {
            sh """
            ## Do something here
            """
          }
        }

        stage ("Do something else on prod agent") {
          steps {
            sh """
            ## Do something here
            """
          }
        }
      }
    }
  }
}

相关问题