如何在jenkins pipeline groovy脚本中从curl获取JSON列表

a0zr77ik  于 2022-12-17  发布在  Jenkins
关注(0)|答案(1)|浏览(226)

在jenkins管道脚本中使用curl获取json文件,之后如何获取依赖列表的列表值并触发这些作业?

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

我目前的程序看起来是这样的,这是不工作。并获得值后,我需要形成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
0lvr5msh

0lvr5msh1#

检查以下示例。

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    
        }      
}

相关问题