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个逻辑条件,包括检查空值和特定数据类型。

相关推荐
亚林瓜子8 小时前
AWS Elastic Beanstalk + CodePipeline(Python Flask Web的国区CI/CD)
python·ci/cd·flask·web·aws·beanstalk·codepipeline
龙智DevSecOps解决方案12 小时前
游戏开发中的CI/CD优化案例:知名游戏公司Gearbox使用TeamCity简化CI/CD流程
ci/cd·游戏开发·jetbrains·teamcity
星释2 天前
如何自动部署GitLab项目
gitlab
keson要进步2 天前
CICD实战(一) -----Jenkins的下载与安装
运维·ci/cd·centos·自动化·jenkins
keson要进步2 天前
CICD实战(二)-----gitlab的安装与配置
linux·运维·gitlab
猫头虎2 天前
[特殊字符]解决 “IDEA 登录失败。不支持早于 14.0 的 GitLab 版本” 问题的几种方法
java·ide·网络协议·http·https·gitlab·intellij-idea
风早君2 天前
jenkins集成gitlab发布到远程服务器
服务器·gitlab·jenkins
爱宇阳2 天前
使用 Docker Compose 部署 Jenkins(LTS 版)持续集成环境
ci/cd·docker·jenkins
头疼的程序员4 天前
Jenkins的学习与使用(CI/CD)
ci/cd·jenkins
试剂界的爱马仕4 天前
软珊瑚成分 CI-A:靶向口腔癌细胞的 “氧化利剑” 与 ERK 密码
网络·人工智能·科技·机器学习·ci/cd·ai写作