Add pipeline-verify jobs to lint and check agents
[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[@]}"
22 do
23     ret=$(curl --silent -X POST -H "$JENKINS_CRUMB" -F "jenkinsfile=<$JENKINS_FILE" "$JENKINS_VAL")
24     if [[ $ret == *"Errors"* ]];then
25         echo "ERROR: Linting error for $JENKINS_FILE"
26         exit_code=1
27     # Check for the line "agent any". This includes master as an agent.
28     elif grep "^\s*agent any" "$JENKINS_FILE"; then
29         echo "ERROR: $JENKINS_FILE is set to use any agent. Please specify a label instead."
30         exit_code=1
31     # If "any" is not used, check for master as a specified label or node.
32     elif grep -Pz "agent\s*\{\s*(label|node)\s+['\"]*master" "$JENKINS_FILE"; then
33         echo "ERROR: $JENKINS_FILE is set to use master as an agent. Please specify a different label."
34         exit_code=1
35     else
36         echo "$JENKINS_FILE successfully validated"
37     fi
38 done
39
40 # Set non-zero exit if linter reports any errors
41 exit "$exit_code"