Last active
May 10, 2023 15:16
-
-
Save steven-terrana/01e131eb2cda05bf02e37e1f24b94834 to your computer and use it in GitHub Desktop.
Convert YAML to ModelASTPipelineDef
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.jenkinsci.plugins.pipeline.modeldefinition.parser.Converter | |
import org.jenkinsci.plugins.pipeline.modeldefinition.parser.JSONParser | |
import net.sf.json.JSONObject | |
import org.yaml.snakeyaml.Yaml | |
import com.github.fge.jsonschema.tree.JsonTree | |
import org.jenkinsci.plugins.pipeline.modeldefinition.ast.ModelASTPipelineDef | |
String yamlString = """ | |
pipeline: | |
stages: | |
- name: foo | |
stages: | |
- name: bar | |
branches: | |
- name: default | |
steps: | |
- name: echo | |
arguments: | |
- key: message | |
value: | |
isLiteral: false | |
value: '"In stage \${STAGE_NAME} in group foo"' | |
- name: baz | |
branches: | |
- name: default | |
steps: | |
- name: echo | |
arguments: | |
- key: message | |
value: | |
isLiteral: false | |
value: '"In stage \${STAGE_NAME} in group foo"' | |
agent: | |
type: none | |
""" | |
Yaml yaml = new Yaml() | |
Map<String,Object> parsed = (Map<String, Object>) yaml.load(yamlString) | |
JSONObject jsonObject = new JSONObject(parsed); | |
JsonTree tree = Converter.jsonTreeFromJSONObject(jsonObject) | |
JSONParser parser = new JSONParser(tree) | |
ModelASTPipelineDef pipeline = parser.parse() | |
println pipeline |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment