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

Merge branch 'fforster/yamlfmt' into 'main'

feat(yamlfmt): Add shared CI definition for `yamlfmt`.

See merge request gitlab-com/gl-infra/common-ci-tasks!838
parents ccc5ffdd 76acb16c
No related branches found
No related tags found
No related merge requests found
# yamlfmt
Runs **[yamlfmt](https://github.com/google/yamlfmt)** to format YAML files uniformly.
## Setup
Simply include `yamlfmt.yml` in your `.gitlab-ci.yml` file:
```yaml
stages:
- validate
include:
- project: "gitlab-com/gl-infra/common-ci-tasks"
ref: v2.47.0 # renovate:managed
file: yamlfmt.yml
```
## Configuration
The *yamlfmt* job provides very limited configuration options. This is by design.
### 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.
spec:
inputs:
stage:
default: validate
---
yamlfmt:
stage: $[[ inputs.stage ]]
needs: []
image:
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
- |
# Dump yamlfmt config
echo "# /etc/yamlfmt.yaml"
cat /etc/yamlfmt.yaml
- yamlfmt -conf /etc/yamlfmt.yaml .
- 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}"
exit 1
fi
after_script: |
cat <<-EOD
----------------------------------------------------------
Need help? Documentation on the yamlfmt CI job can be found at:
https://gitlab.com/gitlab-com/gl-infra/common-ci-tasks/-/blob/main/yamlfmt.md
EOD
rules:
- if: '$GL_ASDF_YAMLFMT_VERSION == null || $GL_ASDF_YAMLFMT_VERSION == ""'
when: 'never'
- if: '$CI_PIPELINE_SOURCE == "schedule"'
when: 'never'
- if: '($CI_PIPELINE_SOURCE == "merge_request_event") || ($CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH)'
allow_failure: true
artifacts:
paths:
- yamlfmt.patch
expose_as: 'yamlfmt patch'
when: on_failure
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