詹金斯 - >循环槽Docker-Agent

发布于 2025-02-06 20:14:01 字数 1851 浏览 3 评论 0原文

我想在 jenkins-pipeline中使用几个 docker-images 构建一些软件包 。下面的管道有效..但是我想实现阶段是一个更“循环”的方式。...

pipeline {
  
  agent { label 'docker' }

  stages {

    stage ('Running on ubuntu:20.04') {
      agent {
        docker {
          reuseNode true
          label 'docker'
          image 'ubuntu:20.04'
        }
      }

      steps {
        sh 'uname -a && bash --version'
      }
    }

    stage ('Running on ubuntu:22.04') {
      agent {
        docker {
          reuseNode true
          label 'docker'
          image 'ubuntu:22.04'
        }
      }
      steps {
        sh 'uname -a && bash --version'
      }
    }
  }
}

一个想法如何实施? ..

最初,我尝试使用这样的东西


pipeline {
  agent { label 'docker' }

  stages {
    stage ('Loop-Stage') {
      steps {
        script {
     
          // Loop trough List (*works*)
          for (item in ['ubuntu:20.04', 'ubuntu:22.04']) {
            
            // Create new stage (*works*)
            stage ('Running on ${item}') {
              
              // Set agent (*fails*)
              agent {
                docker {
                  reuseNode true
                  label 'docker'
                  image "${item}"
                }
              }

              steps {
                sh 'uname -a && bash --version'
              }
            }
          }
        }
      }
    }
  }
}

,但现在确实有效。该错误似乎表明脚本化和声明语法之间的混合物。您会收到以下错误:

java.lang.NoSuchMethodError: No such DSL method 'agent' found among steps [ArtifactoryGradleBuild, MavenDescriptorStep, addInteractivePromotion, ansiColor, archive, artifactoryBuildTrigger, artifactoryDistributeBuild, artifactoryDownload, artifactoryEditProps, artifactoryGoPublish, ar

如何将相应的“代理”应用于动态生成的阶段?

I want to build some Packages using several Docker-Images in a Jenkins-Pipeline
. The Pipeline below works .. but I want to implement the Stage(s) is a more "Loop"-ish way ....

pipeline {
  
  agent { label 'docker' }

  stages {

    stage ('Running on ubuntu:20.04') {
      agent {
        docker {
          reuseNode true
          label 'docker'
          image 'ubuntu:20.04'
        }
      }

      steps {
        sh 'uname -a && bash --version'
      }
    }

    stage ('Running on ubuntu:22.04') {
      agent {
        docker {
          reuseNode true
          label 'docker'
          image 'ubuntu:22.04'
        }
      }
      steps {
        sh 'uname -a && bash --version'
      }
    }
  }
}

An Ideas how to implement that ? ..

Initially I tried using something like this


pipeline {
  agent { label 'docker' }

  stages {
    stage ('Loop-Stage') {
      steps {
        script {
     
          // Loop trough List (*works*)
          for (item in ['ubuntu:20.04', 'ubuntu:22.04']) {
            
            // Create new stage (*works*)
            stage ('Running on ${item}') {
              
              // Set agent (*fails*)
              agent {
                docker {
                  reuseNode true
                  label 'docker'
                  image "${item}"
                }
              }

              steps {
                sh 'uname -a && bash --version'
              }
            }
          }
        }
      }
    }
  }
}

But this does now work. The Error seems to indicate a mixture between scripted&declarative Syntax .. you get the following error:

java.lang.NoSuchMethodError: No such DSL method 'agent' found among steps [ArtifactoryGradleBuild, MavenDescriptorStep, addInteractivePromotion, ansiColor, archive, artifactoryBuildTrigger, artifactoryDistributeBuild, artifactoryDownload, artifactoryEditProps, artifactoryGoPublish, ar

How can I apply a the corresponding "agent" to the dynamically generated Stage ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

虚拟世界 2025-02-13 20:14:01

当您进入脚本块时,您无法再使用一些声明语法,例如agent。这就是为什么您会收到错误java.lang.lang.nosuchmethoderror:没有这样的DSL方法'agent'。詹金斯(Jenkins)对您是否正在尝试使用声明的管道语法或仅使用Groovy的脚本语法感到困惑。

您可以做这样的事情。这将创建一个阶段列表,然后并行执行。

def stageList

def getStages(image) {
    return {
        stage("Running on Ubunt:${image}") {
          docker.image("ubuntu:${image}").inside {
                sh 'uname -a'
            }
        }
    }
}

pipeline {
agent any
  stages {
      
    stage ("Generate Stages") {
        steps {
            script {
                def imageList = ["20.04", "22.04"]
                stageList = imageList.collectEntries {
                        ["${it}" : getStages(it)]}
                    }
                
            }
        }

        stage("Run Stages") {
            steps {
                    script {
                        parallel stageList
                    }
                }
        }
        
    }
}

如果需要顺序执行,则可以修改管道,如下所示。

pipeline {
  agent { label 'docker' }

  stages {
    stage ('Loop-Stage') {
      steps {
        script {
     
          // Loop trough List (*works*)
          for (item in ['ubuntu:20.04', 'ubuntu:22.04']) {
            
            // Create new stage (*works*)
            stage ('Running on ${item}') {
              
            docker.image("${item}").inside {
                sh 'uname -a'
                sh 'uname -a && bash --version'
            }
            }
          }
        }
      }
    }
  }
}

When you go into a script block you can no longer use some of the declarative syntaxes like agent. That's why you are getting the error java.lang.NoSuchMethodError: No such DSL method 'agent'. Jenkins is confused about whether you are trying to use Declarative Pipeline syntax or just scripted syntax with groovy.

You can do something like this. This will create a list of stages and then execute them in parallel.

def stageList

def getStages(image) {
    return {
        stage("Running on Ubunt:${image}") {
          docker.image("ubuntu:${image}").inside {
                sh 'uname -a'
            }
        }
    }
}

pipeline {
agent any
  stages {
      
    stage ("Generate Stages") {
        steps {
            script {
                def imageList = ["20.04", "22.04"]
                stageList = imageList.collectEntries {
                        ["${it}" : getStages(it)]}
                    }
                
            }
        }

        stage("Run Stages") {
            steps {
                    script {
                        parallel stageList
                    }
                }
        }
        
    }
}

If you want sequential execution, you can modify your pipeline as shown below.

pipeline {
  agent { label 'docker' }

  stages {
    stage ('Loop-Stage') {
      steps {
        script {
     
          // Loop trough List (*works*)
          for (item in ['ubuntu:20.04', 'ubuntu:22.04']) {
            
            // Create new stage (*works*)
            stage ('Running on ${item}') {
              
            docker.image("${item}").inside {
                sh 'uname -a'
                sh 'uname -a && bash --version'
            }
            }
          }
        }
      }
    }
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文