Gitlab CI/CD只在标签推送时运行流水线,且仅在特定文件更改时运行

3
我有一个单库存储库,里面有很多需要分别部署的内容。
现在我想知道是否可能仅在推送标签且某个目录中有更改时运行任务。
我尝试了以下方法:
.components:
  only:
    changes:
      - source/packages/components/**/*
  except:
    - branches

.components:
  only:
    refs:
      - tags
    changes:
      - source/packages/components/**/*


.components:
  rules:
    - if: $CI_COMMIT_TAG
    changes:
      - source/packages/components/**/*


.components:
  rules:
    - if: $CI_COMMIT_TAG
      changes:
        - source/packages/components/**/*
      when: always
    - when: never

这是我使用的 .gitlab-ci.yml 文件之一。除了一些细微差别外,它们都看起来相同。
.components:
  rules:
    - if: '$CI_COMMIT_TAG'
      changes:
        - source/packages/components/**/*

build_componets:
  stage: build
  extends: .components
  needs: []
  script:
    - echo "Compiling the components code..."

test_componets:
  stage: test
  extends: .components
  needs: ["build_componets"]
  script:
    - echo "Compiling the components code..."

deploy_componets:
  stage: deploy
  extends: .components
  needs: ["test_componets"]
  script:
    - echo "Compiling the components code..."

当前行为:每个流水线都会运行...而不考虑这些配置,但只有在我推送标签时才运行。

所有这些文件都被导入到项目根目录下的一个主文件中。

stages:
  - build
  - test
  - deploy

include:
  - source/packages/business-logic/.gitlab-ci.yml
  - source/packages/components/.gitlab-ci.yml
  - source/applications/native/.gitlab-ci.yml
  - source/services/user-service/.gitlab-ci.yml

合并后的YAML如下所示。

---
".business-logic":
  rules:
  - if: "$CI_COMMIT_TAG"
    changes:
    - source/packages/business-logic/**/*
    when: always
  - when: never
build_business_logic:
  rules:
  - if: "$CI_COMMIT_TAG"
    changes:
    - source/packages/business-logic/**/*
    when: always
  - when: never
  stage: build
  extends: ".business-logic"
  needs: []
  script:
  - yarn install
  - yarn workspace @zeou/business-logic build
test_business_logic:
  rules:
  - if: "$CI_COMMIT_TAG"
    changes:
    - source/packages/business-logic/**/*
    when: always
  - when: never
  stage: test
  extends: ".business-logic"
  needs:
  - build_business_logic
  script:
  - echo "Compiling the business-logics code..."
deploy_business_logic:
  rules:
  - if: "$CI_COMMIT_TAG"
    changes:
    - source/packages/business-logic/**/*
    when: always
  - when: never
  stage: deploy
  extends: ".business-logic"
  needs:
  - test_business_logic
  script:
  - echo "Compiling the business-logics code..."
".components":
  rules:
  - if: "$CI_COMMIT_TAG"
    changes:
    - source/packages/components/**/*
    when: always
  - when: never
build_components:
  rules:
  - if: "$CI_COMMIT_TAG"
    changes:
    - source/packages/components/**/*
    when: always
  - when: never
  stage: build
  extends: ".components"
  needs: []
  script:
  - echo "Compiling the components code..."
test_components:
  rules:
  - if: "$CI_COMMIT_TAG"
    changes:
    - source/packages/components/**/*
    when: always
  - when: never
  stage: test
  extends: ".components"
  needs:
  - build_components
  script:
  - echo "Compiling the components code..."
deploy_components:
  rules:
  - if: "$CI_COMMIT_TAG"
    changes:
    - source/packages/components/**/*
    when: always
  - when: never
  stage: deploy
  extends: ".components"
  script:
  - echo "Compiling the components code..."
stages:
- ".pre"
- build
- test
- deploy
- ".post"
before_script: 

有人能告诉我我做错了什么吗?

谢谢100次:D

2个回答

1
在这种情况下,使用rules:changes:子句与if:规则组合起来,以检查是否存在标签。
myjob:
  rules:
    - if: "$CI_COMMIT_TAG"
      changes:
        - source/services/user-service/**/*
   # ...

谢谢回答。我已经尝试过了。不幸的是,它没有成功。我已经将新的方法添加到问题中。 每次我推送标签时,无论更改如何,都会构建一切。 - Tjerk Haaye Henricus Dames
你必须像这里一样使用 when 关键字 https://dev59.com/3lIG5IYBdhLWcg3w_nTt#69551744 - farch
我已经尝试过了,但是如果我推送一个标签,其中只有另一个目录中的更改,管道仍然会运行:o 我也在问题中添加了这种方法。我做得对吗? - Tjerk Haaye Henricus Dames

1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接