-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ci: workflow to validate YAML schema of workflow files (#238)
Co-authored-by: Khuda Dad Nomani <[email protected]>
- Loading branch information
1 parent
31b1ebe
commit 6bc610c
Showing
3 changed files
with
101 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -161,4 +161,22 @@ jobs: | |
committer_username: asyncapi-bot | ||
committer_email: [email protected] | ||
commit_message: "ci: update of files from global .github repo" | ||
bot_branch_name: bot/update-files-from-global-repo | ||
bot_branch_name: bot/update-files-from-global-repo | ||
|
||
replicate_validate_workflow_schema: | ||
if: startsWith(github.repository, 'asyncapi/') | ||
name: Replicate workflow schema validation to repositories | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout repository | ||
uses: actions/checkout@v3 | ||
- name: Replicating file | ||
uses: derberg/manage-files-in-multiple-repositories@beecbe897cf5ed7f3de5a791a3f2d70102fe7c25 | ||
with: | ||
github_token: ${{ secrets.GH_TOKEN }} | ||
patterns_to_include: .github/workflows/validate-workflow-schema.yml | ||
topics_to_include: get-workflows-validation | ||
committer_username: asyncapi-bot | ||
committer_email: [email protected] | ||
commit_message: "ci: update of files from global .github repo" | ||
bot_branch_name: bot/update-files-from-global-repo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
name: YAML Lint Check | ||
|
||
on: | ||
pull_request: | ||
branches: | ||
- master | ||
paths: | ||
- '.github/workflows/**' | ||
|
||
jobs: | ||
yaml-lint: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout Code | ||
uses: actions/checkout@v2 | ||
|
||
- name: Install Dependencies | ||
run: npm install yaml-lint ajv axios js-yaml | ||
|
||
- name: Lint YAML Files | ||
run: | | ||
set -e | ||
find .github/workflows -type f -name "*.yml" -print0 | xargs -0 ./node_modules/.bin/yamllint -q | ||
- name: List YAML files to validate | ||
run: | | ||
set -e | ||
files=$(find .github/workflows -name '*.yml') | ||
echo "Validating the following files:" | ||
echo "$files" | ||
- name: Run YAML validation | ||
uses: actions/github-script@v6 | ||
with: | ||
script: | | ||
const Ajv = require("ajv"); | ||
const axios = require("axios"); | ||
const yaml = require("js-yaml"); | ||
const fs = require("fs").promises; | ||
async function validateWorkflows() { | ||
const schema = await axios.get( | ||
"https://raw.githubusercontent.com/SchemaStore/schemastore/master/src/schemas/json/github-workflow.json" | ||
); | ||
const files = await fs.readdir(".github/workflows"); | ||
const ymlFiles = files.filter((file) => file.endsWith(".yml")); | ||
for (const file of ymlFiles) { | ||
try { | ||
const content = await fs.readFile(`.github/workflows/${file}`, "utf8"); | ||
const target = yaml.load(content); | ||
const ajv = new Ajv({ strict: false, allErrors: true }); | ||
const validator = ajv.compile(schema.data); | ||
const valid = validator(target); | ||
if (!valid) { | ||
console.error( | ||
`Validation failed for file '${file}' with the following errors:` | ||
); | ||
console.error(validator.errors); | ||
process.exitCode = 1; | ||
} else { | ||
console.log(`Workflow in file '${file}' is valid`); | ||
} | ||
} catch (error) { | ||
console.error(`Error validating file '${file}': ${error.message}`); | ||
process.exitCode = 1; | ||
} | ||
} | ||
} | ||
validateWorkflows(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters