Skip to content
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

Closed
wants to merge 11 commits into from
51 changes: 51 additions & 0 deletions .github/scripts/validate_yaml.go
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")
Copy link
Member

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

os.Exit(1)

Copy link
Contributor

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.

Copy link
Contributor Author

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

}
} else {
fmt.Println("All YAML files are valid 🎉.")
}
}
68 changes: 68 additions & 0 deletions .github/workflows/yaml-lint-checker.yaml
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 👀
runs-on: ubuntu-22.04
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
with:
fetch-depth: '0'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you need to fetch the entire history?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As we edit the release notes, we usually keep the history: #2542
image

But then before we merge into the sig-release repo, we clean up everything into a single commit.

I thought fetch-depth: 1 only fetches the latest commit? So unless we get the entire history, the workflow will only run on a single commit (which might be fine since the last commit will have the entire diff, but makes it a little harder to identify the invalid yaml during the release notes review).

- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: '1.22.4'
- 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)
Copy link
Member

Choose a reason for hiding this comment

The 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

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this works? not 100% sure that this works

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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

Copy link
Contributor

Choose a reason for hiding this comment

The 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}`
})

7 changes: 7 additions & 0 deletions go.mod
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
)
4 changes: 4 additions & 0 deletions go.sum
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=