GitLab CI/CD使用经验,来自于莫纳什大学的考试任务解析

CI/CD简介

CI/CD的作用在于自动化和加速软件开发、测试和交付流程,通过持续集成确保代码协同工作和质量,通过持续交付降低风险,使每次代码变更都能够快速、高质量地交付到生产环境,从而提高软件开发效率、质量和协作。

作业要求

学校给出一个售货系统的代码,需要学生对于代码进行构建CI/CD(flake8,mypy,pycodestyle,pydocstyle,pylint),并且使用Python完善软件测试(test)。

解析方案

在构建CI/CD管道时,在使用配置文件创建静态分析时,我最初没有区分地将所有阶段合并在一起。在后来的过程中,我通过为每个阶段使用相同的阶段名称来解决这个问题。当测试没有被代码纠正时发生的测试用例。

Below is my code:

yml 复制代码
stages:
  - Static Analysis 
  - Test

flake8:
  stage: Static Analysis
  script:
    - pip install flake8
    - flake8 megamart.py
  allow_failure: true

mypy:
  stage: Static Analysis  
  script:
    - pip install mypy
    - mypy megamart.py
  allow_failure: true

pycodestyle:
  stage: Static Analysis
  script: 
    - pip install pycodestyle
    - pycodestyle megamart.py 
  allow_failure: true

pydocstyle:
  stage: Static Analysis
  script:
    - pip install pydocstyle
    - pydocstyle megamart.py
  allow_failure: true

pylint:
  stage: Static Analysis
  script:
    - pip install pylint
    - pylint megamart.py
  allow_failure: true

testing:
  stage: Test
  script:
    - python -m unittest test_megamart.py
  allow_failure: false

在修改完测试用例后可以登录你的GitLab可以看到你的测试结果已经通过,但是你的代码风格分析产生一些错误异常

分析代码风格产生异常的原因

PyCodeStyles

使用Python绘制一个柱状图来查看代码风格产生异常的错误代码

接下来根据每一个错误给出解决错误的方式

D200: One-line docstring should fit on one line with quotes (found 4):

这个错误只需要在导入一个模块的时候加上备注写入对于模块的描述就可以解决这个问题,下面是对比:

An Example of the Original Version.

python 复制代码
from datetime import datetime

An Example of the Fixed Version.

python 复制代码
"""
This is the Megamart module.

It is Megamart
"""
from datetime import datetime

D205: 1 blank line required between summary line and description

An Example of the Original Version.

python 复制代码
def is_not_allowed_to_purchase_item(item: Item, customer: Customer, purchase_date_string: str) -> bool:
  """
  Returns True if the customer is not allowed to purchase the specified item, False otherwise.
  If an item object or the purchase date string was not actually provided, an Exception should be raised.
  Items that are under the alcohol, tobacco or knives category may only be sold to customers who are aged 18+ and have their ID verified.
  An item potentially belongs to many categories - as long as it belongs to at least one of the three categories above, restrictions apply to that item.
  The checking of an item's categories against restricted categories should be done in a case-insensitive manner.
   For example, if an item A is in the category ['Alcohol'] and item B is in the category ['ALCOHOL'], both items A and B should be identified as restricted items.
  Even if the customer is aged 18+ and is verified, they must provide/link their member account to the transaction when purchasing restricted items.
  Otherwise, if a member account is not provided, they will not be allowed to purchase the restricted item even if normally allowed to.
  It is optional for customers to provide their date of birth in their profile.
  Purchase date string should be of the format dd/mm/yyyy.
  The age of the customer is calculated from their specified date of birth, which is also of the format dd/mm/yyyy.
  If an item is a restricted item but the purchase or birth date is in the incorrect format, an Exception should be raised.
  A customer whose date of birth is 01/08/2005 is only considered to be age 18+ on or after 01/08/2023.
  """

An Example of the Fixed Version.

python 复制代码
def is_not_allowed_to_purchase_item(item: Item, customer: Customer, purchase_date_string: str) -> bool:
  """
  Returns True if the customer is not allowed to purchase the specified item, False otherwise.

  Args:
      item (Item): The item being purchased.
      customer (Customer): The customer attempting the purchase.
      purchase_date_string (str): The purchase date in the format dd/mm/yyyy.

  Raises:
      Exception: If the provided item is None.

  If an item object or the purchase date string was not actually provided, an Exception should be raised.
  Items that are under the alcohol, tobacco or knives category may only be sold to customers who are aged 18+ and have their ID verified.
  An item potentially belongs to many categories - as long as it belongs to at least one of the three categories above, restrictions apply to that item.
  The checking of an item's categories against restricted categories should be done in a case-insensitive manner.
  For example, if an item A is in the category ['Alcohol'] and item B is in the category ['ALCOHOL'], both items A and B should be identified as restricted items.
  Even if the customer is aged 18+ and is verified, they must provide/link their member account to the transaction when purchasing restricted items.
  Otherwise, if a member account is not provided, they will not be allowed to purchase the restricted item even if normally allowed to.
  It is optional for customers to provide their date of birth in their profile.
  Purchase date string should be of the format dd/mm/yyyy.
  The age of the customer is calculated from their specified date of birth, which is also of the format dd/mm/yyyy.
  If an item is a restricted item but the purchase or birth date is in the incorrect format, an Exception should be raised.
  A customer whose date of birth is 01/08/2005 is only considered to be age 18+ on or after 01/08/2023.
  """

pyLine

C0301: Line too long (103/100) (line-too-long)

An Example of the Original Version.

python 复制代码
def is_not_allowed_to_purchase_item(item: Item, customer: Customer, purchase_date_string: str) -> bool:
  """
  Determine if the customer is allowed to purchase the specified item.

  Returns True if the customer is not allowed to purchase the specified item, False otherwise.
python 复制代码
def is_not_allowed_to_purchase_item(item: Item, 
                                    customer: Customer, 
                                    purchase_date_string: str) -> bool:
  """
  Determine if the customer is allowed to purchase the specified item.

  Returns True if the customer is not allowed to purchase the specified item, False otherwise.

  Args:
      item (Item): The item being purchased.
      customer (Customer): The customer attempting the purchase.
      purchase_date_string (str): The purchase date in the format dd/mm/yyyy.

PyDocstyle

Lizard

Mypy

Flake8

对比汇总表

其实这个项目中出现的错误有很多,但是因为篇幅问题我没有办法一一把示例代码给出,所以在此我制作出一个统计表格,总结下来CI/CD可以检测代码风格进行统一管理,方便于不同工程师之间进行协同时,因为有统一的代码和备注风格所以减少协同成本。

结论和建议

在"megamart"文件中,最常见的问题是"pyflakes ",其次是"pydocstyles "。这些问题大多与代码注释有关。为了解决这些问题,建议保持代码注释简洁,并遵循一致的代码格式标准。如果您正在使用集成开发环境(IDE),请考虑为自动代码格式化配置插件。

最复杂的函数是"checkout",因为它涉及18个逻辑条件,包括检查空值和特定数据类型。

相关推荐
明月心9526 分钟前
git remote add 用法
gitlab
爬山算法1 小时前
Hibernate(85)如何在持续集成/持续部署(CI/CD)中使用Hibernate?
java·ci/cd·hibernate
吹牛不交税3 小时前
gitea安装windows并实现CICD持续集成部署
ci/cd·gitea
only_Klein6 小时前
jenkins流水线报错:Connection reset by peer
ci/cd·kubernetes·gitlab·jenkins·ssl
_运维那些事儿6 小时前
skywalking链路追踪
java·运维·ci/cd·软件构建·skywalking·devops
学嵌入式的小杨同学16 小时前
从零打造 Linux 终端 MP3 播放器!用 C 语言实现音乐自由
linux·c语言·开发语言·前端·vscode·ci/cd·vim
u0104058361 天前
淘客返利系统的CI/CD流水线搭建:Docker镜像构建与K8s部署实践
ci/cd·docker·kubernetes
梁萌1 天前
docker部署gitlab和gitlab runner
docker·eureka·gitlab
手动阀行1 天前
守护发布的最后一道防线:将自动化红队测试深度嵌入 CI/CD 流水线,筑牢 MCP 应用持续交付的安全底座
安全·ci/cd·自动化
爱内卷的学霸一枚1 天前
现代DevOps实践:从CI/CD到GitOps的深度技术解析
运维·ci/cd·devops