gitlab-runner.exe exec 调试 ci 配置

介绍

使用 gitlab-runner.exe exec 可以在本地调试 CI/CD 配置,而无需每次都提交代码到 GitLab,从而提高调试效率。

前置条件

在开始调试之前,请确保满足以下条件:

  • 已下载 gitlab-runner.exe
  • 配置好 config.toml 文件,并确保 gitlab-runner 能成功连接 GitLab。
  • 已安装 Python 及相关依赖(如 flake8)。

基本命令

在本地执行 GitLab CI/CD 配置的基本命令如下:

1
gitlab-runner.exe exec shell <job_name>

例如,执行 pep8_check 任务:

1
gitlab-runner.exe exec shell pep8_check

此命令会调用 gitlab-runner.exe,并在当前目录下执行 .gitlab-ci.yml 文件中的 pep8_check 任务。

编辑 .gitlab-ci.yml 并进行调试

在实际调试时,需要进入 .gitlab-ci.yml 所在的目录,并使用 exec 命令执行 job 任务。

示例配置:PEP8 代码风格检查

以下 gitlab-ci.yml 配置定义了 pep8_check 任务,使用 flake8 进行 Python 代码风格检查。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# 定义阶段
stages:
- lint

# PEP8 代码风格检查
test_pep8:
stage: lint
before_script:
- where python
- python --version
- python -c "import os, sys; print(os.path.join(os.path.dirname(sys.executable), 'Scripts', 'activate.bat'))" > activate_path.txt
- set /p ACTIVATE_PATH=<activate_path.txt
- call "%ACTIVATE_PATH%" envPython
- where python
- python --version
- pip install flake8
script:
- flake8 --version
- flake8 .
only:
- branches

在本地调试 pep8_check

  1. 进入 .gitlab-ci.yml 所在目录。

  2. 运行以下命令执行 pep8_check 任务:

    1
    gitlab-runner.exe exec shell pep8_check

仅检查提交的 Python 代码

由于历史原因,仓库中可能包含大量不符合 PEP8 规范的代码。为了避免检查旧代码,可以仅扫描本次提交中修改的 Python 文件。

过滤提交的 Python 文件

以下 gitlab-ci.yml 配置只检查本次提交中变更的 .py 文件。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# 定义阶段
stages:
- lint

# PEP8 代码风格检查
pep8_check:
stage: lint
before_script:
- where python
- python --version
- python -c "import os, sys; print(os.path.join(os.path.dirname(sys.executable), 'Scripts', 'activate.bat'))" > activate_path.txt
- set /p ACTIVATE_PATH=<activate_path.txt
- call "%ACTIVATE_PATH%" envPython
- where python
- python --version
script:
# 获取当前分支与目标分支的共同祖先 commit
- git merge-base HEAD origin/master > merge_base.txt
- set /p MERGE_BASE=<merge_base.txt
- git diff --name-only %MERGE_BASE% HEAD | findstr /i /r ".py" || (echo No .py files changed && exit 0)
- pip install flake8
- flake8 --version
- for /F "delims=" %%F in ('git diff --name-only %MERGE_BASE% HEAD ^| findstr /i /r ".py"') do (
flake8 --max-line-length=120 "%%F" || (echo Flake8 found issues in %%F && exit 0))
only:
- branches

本地调试命令示例

  1. 手动获取当前分支与 origin/master 的共同祖先 commit:

    1
    2
    git merge-base HEAD origin/master > merge_base.txt
    set /p MERGE_BASE=<merge_base.txt
  2. 获取本次提交变更的 .py 文件:

    1
    git diff --name-only %MERGE_BASE% HEAD | findstr /i /r ".py" || (echo No .py files changed)

    输出示例:

    1
    utils/constant.py
  3. 运行 flake8 仅检查变更的文件:

    1
    2
    for /F "delims=" %F in ('git diff --name-only %MERGE_BASE% HEAD ^| findstr /i /r ".py"') do (
    flake8 --max-line-length=120 "%F" || (echo Flake8 found issues in %F))

    示例输出:

    1
    2
    utils/constant.py:10:1: E303 too many blank lines (4)
    Flake8 found issues in utils/constant.py

总结

使用 gitlab-runner.exe exec shell <job_name> 进行本地调试,可以大幅提升 GitLab CI/CD 配置的调试效率。结合 git diff 过滤提交文件,可以避免对整个代码库执行不必要的检查,进一步优化 CI/CD 任务的执行效率。