-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[feat] add workflow to lint and validate reusable workflows
- Loading branch information
Showing
3 changed files
with
86 additions
and
0 deletions.
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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
--- | ||
extends: default | ||
|
||
rules: | ||
line-length: disable | ||
new-lines: | ||
type: unix | ||
new-line-at-end-of-file: | ||
level: warning | ||
trailing-spaces: | ||
level: warning |
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,75 @@ | ||
name: Workflow Linting and Validation | ||
|
||
on: | ||
pull_request: | ||
paths: | ||
- '.github/workflows/**' | ||
push: | ||
branches: | ||
- main | ||
- master | ||
paths: | ||
- '.github/workflows/**' | ||
|
||
jobs: | ||
lint-and-validate: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout code | ||
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1 | ||
|
||
- name: Linting workflow files | ||
uses: ibiqlik/action-yamllint@2576378a8e339169678f9939646ee3ee325e845c # v3.1.1 | ||
with: | ||
config_file: .github/linters/.yamllint.yml | ||
|
||
- name: Validate GitHub Actions workflows | ||
id: validate | ||
uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1 | ||
with: | ||
script: | | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const { Octokit } = require("@octokit/core"); | ||
const octokit = new Octokit({ auth: process.env.GITHUB_TOKEN }); | ||
const workflowsDir = '.github/workflows'; | ||
let isValid = true; | ||
let errorMessage = ''; | ||
fs.readdirSync(workflowsDir).forEach(file => { | ||
const fullPath = path.join(workflowsDir, file); | ||
console.log(`Validating ${fullPath}...`); | ||
try { | ||
// This is a simple placeholder for actual validation logic | ||
// Actual validation should be implemented as per specific requirements | ||
const fileContents = fs.readFileSync(fullPath, 'utf8'); | ||
if (!fileContents.includes('name')) { | ||
throw new Error('Workflow must have a name'); | ||
} | ||
} catch (error) { | ||
isValid = false; | ||
errorMessage += `Validation failed for ${file}: ${error.message}\n`; | ||
} | ||
}); | ||
if (!isValid) { | ||
// Post a comment on the PR or commit if the validation fails | ||
const context = github.context; | ||
const issue_number = context.issue.number || context.payload.pull_request.number; | ||
const owner = context.repo.owner; | ||
const repo = context.repo.repo; | ||
const comment = { | ||
owner, | ||
repo, | ||
issue_number, | ||
body: `🚨 Workflow Validation Error:\n\`\`\`\n${errorMessage}\n\`\`\``, | ||
}; | ||
octokit.request('POST /repos/{owner}/{repo}/issues/{issue_number}/comments', comment); | ||
throw new Error('One or more workflow validations failed.'); | ||
} | ||
env: | ||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
|
File renamed without changes.