2017-11-07 13 views
0

중복 된 부분이있는 gitlab-ci yaml 파일이 있습니다.YAML 병합 수준

test:client: 
    before_script: 
    - node -v 
    - yarn install 
    cache: 
    untracked: true 
    key: client 
    paths: 
     - node_modules/ 
    script: 
    - npm test 

build:client: 
    before_script: 
    - node -v 
    - yarn install 
    cache: 
    untracked: true 
    key: client 
    paths: 
     - node_modules/ 
    policy: pull 
    script: 
    - npm build 
나는이 두 부분의 맥락에서 효율적으로 재사용 할 공통의 ​​부분을 추출 할 수있는 경우, 병합 구문을 알고 싶습니다

.

.node_install_common: &node_install_common 
    before_script: 
    - node -v 
    - yarn install 
    cache: 
    untracked: true 
    key: client 
    paths: 
     - node_modules/ 

하지만 진짜 문제는 : 들여 쓰기하는 수준에서 나는 정책을 보장하기 위해 블록을 병합해야합니까 : 풀 캐시 섹션에 적용됩니다. 나는 그걸하려고 :

test:client: 
    <<: *node_install_common 
    script: 
    - npm test 

test:build: 
    <<: *node_install_common 
    policy: pull 
    script: 
    - npm build 

하지만 잘못된 yaml 오류가 발생합니다. 올바른 병합 동작을 얻기 위해 들여 쓰기하는 방법?

답변

1

병합 키는 YAML 사양의 일부가 아니므로 작동하지 않을 수 있습니다. 그것들은 또한 구식 YAML 1.1 버전을 위해 지정되었으며 현재 YAML 1.2 버전 용으로 업데이트되지 않았습니다. 우리는 향후 YAML 1.3에서 병합 키를 명시 적으로 제거하려는 의도가 있습니다.

즉 : 병합 구문이 없습니다. 병합 키 <<은 매핑의 일반 키처럼 배치해야합니다. 즉, 키는 다른 키와 동일한 들여 쓰기를 가져야합니다. 그래서이 유효 할 것 :

test:client: 
    <<: *node_install_common 
    script: 
    - npm test 

을이 아니지만 : 코드에 비해, 나는 test:clienttest:build 라인에 :을 추가

test:build: 
    <<: *node_install_common 
    policy: pull 
    script: 
    - npm build 

하는 것으로.

은 이제 병합들은 이미에 존재하지 않는 경우에 현재 매핑 에 참조 매핑의 모든 키 - 값 쌍을 배치하는 지정됩니다. 즉, 하위 트리에서 더 깊은 값을 바꿀 수는 없습니다 (병합은 하위 트리의 부분 대체를 지원하지 않습니다). 그러나 병합을 여러 번 사용할 수 있습니다.

.node_install_common: &node_install_common 
    before_script: 
    - node -v 
    - yarn install 
    cache: &cache_common 
    untracked: true 
    key: client 
    paths: 
     - node_modules/ 

test:client: 
    <<: *node_install_common 
    script: 
    - npm test 

test:build: 
    <<: *node_install_common 
    cache: # define an own cache mapping instead of letting merge place 
     # its version here (which could not be modified) 
    <<: *cache_common # load the common cache content 
    policy: pull  # ... and place your additional key-value pair 
    script: 
    - npm build 
+0

도움 주셔서 감사합니다. 나는 당신의 충고에 따라'cache_common'에서 값을 분리했다. – BlackHoleGalaxy