如何通过dsl脚本读取yaml文件-jenkins

问题描述 投票:0回答:1

我在 Jenkins 上开发了一个种子作业(这是一个自由式项目)。该作业采用特定参数,例如“name”和“gitrepourl”。现在,我的目标是通过使用包含这些参数的 YAML 文件来自动化此过程。我希望我的 DSL 脚本从 YAML 文件中读取这些参数。您能否指导我在哪里创建此文件以及如何将其与 Jenkins 集成?

供您参考,我目前正在使用“Process DSL Script”构建步骤对此进行测试。不过,我有兴趣从 GitHub 存储库访问我的脚本。不幸的是,我也不知道如何实现这一目标

我尝试通过包含一个文件作为 YAML 文件来利用“参数化”选项。然而,我仍然对这种方法感到困惑

github jenkins groovy yaml dsl
1个回答
0
投票

为什么不切换到

Pipeline
项目呢?在 git 存储库的根目录中创建一个
Jenkinsfile
。然后您可以在该脚本中编写 Jenkins 管道,如下所示:

pipeline {
   agent any

   stages {
       stage("checkout") {
          steps {
             checkout scmGit( branches: [[name:'main']], 
                    userRemoteConfigs: [
                       [ 
                          credentialsId: 'mylogin', 
                          url: "https://github.com/github/git.git"
                       ]
                    ])
          }          
       }
       step("Process YAML") {
          script {
             def data = readYaml "some_config.yml"
             println( data.someField )
          }
       }
   }
}

然后,您可以在

some_config.yml
旁边签入要读取的 YAML 文件(即
Jenkinsfile
),并使用
readYaml
将存储库中的配置文件读取到管道脚本中。

© www.soinside.com 2019 - 2024. All rights reserved.