如何在.gitlab-ci.yml中使用模板中的脚本

发布于 2025-02-11 12:22:47 字数 337 浏览 2 评论 0原文

我使用脚本为gitlab-ci创建模板

.test-script: &test
    - echo "hello"

如何在.gitlab-ci.yml中使用此脚本?我尝试过这样的

include: '/templates/test-template.yml'

example-stage:
  script:
    - *test

错误发生“此gitlab ci配置无效:未知别名:测试。” ,因为在合并的yaml中没有此脚本的别名。

Gitlab版本:GitLab社区版14.6.0

I create template for gitlab-ci with script

.test-script: &test
    - echo "hello"

How to use this script in .gitlab-ci.yml? I tried like this

include: '/templates/test-template.yml'

example-stage:
  script:
    - *test

Error occurs "This GitLab CI configuration is invalid: Unknown alias: test." because there is no alias for this script in the merged YAML.

GitLab version: GitLab Community Edition 14.6.0

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

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

发布评论

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

评论(2

朱染 2025-02-18 12:22:47

为什么不使用GitLab的扩展或参考关键字?

---
include:
  - local: '/templates/test-template.yml'

example-stage:
  script:
    - !reference [.test-script, script]

或者

---
include:
  - local: '/templates/test-template.yml'

example-stage:
  extends: .test-script

Why not use GitLab's extends or reference keywords?

---
include:
  - local: '/templates/test-template.yml'

example-stage:
  script:
    - !reference [.test-script, script]

or

---
include:
  - local: '/templates/test-template.yml'

example-stage:
  extends: .test-script
等风来 2025-02-18 12:22:47

也许您可以尝试此语法:

.test-script: &test-script
   script:
     - echo "hello"

对于您的示例阶段作业:

example-stage:
  <<: *test-script

Maybe you could try this syntax :

.test-script: &test-script
   script:
     - echo "hello"

For your example-stage job :

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