我使用gitlab-ci
相当新,因此,我遇到了一个问题,因为我使用锚点/引用,以下命令失败ci-lint
:
image: docker:latest
services:
- docker:dind
variables:
DOCKER_DRIVER: overlay2
DOCKER_HOST: tcp://localhost:2375
.install_thing1: &install_thing1
- do things
- to install
- thing1
.install_thing2: &install_thing2
- do things to
- install thing2
.setup_thing1: &setup_things1
variables:
VAR: var
FOO: bar
script:
- all
- the
- things
before_script:
...
stages:
- deploy-test
- deploy-stage
- deploy-prod
test:
stage: deploy-test
variables:
RUN_ENV: "test"
...
only:
- tags
- branches
script:
- *install_thing1
- *install_thing2
- *setup_thing1
- other stuff
...
test:
stage: deploy-stage
variables:
RUN_ENV: "stage"
...
only:
- master
script:
- *install_thing1
- *install_thing2
- *setup_thing1
- other stuff
当我尝试lint the gitlab-ci.yml
时,我收到以下错误:
Status: syntax is incorrect
Error: jobs:test:script config should be a string or an array of strings
错误导致只需要一个script
片的数组,我相信我有。使用<<: *anchor
编译指示也会导致错误。
那么,如何才能完成我在这里尝试做的事情,而不必在每个块中重复代码?
你可以解决它,甚至让它更干,看看the Auto DevOps template Gitlab created。
它可以解决您的问题,甚至可以更好地改进您的CI文件,只需要像auto_devops
job这样的模板工作,将其包含在before_script中,然后您就可以在脚本块中组合并调用multiple functions。
锚只会给你有限的灵活性。
(这个概念使我有可能为20多个项目和一个集中的函数文件I wget
创建一个CI文件并加载到我的before_script
中。)