diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 56243e7..3e364bc 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -8,11 +8,20 @@ stages: - sync include: + - local: $JOB_ROOT_DIR/yaml_lint/.gitlab-ci.yml - local: $JOB_ROOT_DIR/conventional_commits/.gitlab-ci.yml - local: $JOB_ROOT_DIR/git_push_mirror/.gitlab-ci.yml - local: $JOB_ROOT_DIR/gitlab_release/.gitlab-ci.yml +gilab-ci.yml Lint (python 3.6): + variables: + YAML_LINT_PATH: "/*/.gitlab-ci.yml" + extends: + - .yaml_lint_defaults + image: python:3.6-slim + + Gitlab Release: variables: MY_COMMAND: ./sub-folder_changlog.sh @@ -20,7 +29,6 @@ Gitlab Release: - .gitlab_release - Github (Push --mirror): variables: GIT_SYNC_URL: "https://$GITHUB_USERNAME_ROBOT:$GITHUB_TOKEN_ROBOT@github.com/NoFussComputing/gitlab-ci.git" diff --git a/yaml_lint/.gitlab-ci.yml b/yaml_lint/.gitlab-ci.yml new file mode 100644 index 0000000..0cd3eb6 --- /dev/null +++ b/yaml_lint/.gitlab-ci.yml @@ -0,0 +1,44 @@ + +.yaml_lint_defaults: + variables: + YAML_LINT_PATH: "/*.yml" + YAML_LINT_CONFIG: "/yaml_lint/.yamllint.yaml" + stage: validation + before_script: + - if [ "0$JOB_ROOT_DIR" == "0" ]; then ROOT_DIR=gitlab-ci; else ROOT_DIR=$JOB_ROOT_DIR ; fi + - echo "[DEBUG] ROOT_DIR[$ROOT_DIR]" + - mkdir -p "$CI_PROJECT_DIR/artifacts/$CI_JOB_STAGE/$CI_JOB_NAME" + - mkdir -p "$CI_PROJECT_DIR/artifacts/$CI_JOB_STAGE/tests" + - export PYTHON_VERSION=`python -c 'import sys; version=sys.version_info[:3]; print("{0}.{1}.{2}".format(*version))'` + - apt update + - apt install --no-install-recommends -y git + - python3 -m venv env + - . env/bin/activate + - pip install --upgrade pip + - pip install -r $ROOT_DIR/yaml_lint/requirements.txt + - mkdir $PYTHON_VERSION + script: + - YAML_LINT_PATH=$ROOT_DIR$YAML_LINT_PATH + - echo "[DEBUG] YAML_LINT_PATH[$YAML_LINT_PATH]" + - YAML_LINT_CONFIG=$ROOT_DIR$YAML_LINT_CONFIG + - echo "[DEBUG] YAML_LINT_CONFIG[$YAML_LINT_CONFIG]" + - yamllint $YAML_LINT_PATH -f parsable -c $ROOT_DIR/yaml_lint/.yamllint.yaml > "$CI_PROJECT_DIR/artifacts/$CI_JOB_STAGE/$CI_JOB_NAME/$PYTHON_VERSION-yaml-lint.log" 1>&1 || YAML_LINT=$? + - cat "$CI_PROJECT_DIR/artifacts/$CI_JOB_STAGE/$CI_JOB_NAME/$PYTHON_VERSION-yaml-lint.log" | ansible-lint-to-junit-xml > "$CI_PROJECT_DIR/artifacts/$CI_JOB_STAGE/tests/$PYTHON_VERSION-yaml-lint.junit.xml" + - if [ $YAML_LINT > 0 ]; then echo "YAML lint failed with $YAML_LINT"; exit $YAML_LINT; fi + after_script: + - echo deactivate + artifacts: + expire_in: 3 days + when: always + paths: + - "artifacts/*" + reports: + junit: + - "$CI_PROJECT_DIR/artifacts/$CI_JOB_STAGE/tests/*.junit.xml" + rules: + - if: '$CI_COMMIT_BRANCH == "master"' + when: never + - if: '$CI_COMMIT_BRANCH' + when: always + - when: never + diff --git a/yaml_lint/.yamllint.yaml b/yaml_lint/.yamllint.yaml new file mode 100644 index 0000000..ba30e87 --- /dev/null +++ b/yaml_lint/.yamllint.yaml @@ -0,0 +1,26 @@ +--- +extends: default + +rules: + braces: {max-spaces-inside: 1, level: error} + brackets: {max-spaces-inside: 1, level: error} + colons: {max-spaces-after: -1, level: error} + commas: {max-spaces-after: -1, level: error} + comments: disable + comments-indentation: disable + document-start: disable + empty-lines: + max: 2 + max-start: 1 + max-end: 1 + level: error + hyphens: {level: error} +# indentation: disable + indentation: + spaces: consistent + key-duplicates: enable + line-length: disable + new-line-at-end-of-file: disable + new-lines: {type: unix} + trailing-spaces: disable + truthy: disable diff --git a/yaml_lint/README.md b/yaml_lint/README.md new file mode 100644 index 0000000..03fddc6 --- /dev/null +++ b/yaml_lint/README.md @@ -0,0 +1,52 @@ +# Yaml Lint +This job does yaml linting when any commit is pushed to any branch. + + +This job provides the following badge: + +- None + +## Dependencies + +- None + +## your .gitlab-ci.yml changes +To use this job add the following to your `.gitlab-ci.yml` file + +``` yaml +stages: + - validation + +include: + - remote: https://gitlab.com/nofusscomputing/projects/gitlab-ci/-/raw/master/yaml_lint/.gitlab-ci.yml + +Yaml Lint (python 3.6): + variables: + YAML_LINT_PATH: "roles/" + YAML_LINT_CONFIG: ".yamllint.yaml" + extends: + - .yaml_linter_defaults + image: python:3.6-slim +``` +> You can use any python version you wish. + +## CI/CD Variables required + +| var name | Description | +|:----:|:----| +| YAML_LINT_PATH | *The path you wish the linter to search for yaml files* | +| YAML_LINT_CONFOG | *The path you have stored the yaml config file in* | + + +## Job Workflow + + - This job will lint any yaml file in the specified directory using the specified rules. + +## Artifacts + + - `$CI_PROJECT_DIR/artifacts` - Root artifact directory + - `$CI_PROJECT_DIR/artifacts/$CI_JOB_STAGE/tests/$PYTHON_VERSION-yaml-lint.junit.xml` - JUnit Test report + - `$CI_PROJECT_DIR/artifacts/$CI_JOB_STAGE/$CI_JOB_NAME/$PYTHON_VERSION-yaml-lint.log` - Linter log + +## License +To view the license for this folder and any sub-folders, refer [here](https://gitlab.com/nofusscomputing/projects/gitlab-ci) diff --git a/yaml_lint/requirements.txt b/yaml_lint/requirements.txt new file mode 100644 index 0000000..1013116 --- /dev/null +++ b/yaml_lint/requirements.txt @@ -0,0 +1,2 @@ +yamllint +ansible-lint-to-junit-xml