-
Notifications
You must be signed in to change notification settings - Fork 402
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Lint yaml workflow update #2547
Changes from 4 commits
b7bb4ca
6f564be
5704e85
f1c7199
634970b
d72bb9c
3b3105e
72a2b19
79d0111
2e0645c
6e4e5da
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"strings" | ||
|
||
"sigs.k8s.io/yaml" | ||
) | ||
|
||
// validateYAML reads and parses the YAML file at the given path. | ||
func validateYAML(filePath string) string { | ||
data, err := os.ReadFile(filePath) | ||
if err != nil { | ||
return fmt.Sprintf("Error reading %s:\n%s", filePath, err.Error()) | ||
} | ||
|
||
var content interface{} | ||
if err := yaml.UnmarshalStrict(data, &content); err != nil { | ||
return fmt.Sprintf("Error in %s:\n%w", filePath, err.Error()) | ||
} | ||
|
||
return "Valid" | ||
} | ||
|
||
func main() { | ||
if len(os.Args) < 2 { | ||
fmt.Println("No YAML files specified.") | ||
os.Exit(1) | ||
} | ||
|
||
files := os.Args[1:] | ||
invalidFiles := []string{} | ||
|
||
for _, file := range files { | ||
result := validateYAML(file) | ||
if result != "Valid" { | ||
invalidFiles = append(invalidFiles, result) | ||
} | ||
} | ||
|
||
if len(invalidFiles) > 0 { | ||
fmt.Println("YAML Validation Failed:\n") | ||
for _, errorMsg := range invalidFiles { | ||
fmt.Println(errorMsg) | ||
fmt.Println("\n" + strings.Repeat("-", 40) + "\n") | ||
} | ||
} else { | ||
fmt.Println("All YAML files are valid 🎉.") | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
--- | ||
name: Yaml Lint Release Notes Pull Request | ||
on: | ||
pull_request: | ||
paths: | ||
- releases/**/release-notes/**.yaml | ||
# Allow manual triggering | ||
workflow_dispatch: {} | ||
|
||
concurrency: | ||
group: ${{ github.ref }}-${{ github.workflow }} | ||
cancel-in-progress: true | ||
jobs: | ||
yaml-lint-checker: | ||
name: Release Notes Check Yaml 👀 | ||
Checksumz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
runs-on: ubuntu-22.04 | ||
Checksumz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if: ${{ !github.event.pull_request.draft }} | ||
steps: | ||
- name: Cancel Previous Actions | ||
uses: styfle/cancel-workflow-action@0.9.1 | ||
with: | ||
access_token: ${{ github.token }} | ||
- name: Check out code | ||
uses: actions/checkout@v4 | ||
Checksumz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
with: | ||
fetch-depth: '0' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do you need to fetch the entire history? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As we edit the release notes, we usually keep the history: #2542 But then before we merge into the sig-release repo, we clean up everything into a single commit. I thought |
||
- name: Set up Go | ||
uses: actions/setup-go@v5 | ||
with: | ||
go-version: '1.22.4' | ||
Checksumz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
- name: Install go dependencies | ||
run: go mod download | ||
- name: Build go prog | ||
run: go build -o validate_yaml .github/scripts/validate_yaml.go | ||
- name: Validate YAML | ||
id: validate_yaml | ||
run: | | ||
INVALID_FILES="" | ||
# check diff for invalid yaml files | ||
git diff --name-only ${{ github.event.pull_request.base.sha }} ${{ github.sha }} -- releases/ | grep -E '\.ya?ml$' | ||
CHANGED_FILES=$(git diff --name-only $(git merge-base origin/master HEAD) -- releases/ | grep -E '\.ya?ml$' || true) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. for this part you maybe can use this action that get all you need: https://github.com/tj-actions/changed-files There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. checking this |
||
if [ -n "$CHANGED_FILES" ]; then | ||
echo "Validating yaml files: \n${CHANGED_FILES}" | ||
OUTPUT=$(./validate_yaml $CHANGED_FILES) | ||
if [[ "$OUTPUT" == *"YAML Validation Failed"* ]]; then | ||
# If there are invalid files, set the output variable and fail the job. GITHUB_ENV is a special file that | ||
# allows you to set env variables for subsequent steps in the same job | ||
echo "invalid_files<<EOF" >> $GITHUB_ENV | ||
echo "$OUTPUT" >> $GITHUB_ENV | ||
echo "EOF" >> $GITHUB_ENV | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this works? not 100% sure that this works There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Verified on my local branch that this allows to create a multiline variable for use in the next step. Will send a screenshot with echo later There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Release action from today's release meeting for reference : https://github.com/kubernetes-sigs/release-actions |
||
exit 1 | ||
fi | ||
else | ||
echo "No YAML files changed under /releases/*" | ||
fi | ||
- name: Comment on PR if invalid yaml detected | ||
if: failure() | ||
uses: actions/github-script@v7 | ||
with: | ||
script: | | ||
const invalidFiles = process.env.invalid_files.trim(); | ||
github.rest.issues.createComment({ | ||
owner: context.repo.owner, | ||
repo: context.repo.repo, | ||
issue_number: context.issue.number, | ||
body: `The following YAML files are invalid:\n\n${invalidFiles}` | ||
}) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
module yaml-validator | ||
|
||
go 1.22.4 | ||
|
||
require ( | ||
sigs.k8s.io/yaml v1.4.0 // indirect | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | ||
sigs.k8s.io/yaml v1.4.0 h1:Mk1wCc2gy/F0THH0TAp1QYyJNzRm2KCLy3o5ASXVI5E= | ||
sigs.k8s.io/yaml v1.4.0/go.mod h1:Ejl7/uTz7PSA4eKMyQCUTnhZYNmLIl+5c2lQPGR2BPY= |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
here should exit with error
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we want to print all the invalid files, and then exit after the loop.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As Nina mentioned, this is to gather all the output into the variable on failure