简体   繁体   English

如何从 jenkins 管道 groovy 脚本中的 curl 获取 json 列表

[英]How to get a json list from the curl in jenkins pipeline groovy script

Using curl in jenkins pipeline script getting below json file, after that How to get the list values of dependencies list and trigger those jobs?在 jenkins 管道脚本中使用 curl 获取低于 json 文件,之后如何获取依赖项列表的列表值并触发这些作业?

{
  "Dependencies": [
    "Dep1",
    "Dep2",
    "Dep3"
  ]
 
}

My current program is looking like this which is not working.我当前的程序看起来像这样,但无法正常工作。 And after getting values I need to form the Jenkins jobs and trigger from pipeline在获得价值后,我需要形成 Jenkins 工作并从管道触发

pipeline {
agent {
        label 'Dep-Demo'
    }
stages{
    stage('Getting Dependecies jason-object'){
            steps {
                script {
                    final String url = "https://***************/repository/files/sample.jason/raw?ref=main"
                    withCredentials([usernamePassword(credentialsId: 'api', passwordVariable: 'PASSWORD', usernameVariable: 'USELESS')]) {
                      sh ''' echo $PASSWORD ''' 

                      def response =  sh(script: "curl -s --header \"PRIVATE-TOKEN: $PASSWORD\" $url ", returnStdout:true)
                      echo "***** $response" 
                      def jsonObj = readJSON text: response.join(" ")
                      echo jsonObj 

Check the following example.检查以下示例。

script {
    def jString = '''
            {
              "Dependencies": [
                "Dep1",
                "Dep2",
                "Dep3"
              ]
             
            }
        '''
        def jsonObj = readJSON text: jString
        jsonObj.Dependencies.each { dep -> 
            echo "Building ${dep}"
            //Building the Job
            build job: dep    
        }      
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM