Why embed YAML into the Jenkinsfile when you can read it as a file? What
Ivan is describing is the correct answer. Prevent this type of mistake from
ever entering the codebase at the time of commit. If you absolutely have to
validate inline YAML it might look something like this (which purposely
fails due to a missing colon for size key):
pipeline {
agent any
stages {
stage("read yaml") {
steps {
script {
try {
readYaml text: """
something: 'my datas'
size 3
isEmpty: false
"""
} catch (Exception err) {
currentBuild.result = 'ABORTED'
error("Malformed YAML detected:\n${err}")
}
}
echo "YAML verified"
}
}
}
}
Errors look like this in the console output, this happens before anything
is built (next echo step) based on the example above:
ERROR: Malformed YAML detected:
while scanning a simple key
in 'reader', line 4, column 1:
size 3
^
could not find expected ':'
in 'reader', line 5, column 1:
isEmpty: false
^
Finished: ABORTED
Again, I highly recommend looking into Ivan's suggestion and decoupling
your YAML from your Jenkinsfile so you're not adding complexity to your
pipeline.
On Saturday, October 12, 2019 at 1:08:37 PM UTC-7, Tony Cappellini wrote:
>
> Thanks- this might work for yank files but
> I can’t see how it would work for pipeline jobs that have embedded yaml.
> This yaml gets edited just before the use clicks the BUILD button.
> Essentially, I need to call the yaml sanitizer just after BUILD is clicked,
> but before Jenkins parses it.
>
> On Sat, Oct 12, 2019 at 11:29 AM Ivan Fernandez Calvo <[email protected]
> <javascript:>> wrote:
>
>> We use pre-commit on a stage, pre-commit has a bunch of linters, syntax
>> check, YAML linter, JSON linter, best practices check on code and scripts,
>> .... https://pre-commit.com/ also you can configure it to run locally as
>> a git hook to check your project before commit something
>>
>> --
>> You received this message because you are subscribed to a topic in the
>> Google Groups "Jenkins Users" group.
>> To unsubscribe from this topic, visit
>> https://groups.google.com/d/topic/jenkinsci-users/uFsC3geutC0/unsubscribe
>> .
>> To unsubscribe from this group and all its topics, send an email to
>> [email protected] <javascript:>.
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/jenkinsci-users/2a832ccd-303c-448b-b83a-cc15a7c26603%40googlegroups.com
>> .
>>
>
--
You received this message because you are subscribed to the Google Groups
"Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/jenkinsci-users/00356736-acae-4314-99dc-207d919d4fed%40googlegroups.com.