gpt4 book ai didi

gitlab-ci - 用于检查代码格式的 gitlab CI 管道

转载 作者:行者123 更新时间:2023-12-04 01:29:02 24 4
gpt4 key购买 nike

我要安装 google-java-formatter在我的几个项目中。

但是,在提交代码时,我想要 CI pipline 检查是否已先完成格式化。

我知道我可以用 .gitlab-ci.yml 来完成这件事文件在我的根目录中,但是我非常不确定如何实现我检查所有文件格式是否正确的目标,有人可以帮助我如何使用 google-java-formatter 执行此操作吗?和 gitlab请。

最佳答案

google-java-formatter用于代码格式化
google-java-formatter对代码进行修改(格式化),然后更改要提交的代码。
根据 google-java-format源代码:

google-java-format is a program that reformats Java source code to comply with Google Java Style.


所以,你需要一个 pre-commit钩。
例如。您可以使用 pre-commit - 用于管理和维护多语言预提交 Hook 的框架。
您可以看到示例文件 here
.pre-commit-hooks.yaml:

- id: eclipse-formatter
name: Eclipse Java Formatter
description: This hook formats Java code with the Eclipse formatter.
entry: eclipse-formatter
language: python
types:
- java
- id: google-java-formatter
name: Google Java Formatter
description: This hook formats Java code with Google Java Formatter.
entry: google-java-formatter
language: python
types:
- java
如果您确实想将 git hooks 与 GitLab 集成, 尝试创建自定义 GitLab Server hook
linters 与格式化程序
如你所说:

when committing code I want the CI pipeline to check if the formatting has been done first.


你在问什么 - 是 checkstyle linting不是 formatting因此,要检查格式化是否已经完成,您可以使用一些 linter。
IE。 from this answer :
  • SpotBugs (更早的 Findbugs )用于查找现有错误。非常好!
  • PMD用于查找可能导致错误的模式(例如未使用的变量)
  • Checkstyle强制执行编码标准和约定(例如空格、Javadoc)
  • Error Prone直接 Hook 到应用程序的编译步骤
  • checkstyle短绒使用
    有很多指南,例如: GitLab CI/CD Pipeline for Maven-Based Applications – The Blog of Ivan Krizsan
    还有 250+ samples of .gitlab-ci.yml 带有`checkstyle。
    Client_Checkstyle:
    stage: test
    script:
    - mvn checkstyle:checkstyle
    - cat checkstyle-result.xml
    allow_failure: false

    关于gitlab-ci - 用于检查代码格式的 gitlab CI 管道,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61344217/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com