Skip to content

Commit

Permalink
Merge pull request #302 from Lombiq/issue/OSOE-188
Browse files Browse the repository at this point in the history
OSOE-188: YAML Linting
  • Loading branch information
DemeSzabolcs authored Jan 26, 2024
2 parents 670d30a + 70e6288 commit cb50f65
Show file tree
Hide file tree
Showing 13 changed files with 194 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ runs:
shell: pwsh
run: |
(Resolve-Path "${{ github.action_path }}/../../../Scripts").Path >> $Env:GITHUB_PATH
- name: Check Current Workflow Equals Latest
shell: pwsh
id: check-current-workflow-equals-latest
Expand Down
4 changes: 2 additions & 2 deletions .github/actions/check-merge-queue-adds/action.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: Check Merge Queue Adds
description: >
Checks current pull request timeline add events to determine if it was ever added to a
merge queue. Intentionally not documented in Actions.md since it's only meant for
Checks current pull request timeline add events to determine if it was ever added to a
merge queue. Intentionally not documented in Actions.md since it's only meant for
internal use.
outputs:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ param(

# Filter actions based on files in action directory.
[array]$actionFiles = $FileIncludeList | Where-Object -FilterScript {
$itemDirectory = (Get-Item $PSItem).Directory.FullName
$isInGitHubDir = $itemDirectory -like '*/.github/*' -or $itemDirectory -eq '*/.github'
if (-not $isInGitHubDir) {
return $false
}

(Get-Item $PSitem).Directory.GetFiles('action.yml').Count -gt 0 -or
(Get-Item $PSitem).Directory.GetFiles('action.yaml').Count -gt 0
}
Expand All @@ -13,9 +19,16 @@ param(

# Filter workflow files excluding action yaml file names.
[array]$workflows = $FileIncludeList | Where-Object -FilterScript {
(Get-Item $PSitem).BaseName -ne 'action' -and
((Get-Item $PSitem).Extension -eq '.yml' -or
(Get-Item $PSitem).Extension -eq '.yaml')
try
{
(Get-Item $PSitem).BaseName -ne 'action' -and
((Get-Item $PSitem).Extension -eq '.yml' -or
(Get-Item $PSitem).Extension -eq '.yaml')
}
catch
{
return $false
}
}

# Combine actions and workflows.
Expand Down
6 changes: 3 additions & 3 deletions .github/actions/get-changed-gha-items/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ inputs:
required: false
default: '@()'
description: >
PowerShell string array of file names to return corresponding GitHub Actions (GHA) items. The
PowerShell string array of file names to return corresponding GitHub Actions (GHA) items. The
parameter must be a PowerShell string array.
outputs:
changed-items:
Expand All @@ -20,12 +20,12 @@ runs:
shell: pwsh
run: |
"${{ github.action_path }}" >> $Env:GITHUB_PATH
- name: Get GHA Items
id: get-gha-items
shell: pwsh
run: |
$params = @{
FileIncludeList = ${{ inputs.file-include-list }}
}
Get-GHA-Items-From-File-List @params
Get-GhaItemsFromFileList @params
1 change: 1 addition & 0 deletions .github/actions/spelling/allow.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
runtimes
21 changes: 21 additions & 0 deletions .github/actions/yaml-lint/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: YAML Linting
description: Runs a linter on YAML files.


inputs:
config-file-path:
description: 'Path to the yamllint configuration file'
required: true
default: '.yamllint.yaml'

search-path:
description: 'Path to search for YAML files'
required: true
default: '.'

runs:
using: "composite"
steps:
- name: Run yamllint
run: yamllint -c "${{ inputs.config-file-path }}" "${{ inputs.search-path }}"
shell: pwsh
16 changes: 16 additions & 0 deletions .github/workflows/yaml-lint-this-repo.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
name: YAML Linting

# Runs for PRs opened for any branch, and pushes to the dev branch.
on:
pull_request:
push:
branches:
- dev

jobs:
yaml-linting:
name: YAML Linting
uses: Lombiq/GitHub-Actions/.github/workflows/yaml-lint.yml@dev
with:
config-file-path: '.trunk/configs/.yamllint.yaml'
search-path: '.'
55 changes: 55 additions & 0 deletions .github/workflows/yaml-lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
name: YAML Linting

on:
workflow_call:
secrets:
CHECKOUT_TOKEN:
required: false
description: >
The GitHub token to authenticate checkout. Pass in a GitHub personal access token if authenticated submodules
are used.
inputs:
timeout-minutes:
required: false
type: number
default: 10
description: Configuration for the timeout-minutes parameter of the workflow. GitHub's default is 360.
cancel-workflow-on-failure:
description: If set to "true", this will cancel the current workflow run with all jobs if this workflow fails.
required: false
type: string
default: "true"
config-file-path:
description: 'Path to the yamllint configuration file'
required: true
type: string
default: '.yamllint.yaml'
search-path:
description: 'Path to search for YAML files'
required: true
type: string
default: '.'

jobs:
yaml-linting:
runs-on: ubuntu-22.04
name: YAML Linting Job
timeout-minutes: ${{ inputs.timeout-minutes }}
steps:
- name: Checkout
uses: Lombiq/GitHub-Actions/.github/actions/checkout@dev
with:
token: ${{ secrets.CHECKOUT_TOKEN }}

- name: YAML Linting
uses: Lombiq/GitHub-Actions/.github/actions/yaml-lint@dev
with:
config-file-path: ${{ inputs.config-file-path }}
search-path: ${{ inputs.search-path }}

- name: Cancel Workflow on Failure
if: failure() && inputs.cancel-workflow-on-failure == 'true'
uses: Lombiq/GitHub-Actions/.github/actions/cancel-workflow@dev
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
8 changes: 8 additions & 0 deletions .trunk/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
*out
*logs
*actions
*notifications
*tools
plugins
user_trunk.yaml
user.yaml
16 changes: 16 additions & 0 deletions .trunk/configs/.yamllint.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
yaml-files:
- '*.yaml'
- '*.yml'
- '.yamllint'

ignore: |
**/node_modules/**
**/.trunk/**
rules:
trailing-spaces: {}
key-duplicates: {}
quoted-strings: {
quote-type: any,
required: false
}
20 changes: 20 additions & 0 deletions .trunk/trunk.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# This file controls the behavior of Trunk: https://docs.trunk.io/cli
# To learn more about the format of this file, see https://docs.trunk.io/reference/trunk-yaml
version: 0.1
cli:
version: 1.17.2
# Trunk provides extensibility via plugins. (https://docs.trunk.io/plugins)
plugins:
sources:
- id: trunk
ref: v1.3.0
uri: https://github.com/trunk-io/plugins
# Many linters and tools depend on runtimes - configure them here. (https://docs.trunk.io/runtimes)
runtimes:
enabled:
- [email protected]
- [email protected]
# This is the section where you manage your linters. (https://docs.trunk.io/check/configuration)
lint:
enabled:
- [email protected]
1 change: 1 addition & 0 deletions Docs/Workflows.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ These features are designed to reduce resource usage (like paid GitHub Actions m
- [Spell-checking](Workflows/Productivity/SpellChecking.md)
- [Validate pull request](Workflows/Productivity/ValidatePullRequest.md)
- [Validate submodule](Workflows/Productivity/ValidateSubmodule.md)
- [Lint YAML files](Workflows/Productivity/YamlLinting.md)

## Azure hosting

Expand Down
34 changes: 34 additions & 0 deletions Docs/Workflows/Productivity/YamlLinting.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# YAML Linting

This solution uses [`yamllint`](https://github.com/adrienverge/yamllint) for linting YAML files according to a configuration file. Such a configuration file includes a set of rules that are checked against when linting the files and can be found in `/.trunk/configs/.yamllint.yaml`.

You would typically consume the corresponding GHA workflow for YAML linting like this:

```yaml
...

jobs:
yaml-linting:
name: YAML Linting
uses: Lombiq/GitHub-Actions/.github/workflows/yaml-lint.yml@dev
with:
config-file-path: 'tools/Lombiq.GitHub.Actions/.trunk/configs/.yamllint.yaml'
search-path: '.'
```
Where:
- `config-file-path`: Specifies the location of the `yamllint` rules file to use. See more details about such file [here](https://yamllint.readthedocs.io/en/stable/rules.html).
- `search-path`: Where the files to lint should be searched.

## Integration with VSCode

During local development, YAML linting can be enabled in VSCode via the [Trunk Check](https://marketplace.visualstudio.com/items?itemName=Trunk.io) extension. Such a tool will look for the `yamllint` configuration file located in the folder `/.trunk`, which is already setup.

Additionally, the linter can be run as a standalone tool:

```bash
yamllint -c ./.trunk/configs/.yamllint.yaml ./github
```

Optionally, the Trunk Code extension can be used in conjunction with the [YAML by Red Hat](https://marketplace.visualstudio.com/items?itemName=redhat.vscode-yaml) extension, which provides IntelliSense and description-on-hover capabilities based on a schema.

0 comments on commit cb50f65

Please sign in to comment.