Validate a Jenkinsfile

问题

总是在运行jenkins pipeline时,才发现语法问题。能不能在运行前发现问题呢

方法

方法之一如下,更多方法可参考链接

新建一个文件,JenkinsfileA,添加以下正确的内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
pipeline {
agent any
environment {
MY_ENV = 'my-test'
}

stages {
stage("Open the door") {
steps {
echo 'Open the door ..'
bat label: '', script: 'chdir'
bat label: '', script: 'set'
powershell label: '', script: "echo ${MY_ENV}"
powershell label: '', script: "echo \\name\\folder"
powershell label: '', script: "echo //123//565"
}
}

在文件所在的目录,运行下面的命令
curl --user admin:admin -X POST -F "jenkinsfile=<JenkinsfileA" http://localhost:8080/pipeline-model-converter/validate

送上原文的链接 https://sandrocirulli.net/how-to-validate-a-jenkinsfile/

curl --user username:password -X POST -F "jenkinsfile=<Jenkinsfile" http://jenkins-url:8080/pipeline-model-converter/validate

< (尖括号)一定要,不然有问题

Note the following:

If your Jenkins is authenticating users, you need to pass the username and password otherwise you can omit that part.
By default, this command expects your Jenkinsfile to be called Jenkinsfile. If not, change the name in the command.
Replace jenkins_url and possibly port 8080 based on the URL and port where you are running Jenkins. You can also use localhost as URL if you are running Jenkins on your machine.

其他

VS Code 的插件也很不错 https://www.jenkins.io/blog/2018/11/07/Validate-Jenkinsfile/ ,但如果本地没有安装 VS Code 就还是用其他方法吧

Donate
  • Copyright: Copyright is owned by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.
  • Copyrights © 2019-2024 John Doe
  • Visitors: | Views:

请我喝杯咖啡吧~