Skip to content

Python


This folder (python) covers jobs for python development

These jobs provides the following badge:

  • PyLint - code quality PyLint Score

Use the following MD to add a badge adjusting the variables and ensuring everything is on one line.

[![PyLint Score](https://img.shields.io/badge/dynamic/json?&style=plastic&logo=python&label=PyLint%20Score&query=%24.PyLintScore&url=https%3A%2F%2Fgitlab.com%2F

{project path}

%2F-%2Fjobs%2Fartifacts%2F

{branch}

%2Fraw%2Fartifacts%2Fvalidation%2FPyLint%2Fbadge_pylint.json%3Fjob%3D

{Job Name}

)](https://gitlab.com/

{project path}

/-/jobs/artifacts/

{branch}

/file/

artifacts/validation/tests/gl-code-quality-report.html

?job=

{Job Name}
)
Variable Description
{project path} project path, what's after gitlab.com/
{branch} git branch to fetch the score from
{Job Name} name of the gitlab-ci job for the linting

Dependencies

  • None

your .gitlab-ci.yml changes

To add the PyLint job, add the following to your .gitlab-ci.yml file

stages:
    - validation

include:
    - remote: https://gitlab.com/nofusscomputing/projects/gitlab-ci/-/raw/master/python/.gitlab-ci.yml

PyLint:
    variables:
        PYLINT_PATH: "/*/*.py"
        PYLINT_RC_PATH: "."
    extends:
        - .PyLint
    image: python:3.6-slim

CI/CD Variables required

var name Description
PYLINT_PATH The path you wish the linter to search for python files
PYLINT_RC_PATH The path to your .pylintrc file.

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/gl-code-quality-report.json - Gitlab code quality report (displays in merge request)

  • $CI_PROJECT_DIR/artifacts/$CI_JOB_STAGE/tests/gl-code-quality-report.html - html code quality report

Gitlab job Definition

When you include this definition the following makes up the job definition

.gitlab-ci.yml
.pylint_badge: &pylint_badge |
    echo "{
        \"PyLintScore\": \"$PyPIScore\"
    }" > "$CI_PROJECT_DIR/artifacts/$CI_JOB_STAGE/$CI_JOB_NAME/badge_pylint.json"

.PyLint:
  variables:
    PYLINT_PATH: "/*/python-module/*/*.py"
    PYLINT_RC_PATH: "/python/.pylintrc"
  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/python/requirements.txt
    - mkdir $PYTHON_VERSION
  script:
    - PYLINT_PATH=$ROOT_DIR$PYLINT_PATH
    - echo "[DEBUG] PYLINT_PATH[$PYLINT_PATH]"
    - PYLINT_RC_PATH=$ROOT_DIR$PYLINT_RC_PATH
    - echo "[DEBUG] PYLINT_RC_PATH[$PYLINT_RC_PATH]"
    - python3 -m pylint --rcfile $PYLINT_RC_PATH --exit-zero --output-format=pylint_gitlab.GitlabCodeClimateReporter $PYLINT_PATH > "$CI_PROJECT_DIR/artifacts/$CI_JOB_STAGE/tests/gl-code-quality-report.json"

    - python3 -m pylint --rcfile $PYLINT_RC_PATH --exit-zero --output-format=pylint_gitlab.GitlabPagesHtmlReporter $PYLINT_PATH > "$CI_PROJECT_DIR/artifacts/$CI_JOB_STAGE/tests/gl-code-quality-report.html"

    - PyPIScore=$(python3 -m pylint --rcfile $PYLINT_RC_PATH --exit-zero $PYLINT_PATH | sed -n 's/^Your code has been rated at \([-0-9./]*\).*/\1/p')

    - *pylint_badge

  after_script:
    - echo deactivate
  artifacts:
    expire_in: 1 days
    when: always
    paths:
      - "artifacts/*"
    reports:
      codequality: "$CI_PROJECT_DIR/artifacts/$CI_JOB_STAGE/tests/gl-code-quality-report.json"
  rules:

    - if: # condition_master_branch_push
        $CI_COMMIT_BRANCH == "master" && 
        $CI_PIPELINE_SOURCE == "push"
      exists:
        #- '{**/,*,!gitlab-ci/**,!website-template/**}*.py'
        - '{**/,*}*.py'
      when: always

    - if: # condition_development_branch_push
        $CI_COMMIT_BRANCH == "development" && 
        $CI_PIPELINE_SOURCE == "push"
      exists:
        #- '{**/,*,!gitlab-ci/**,!website-template/**}*.py'
        - '{**/,*}*.py'
      changes:
        paths:
          #- '{**/,*,!gitlab-ci/**,!website-template/**}*.py'
          - '{**/,*}*.py'
        compare_to: 'master'
      when: always

    - if: # condition_not_master_or_dev_push
        $CI_COMMIT_BRANCH != "master" && 
        $CI_COMMIT_BRANCH != "development" && 
        $CI_PIPELINE_SOURCE == "push"
      exists:
        #- '{**/,*,!gitlab-ci/**,!website-template/**}*.py'
        - '{**/,*}*.py'
      changes:
        paths:
          #- '{**/,*,!gitlab-ci/**,!website-template/**}*.py'
          - '{**/,*}*.py'
        compare_to: 'development'
      when: always

    - when: never

Note

Docs Still under development

About:

This page forms part of our Project Gitlab-CI.

Page Metadata
Version: ToDo: place files short git commit here
Date Created: 2021-08-11
Date Edited: 2023-05-23

Contribution:

Would You like to contribute to our Gitlab-CI project? You can assist in the following ways:

 

ToDo: Add the page list of contributors