Jenkinsfileのlintで救える命がある

Preview:

Citation preview

Jenkinsfile lint

Miyata Jumpei (@miyajan)

Jul 07, 2017 10 Jenkins

⾃⼰紹介

• 宮⽥ 淳平 (@miyajan)

• Cybozu

• ⽣産性向上チーム(⼀⼈)

• 社内で12のJenkinsを管理してます

Declarative Pipeline 😆

pipeline { agent any stage("Test") { steps { echo "test" } }}

よーし、試すぞー

Y^Y^Y^Y^Y

構⽂間違えた…

Jenkinsfile

• Groovy …

• push

Linter

• https://jenkins.io/doc/book/pipeline/development/#linter

• SSH curl

# JENKINS_SSHD_PORT=[sshd port on master]# JENKINS_HOSTNAME=[Jenkins master hostname]

$ ssh -p $JENKINS_SSHD_PORT $JENKINS_HOSTNAME declarative-linter < Jenkinsfile

SSH

# Assuming "anonymous read access" has been enabled on your Jenkins instance.# JENKINS_URL=[root URL of Jenkins master]# JENKINS_CRUMB is needed if your Jenkins master has CRSF protection enabled as it should

$ JENKINS_CRUMB=`curl "$JENKINS_URL/crumbIssuer/api/xml?xpath=concat(//crumbRequestField,\":\",//crumb)"`curl -X POST -H $JENKINS_CRUMB -F "jenkinsfile=<Jenkinsfile" $JENKINS_URL/pipeline-model-converter/validate

curl

(^o^)

jflint

• npm

• https://www.npmjs.com/package/jflint

• curl REST API

$ npm install jflint -g

Install

{  "jenkinsUrl": “http://your-jenkins.example.com”}

.jflintrc

$ jflint Jenkinsfile

Errors encountered validating Jenkinsfile:WorkflowScript: 4: Undefined section "stage" @ line 4, column 5. stage("Test") { ^

WorkflowScript: 1: Missing required section "stages" @ line 1, column 1. pipeline { ^

Usage

• git pre-commit hook

• Declarative Pipeline linter

• jflint

Recommended