Context

During my work at R2Devops, I had the mission to improve our hub pipeline. One of our most interesting job spell_check was broken and I had to fix it 🔨.

Unfortunately, the developer who writes it wasn’t here anymore and it wasn’t well documented.

Untitled

The console of the broken spell_check job 😢

That’s why I decided to rewrite it with another tool. Thanks to some research I found a helpful open-source tool and today I will share how I put it into reusable GitLab job.

Let’s dive into it 🤿

The miracle tool

If you have never heard of codespell, it’s a command line utility to check for common misspellings with the possibility to add your own dictionaries.

GitHub - codespell-project/codespell: check code for common misspellings

Almost 2k stars! Give them power too 🌟

Installation and usage is really simple :

pip install codespell=<version>
codespell <options>

What’s really nice with it, it’s the console output, which shows in order :

  1. The file where’s the typo
  2. The Misspelled word
  3. One or many rewrite suggestion

Untitled

In a nutshell, here are the cooles features :

  • Ignore specific files for the analysis
  • Use a custom dictionary with your words
  • Analyze files based on Regex
  • And many more…

Making a CI/CD jobs

In the process of writing a job, we have some standard to have a job that is easily usable, customizable and maintainable

1. Choosing the image

Choosing the image could seems innocuous, be it must be consider carrefully in order to respects the standards written before.

I first check on hub.docker.com, if there is an available image for codespell. As there isn’t any, I decide to take the official python image based on the lightweight Alpine distribution : python:3.10-alpine3.16.

2. Back on track with the right stage

Here are the common stages we defined :

  • build
  • tests
  • provision
  • review
  • release
  • deploy
  • others

For this stage the tests was the most appropriate, as it performs spell check in the code.

3. Use reusable variables

The Myspelling behavior of the hub project where it aims to be used, determines some condition for the job :

  • Misspell files inside a specific directory
  • Ignore some specific world in a dictionary file
  • Ignore some file inside this directory if they contains code

It brought us with the current content in the .gitlab-ci.yml file :

codespell:
  stage: tests
  image:
    name: python:3.10-alpine3.16
    entrypoint: [""]
  variables:
    CODESPELL_DICTIONARY: "dictionary.txt"
    #separate each file to ignore with a space
    CODESPELL_IGNORE_FILES: ""
    CODESPELL_VERSION: "2.2.1"
    IMAGE_TAG: "3.10-alpine3.16"

4. Write like a scribe

It is the core of the job, what command will be executed. This part written inside the script section of the file, is based on all previous elements and include them in the code to performs the determined condition.

This section won’t be described, if you’re interested, this job is available on the platform r2devops, here

Untitled