简体   繁体   中英

Parse the Jenkins Pipeline in to JSON using Java (get all strings/text between two curly braces)

I have a requirement to parse the Jenkins pipeline script and convert into JSON using JAVA

Input:

pipeline{
    agent any
    stages{
       stage('stage-1'){
           steps{
                script {
                  echo 'hello stage1 world'
                }
            }
        }
        stage('stage-2'){
             steps{
                script {
                  echo 'hello stage2 world'
                }
            }
         }
    }
}```

**Output required**
 "pipeline": {
      "stages": [
        {
          "id": "stage1,
          "steps": [
            {
            "type": "script",
              "value" :"hello stage1 world"
            }
          ]
        },
         {
          "id": "stage2,
          "steps": [
            {
            "type": "script",
            "value" :"hello stage2 world"
            }
          ]
        }
    ]
}

Methods/Approach which i am using: I thought will use regex function to get all content between two curly braces & parsing the same. however i am new to regex, could you please guide me right approach

The JSON objects will have a "{" opening tag and a closing "}" tag. To get this requirement it would be better to devise a custom parser as per your JSON structure.

However, if you are in need of creating a JSON format jenkins job file then you can refer the python based module "jenkins job builder"

https://docs.openstack.org/infra/jenkins-job-builder/

This module helps you define the jenkins jobs in JSON format.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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