在gitlab-ci.yml管道中,我只能正确使用关键字?

发布于 2025-01-31 05:29:44 字数 2360 浏览 2 评论 0原文

这就是我的项目的结构看起来像

├── main_project
│   ├── service1
│   │   ├── [...]
│   ├── service2
│   │   ├── [...]
│   ├── docs
│   │   ├── [...]
│   ├── .gitlab-ci.yml
    └── Makefile

我的.gitlab.ci-yml,

 [...]
 service1_build:
  image: image
  stage: build
  script:
    - #doing something
  only:
    changes:
      - /service1/**/*
      - /.gitlab-ci.yml
      - /Makefile
  except:
    changes:
      - /docs/**/*

 service2_build:
  image: image
  stage: build
  script:
    - #doing something
  only:
    changes:
      - /service2/**/*
      - /.gitlab-ci.yml
      - /Makefile
  except:
    changes:
      - /docs/**/*

 test:
  image: image
  stage: test
  needs:
    - service1_build
    - service2_build
  script:
    - #doing something
  except:
    changes:
      - /docs/**/*

 service1_docker:
  image: image
  stage: docker
  needs:
    - test
  script:
    - #doing something
  only:
    refs:
      - master
    changes:
      - /service1/**/*
      - /.gitlab-ci.yml
      - /Makefile
  except:
    changes:
      - /docs/**/*

 service2_docker:
  image: image
  stage: docker
  needs:
    - test
  script:
    - #doing something
  only:
    refs:
      - master
    changes:
      - /service2/**/*
      - /.gitlab-ci.yml
      - /Makefile
  except:
    changes:
      - /docs/**/*
 [...]

但我尝试过,但它无法正常工作。例如,如果我更改docs文件夹中的文件,则管道将执行test作业。

这些是我要应用于每个作业的规则:

  • service1_build :在所有分支上运行,当service> service> service1文件夹中有任何修改时,或者如果makefile.gitlab-ci.yml已更改。但是,在修改了docs文件夹中的文件时,它不应运行。
  • service2_build :在所有分支上运行,当service2文件夹中进行任何修改时已更改。但是,在修改了docs文件夹中的文件时,它不应运行。
  • test :每次都在所有分支上运行,除非docs中的文件文件夹已修改。
  • service1_docker :仅在Master上运行时,当service1文件夹中有任何修改时,或者如果makefile或<代码> .gitlab-ci.yml 已更改。但是,在修改了docs文件夹中的文件时,它不应运行。
  • service2_docker :仅在Master上运行时,当service2文件夹中有任何修改时> .gitlab-ci.yml 已更改。但是,当文档中的文件文件夹已修改时,它不应运行。

这是这样适用的吗?还是我应该如何处理?当我提供路径时,它总是从gitlab-ci.yml的文件夹中查看它?

This is how my project's structure looks like

├── main_project
│   ├── service1
│   │   ├── [...]
│   ├── service2
│   │   ├── [...]
│   ├── docs
│   │   ├── [...]
│   ├── .gitlab-ci.yml
    └── Makefile

My .gitlab.ci-yml

 [...]
 service1_build:
  image: image
  stage: build
  script:
    - #doing something
  only:
    changes:
      - /service1/**/*
      - /.gitlab-ci.yml
      - /Makefile
  except:
    changes:
      - /docs/**/*

 service2_build:
  image: image
  stage: build
  script:
    - #doing something
  only:
    changes:
      - /service2/**/*
      - /.gitlab-ci.yml
      - /Makefile
  except:
    changes:
      - /docs/**/*

 test:
  image: image
  stage: test
  needs:
    - service1_build
    - service2_build
  script:
    - #doing something
  except:
    changes:
      - /docs/**/*

 service1_docker:
  image: image
  stage: docker
  needs:
    - test
  script:
    - #doing something
  only:
    refs:
      - master
    changes:
      - /service1/**/*
      - /.gitlab-ci.yml
      - /Makefile
  except:
    changes:
      - /docs/**/*

 service2_docker:
  image: image
  stage: docker
  needs:
    - test
  script:
    - #doing something
  only:
    refs:
      - master
    changes:
      - /service2/**/*
      - /.gitlab-ci.yml
      - /Makefile
  except:
    changes:
      - /docs/**/*
 [...]

I tried this, but it is not working properly. For example, if I change a file in docs folder, the test job will be executed by the pipeline.

These are the rules that I want to be applied to each job:

  • service1_build: run on all branches, when there is any modification within the service1 folder or if Makefile or .gitlab-ci.yml has been changed. But it should not run when files in docs folder have been modified.
  • service2_build: run on all branches, when there is any modification within the service2 folder or if Makefile or .gitlab-ci.yml has been changed. But it should not run when files in docs folder have been modified.
  • test: run on all branches every time, except when files in docs folder have been modified.
  • service1_docker: run only on master, when there is any modification within the service1 folder or if Makefile or .gitlab-ci.yml has been changed. But it should not run when files in docs folder have been modified.
  • service2_docker: run only on master, when there is any modification within the service2 folder or if Makefile or .gitlab-ci.yml has been changed. But it should not run when files in docs folder have been modified.

Is this applicable like this? Or how should I handle this? When I am giving the path, it always looking it from the gitlab-ci.yml's folder?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

带上头具痛哭 2025-02-07 05:29:44

遵循项目结构,您应该尝试更改唯一的/除了这样的路径以外:

only:
    changes:
      - main_project/service1/**/*
      - .gitlab-ci.yml
      - Makefile
  except:
    changes:
      - main_project/docs/**/*

这包括main_project目录。

您还应该为您的测试工作添加一些可选的需求,因为如果Service1上只有更新,测试作业将转移到Service2作业并失败:

test:
  stage: test
  needs:
    - job: service1_build
      optional: true
    - job: service2_build
      optional: true

我对这些新修复程序进行了一些测试,您的规则正常工作: https://gitlab.com/sandbox_fm/ci-rules

您还应该考虑从/仅/除了规则因为:

唯一的,除非没有积极发展。规则是
首选关键字可以控制何时将作业添加到管道中。

Following your project structure, you should try to change your only/except paths like this :

only:
    changes:
      - main_project/service1/**/*
      - .gitlab-ci.yml
      - Makefile
  except:
    changes:
      - main_project/docs/**/*

This include the main_project directory.

You should also add some optional needs to your test job, because if there is only updates on service1, the test job will look to the service2 job and fail :

test:
  stage: test
  needs:
    - job: service1_build
      optional: true
    - job: service2_build
      optional: true

I made some tests with these new fix and your rules are working properly : https://gitlab.com/sandbox_fm/ci-rules.

You also should consider moving from only/except to rules because :

only and except are not being actively developed. rules is the
preferred keyword to control when to add jobs to pipelines.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文