Skip to content
Snippets Groups Projects
Commit 98ec0df1 authored by Florian Forster's avatar Florian Forster
Browse files

Merge branch 'fforster/yamlfmt' into 'main'

feat(yamlfmt): Migrate to the "pattern file" discovery method.

See merge request gitlab-com/gl-infra/common-ci-tasks!871
parents cb786c6b 37411bed
No related branches found
No related tags found
No related merge requests found
Pipeline #24649445 failed
Loading
Loading
@@ -20,7 +20,27 @@ include:
 
The *yamlfmt* job provides very limited configuration options. This is by design.
 
### Excluding files
### Including and excluding files
 
To exclude files, create a file named `.yamlfmtignore` at the root of your repository.
The file uses [gitignore](https://git-scm.com/docs/gitignore) syntax to excempt some files, such as test data, from formatting.
The `.yamlfmt.patterns` file allows to specify additional files and/or exclude files from the format check.
It is sometimes desireable to check the format of files that do not have the usual `.yaml` or `.yml` suffix.
At other times it is necessary to exclude files from the format check, for example intentionally malformed test data.
To include or exclude files, create a file named `.yamlfmt.patterns` at the root of your repository.
Files to **include** are added as normal (positive) entries, for example:
```gitignore
/.some-tool # dotfile using YAML syntax but without the suffix
```
Files to **exclude** are added a negated entries, for example:
```gitignore
!testdata/
```
The `.yamlfmt.patterns` file uses [gitignore](https://git-scm.com/docs/gitignore) syntax.
Despite the name, only negated entries (beginning with an exclamation point `!`) are *excluded*, all other entries are *included*.
The `.yamlfmt.patterns` file is combined with [the default `yamlfmt.patterns` file](https://gitlab.com/gitlab-com/gl-infra/common-ci-tasks-images/-/blob/main/yamlfmt/yamlfmt.patterns?ref_type=heads) with the default file coming first and the `.yamlfmt.patterns` file second/below.
Loading
Loading
@@ -11,28 +11,35 @@ yamlfmt:
name: registry.gitlab.com/gitlab-com/gl-infra/common-ci-tasks-images/yamlfmt:${GL_ASDF_YAMLFMT_VERSION}
entrypoint: [""]
script:
# yamlfmt will fail if this file doesn't exist.
- touch .yamlfmtignore
- |
declare -a PATTERNS=('*.yaml' '*.yml')
if [[ -e .yamlfmtextra ]]; then
IFS=' '
while read -r pattern; do
PATTERNS+=("${pattern}")
done <<<$(grep -E -v '^\s*(#|$)' .yamlfmtextra)
fi
# Terminal colors
bright_red="$(printf "\e[31;1m")"
blue="$(printf "\e[34m")"
color_reset="$(printf "\e[0m")"
- |
# Dump yamlfmt config
echo "# /etc/yamlfmt.yaml"
echo -n "${blue}"
cat /etc/yamlfmt.yaml
- yamlfmt -conf /etc/yamlfmt.yaml "${PATTERNS[@]}"
echo -n "${color_reset}"
- |
# Dump pattern file
PATTERN_FILES=("/etc/yamlfmt.patterns")
if [[ -e .yamlfmt.patterns ]]; then
PATTERN_FILES+=(".yamlfmt.patterns")
fi
for pattern_file in "${PATTERN_FILES[@]}"; do
echo "# ${pattern_file}"
echo -n "${blue}"
cat "${pattern_file}"
echo -n "${color_reset}"
done
- yamlfmt -conf /etc/yamlfmt.yaml "${PATTERN_FILES[@]}"
- git diff | tee yamlfmt.patch
- |
if [[ -s yamlfmt.patch ]]; then
red="$(printf "\e[31;1m")"
reset="$(printf "\e[0m")"
echo "${red}Some YAML files are not formatted correctly. Run the following command in the repository root to fix:${reset}"
echo "${red}# curl ${CI_PROJECT_URL}/-/jobs/${CI_JOB_ID}/artifacts/raw/yamlfmt.patch | patch -p1${reset}"
echo "${bright_red}Some YAML files are not formatted correctly. Run the following command in the repository root to fix:${color_reset}"
echo "${bright_red}# curl ${CI_PROJECT_URL}/-/jobs/${CI_JOB_ID}/artifacts/raw/yamlfmt.patch | patch -p1${color_reset}"
exit 1
fi
after_script: |
Loading
Loading
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment