![](/img/trans.png)
[英]Gitlab CI doesn't pull updated docker image from cr to EC2 - [image] is up-to-date
[英]How to add a Gitlab CI job to validate generated open-api documentation is up-to-date
(在我的项目中,我有 docs 文件夹,在文件夹下我有 open-api.yaml)
OpenApi 文档文件是在运行时生成的。/mvnw verify
我想添加一个 Gitlab 作业,我将在其中执行以下步骤:
我必须创建一个预脚本才能运行。/mvn verify
然后,检查生成的文件是否与 git 中的文件不同
如果文件不同,则说明 openapi 没有正确更新,需要报错。
请你帮助我好吗?
您可以在运行生成脚本后使用git diff --exit-code
来断言文件是否已更改。
例如,假设您修改现有文件然后运行git diff --exit-code
,该命令将退出非零。 在 gitlab 作业中,这意味着作业将失败。
$ echo "foo" >> existing-file.txt
$ git diff --exit-code # exits non-zero (failure)
因此,您可以有一个 gitlab 作业来运行您的生成脚本,然后检查文件是否已更改。 如果文件未更改,则命令退出 0 并且作业通过。
check_openapi:
stage: .pre
# ...
script:
- ./mvnw verify # generate the openapi docs
- git diff --exit-code # fails if the files tracked by git have changed
请务必注意, git diff
仅适用于跟踪的 git 文件。 因此,如果您的生成代码可能添加新文件,则应确保为任何新创建的文件运行git add --intent-to-add
。 否则,您可能会错过一些情况,因为默认情况下 git 不会跟踪新文件。 您可以将此添加到 CI 作业中,也可以将其合并到您的代码生成脚本中。
例如:
$ echo "foo" > newfile.txt
$ git diff --exit-code # exits 0 (success?!)
$ echo "foo" > newfile.txt
$ git add --intent-to-add ./newfile.txt
$ git diff --exit-code # exits non-zero (failure)
因此,如果您的生成脚本没有运行git add --intent-to-add
作为其过程的一部分,完整的解决方案可能如下所示:
check_openapi:
stage: .pre
# ...
script:
- ./mvnw verify
- git add --intent-to-add . # make sure new files are tracked/diff'd
- git diff --exit-code
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.