33# Validate GitHub action and workflow yaml schemas #
44# #########################################################
55
6- GITHUB_DIR =" $( cd -- " $( dirname -- " ${BASH_SOURCE[0]} " ) /.. " & > /dev/null && pwd) "
7- readonly GITHUB_DIR
6+ SCRIPT_DIR =" $( cd -- " $( dirname -- " ${BASH_SOURCE[0]} " ) " & > /dev/null && pwd) "
7+ readonly SCRIPT_DIR
88
9- source " $GITHUB_DIR /scripts/shellUtils.sh"
9+ source " $SCRIPT_DIR /shellUtils.sh"
10+
11+ readonly REPO_ROOT=" ${REPO_ROOT:- .} "
1012
1113title " Validating the Github Actions and workflows using the json schemas provided by (https://www.schemastore.org/json/)"
1214
@@ -21,7 +23,7 @@ for SCHEMA in github-action.json github-workflow.json; do
2123 if curl " https://json.schemastore.org/$SCHEMA " --output " $TEMP_SCHEMA_DIR /$SCHEMA " --silent; then
2224 success " Successfully downloaded $SCHEMA schema!"
2325 else
24- error " Failed downloading $SCHEMA schema"
26+ error " Failed downloading $SCHEMA schema" >&2
2527 exit 1
2628 fi
2729done
@@ -33,7 +35,10 @@ info "Validating action metadata files against their JSON schema..."
3335echo
3436
3537# Get all actions, delimited by -d (data arg for ajv)
36- ACTIONS=" $( find " $GITHUB_DIR /.." -type f \( -name " action.yml" -o -name " action.yaml" \) -exec echo -n " -d " {} \; ) "
38+ ACTIONS=" $( find " $REPO_ROOT " -type f \( -name " action.yml" -o -name " action.yaml" \) -exec echo -n " -d " {} \; ) "
39+ if [[ -z " $ACTIONS " ]]; then
40+ warning " No actions found. Did you remember to run this script from the root of a repo?" >&2
41+ fi
3742
3843# Disabling shellcheck because we WANT word-splitting on ACTIONS in this case
3944# shellcheck disable=SC2086
@@ -46,7 +51,10 @@ info "Validating workflows against their JSON schema..."
4651echo
4752
4853# Get all workflows, delimited by -d (data arg for ajv)
49- WORKFLOWS=" $( find " $GITHUB_DIR /workflows" -type f \( -name " *.yml" -o -name " *.yaml" \) -exec echo -n " -d " {} \; ) " \
54+ WORKFLOWS=" $( find " ${REPO_ROOT} /.github/workflows" -type f \( -name " *.yml" -o -name " *.yaml" \) -exec echo -n " -d " {} \; ) "
55+ if [[ -z " $WORKFLOWS " ]]; then
56+ warning " No workflows found. Did you remember to run this script from the root of a repo?" >&2
57+ fi
5058
5159# shellcheck disable=SC2086
5260if ! npx ajv --strict=false -s " $TEMP_SCHEMA_DIR /github-workflow.json" $WORKFLOWS ; then
5664echo
5765
5866if [[ $EXIT_CODE -ne 0 ]]; then
59- error " Some actions and/or workflows are invalid"
67+ error " Some actions and/or workflows are invalid" >&2
6068 exit $EXIT_CODE
6169fi
6270
0 commit comments