Chore: Upgrade Jenkins-job-builder to 6.3.0
[releng/global-jjb.git] / shell / pipeline-linter.sh
1 #!/bin/bash
2 # SPDX-License-Identifier: EPL-1.0
3 ##############################################################################
4 # Copyright (c) 2020 The Linux Foundation and others.
5 #
6 # All rights reserved. This program and the accompanying materials
7 # are made available under the terms of the Eclipse Public License v1.0
8 # which accompanies this distribution, and is available at
9 # http://www.eclipse.org/legal/epl-v10.html
10 ##############################################################################
11 echo "---> pipeline-linter.sh"
12
13 set -eu -o pipefail
14
15 # shellcheck disable=SC2153
16 JENKINS_CRUMB=$(curl --silent "$JENKINS_URL/crumbIssuer/api/xml?xpath=concat(//crumbRequestField,\":\",//crumb)")
17 JENKINS_VAL="$JENKINS_URL/pipeline-model-converter/validate"
18 mapfile -t JENKINS_FILE_LIST < <(grep -lr "^pipeline\s*{" vars src Jenkinsfile*)
19 exit_code=0
20
21 for JENKINS_FILE in "${JENKINS_FILE_LIST[@]}"; do
22     ret=$(curl --silent -X POST -H "$JENKINS_CRUMB" -F "jenkinsfile=<$JENKINS_FILE" "$JENKINS_VAL")
23     if [[ $ret == *"Errors"* ]];then
24         echo "ERROR: Linting error for $JENKINS_FILE"
25         exit_code=1
26     # Check for the line "agent any". This includes master as an agent.
27     elif grep "^\s*agent any" "$JENKINS_FILE"; then
28         echo "ERROR: $JENKINS_FILE is set to use any agent. Please specify a label instead."
29         exit_code=1
30     # If "any" is not used, check for master as a specified label or node.
31     elif grep -Pz "agent\s*\{\s*(label|node)\s+['\"]*master" "$JENKINS_FILE"; then
32         echo "ERROR: $JENKINS_FILE is set to use master as an agent. Please specify a different label."
33         exit_code=1
34     else
35         echo "$JENKINS_FILE successfully validated"
36     fi
37 done
38
39 # Set non-zero exit if linter reports any errors
40 exit "$exit_code"