[英]GitLab CI/CD: Trigger pipeline only when a specific file have changed
I am running compliance scans against directories where a commit is done on a terragrunt.hcl
file.我正在对在terragrunt.hcl
文件上完成提交的目录运行合规性扫描。
I would like the pipeline from my . gitlab-ci.yaml
我想要我的管道. gitlab-ci.yaml
. gitlab-ci.yaml
to be triggered if and only if a commit to this specific file “ terragrunt.hcl
” within a directory and its sub-directories have changed. . gitlab-ci.yaml
当且仅当对目录及其子目录中此特定文件“ terragrunt.hcl
”的提交已更改时才会触发。 Is there a way to do this with gitlab's ci/cd tooling or would it be easier just to run a custom build script?有没有办法使用 gitlab 的 ci/cd 工具来做到这一点,或者只运行自定义构建脚本会更容易吗?
This one-liner bash below does what I want but the issue with that is that it still triggers the pipeline with any commits when I just want the build on that specific terragrunt.hcl
only.下面的这个单行 bash 可以满足我的要求,但问题是当我只想在特定的terragrunt.hcl
上构建时,它仍然会触发任何提交的管道。
for i in $(git diff-tree --no-commit-id --name-only -r $CI_COMMIT_SHA | xargs -n 1 dirname | uniq); do cd /scripts && ./run.sh /path/to/$i; done
I found this resource: GitLab CI/CD: Run jobs only when files in a specific directory have changed and still running some test on it but I would appreciate any additional help please!!!我找到了这个资源: GitLab CI/CD:仅在特定目录中的文件发生更改并且仍在对其进行一些测试时运行作业,但我将不胜感激任何额外的帮助!!! I am very close !!我很亲近!!
Since Gitlab version 11.4, the only
and except
keywords accept a parameter changes
that lets you define a job that only runs when one of the listed files has changed.自 Gitlab 版本 11.4 起, only
和except
关键字接受参数changes
,允许您定义仅在列出的文件之一更改时运行的作业。 So for your example, a job might look like this:因此,对于您的示例,工作可能如下所示:
compliance_job:
stage: compliance
only:
changes:
- terragrunt.hcl
script:
- ./compliance_check.sh
This means that the complaince_job
job will only be added to a pipeline if the file terragrunt.hcl
has changed.这意味着只有当文件terragrunt.hcl
发生更改时,才会将complaince_job
作业作业添加到管道中。
In Gitlab version 12.3, the rules
keyword was introduced that allows finer control of when a job is added to a pipeline or not.在 Gitlab 版本 12.3 中,引入了rules
关键字,可以更好地控制何时将作业添加到管道中。 It allows you to do things like:它允许您执行以下操作:
compliance_job:
stage: compliance
rules:
- if: $CI_COMMIT_MESSAGE ~= /compliance/ || $CI_COMMIT_REF_NAME == "main"
changes:
- terragrunt.hcl
when: always
script:
- ./compliance_check.sh
This means that the compliance_job
job will be added to the pipeline if the commit message contains the word compliance
or the branch or tag is main
, and the terragrunt.hcl
file changed.这意味着如果提交消息包含单词compliance
或者分支或标签是main
,并且terragrunt.hcl
文件发生更改,则compliance_job
作业将被添加到管道中。 If neither of the if
conditions is true, or if the terragrunt.hcl
file didn't change, the job won't run.如果两个if
条件都不为真,或者terragrunt.hcl
文件未更改,则作业将不会运行。
Here's another way to use the rules
keyword to check for changes but without checking other conditionals:这是使用rules
关键字检查更改但不检查其他条件的另一种方法:
compliance_job:
stage: compliance
rules:
- changes:
- terragrunt.hcl
- file1.txt
- file2.txt
- path/to/file3.txt
when: always
script:
- ./compliance_check.sh
In this example, the job will run if any of the files listed have changed in the latest commit, but we don't have to have an "if" along with it.在此示例中,如果列出的任何文件在最新提交中发生更改,则作业将运行,但我们不必附带“if”。
You can read about the only: changes
keyword here: https://docs.gitlab.com/ee/ci/yaml/#onlychangesexceptchanges您可以在此处阅读only: changes
关键字: https://docs.gitlab.com/ee/ci/yaml/#onlychangesexceptchanges
And the rules: if
and rules:changes
are here: https://docs.gitlab.com/ee/ci/yaml/#rules and here: https://docs.gitlab.com/ee/ci/yaml/#ruleschanges And the rules: if
and rules:changes
are here: https://docs.gitlab.com/ee/ci/yaml/#rules and here: https://docs.gitlab.com/ee/ci/yaml/#规则变更
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.