-
Notifications
You must be signed in to change notification settings - Fork 5
77 lines (67 loc) · 2.62 KB
/
lint-validation.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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
uses: actions/github-script@v6
with:
script: |
const core = require('@actions/core');
const github = require('@actions/github');
const fs = require('fs');
const path = require('path');
async function run() {
try {
const token = core.getInput('token');
const octokit = github.getOctokit(token);
const workflowsDir = '.github/workflows';
let isValid = true;
let errorMessage = '';
const files = fs.readdirSync(workflowsDir);
for (const file of files) {
const fullPath = path.join(workflowsDir, file);
console.log(`Validating ${fullPath}...`);
const fileContents = fs.readFileSync(fullPath, 'utf8');
if (!fileContents.includes('name')) {
isValid = false;
errorMessage += `Validation failed for ${file}: Workflow must have a name\n`;
}
}
if (!isValid) {
const context = github.context;
const issue_number = context.issue.number || context.payload.pull_request?.number;
if (issue_number) {
await octokit.rest.issues.createComment({
...context.repo,
issue_number,
body: `🚨 Workflow Validation Error:\n\`\`\`\n${errorMessage}\n\`\`\``,
});
}
core.setFailed('One or more workflow validations failed.');
}
} catch (error) {
core.setFailed(error.message);
}
}
run();
env:
token: ${{ secrets.GITHUB_TOKEN }}