Skip to content
Snippets Groups Projects
Unverified Commit 5f0b08bd authored by Florian Forster's avatar Florian Forster
Browse files

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

* Print a diff of the changes to STDOUT.
* Upload a patch that users can apply to fix the issues.
* Print instructions to download and apply the patch.

Issue: gitlab-com/gl-infra/common-template-copier#6
parent ccc5ffdd
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:
- |
# Create default config if .yamlfmt.yaml doesn't exist.
if [ ! -e .yamlfmt.yaml ]; then
cat >.yamlfmt.yaml <<EOF
formatter:
type: basic
retain_line_breaks_single: true
scan_folded_as_literal: true
drop_merge_tag: true
pad_line_comments: 2
eof_newline: true
EOF
if [ -e .yamlfmtignore ]; then
echo "gitignore_path: .yamlfmtignore" >>.yamlfmt.yaml
echo "gitignore_excludes: true" >>.yamlfmt.yaml
fi
fi
echo "# .yamlfmt.yaml"
cat .yamlfmt.yaml
- yamlfmt -conf .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