How to merge YAML arrays?

后端 未结 5 1682
旧时难觅i
旧时难觅i 2020-11-29 23:16

I would like to merge arrays in YAML, and load them via ruby -

some_stuff: &some_stuff
 - a
 - b
 - c

combined_stuff:
  <<: *some_stuff
  - d
  -         


        
5条回答
  •  情歌与酒
    2020-11-29 23:55

    If the aim is to run a sequence of shell commands, you may be able to achieve this as follows:

    # note: no dash before commands
    some_stuff: &some_stuff |-
        a
        b
        c
    
    combined_stuff:
      - *some_stuff
      - d
      - e
      - f
    

    This is equivalent to:

    some_stuff: "a\nb\nc"
    
    combined_stuff:
      - "a\nb\nc"
      - d
      - e
      - f
    

    I have been using this on my gitlab-ci.yml (to answer @rink.attendant.6 comment on the question).


    Working example that we use to support requirements.txt having private repos from gitlab:

    .pip_git: &pip_git
    - git config --global url."https://gitlab-ci-token:${CI_JOB_TOKEN}@gitlab.com".insteadOf "ssh://git@gitlab.com"
    - mkdir -p ~/.ssh
    - chmod 700 ~/.ssh
    - echo "$SSH_KNOWN_HOSTS" > ~/.ssh/known_hosts
    - chmod 644 ~/.ssh/known_hosts
    
    test:
        image: python:3.7.3
        stage: test
        script:
            - *pip_git
            - pip install -q -r requirements_test.txt
            - python -m unittest discover tests
    
    use the same `*pip_git` on e.g. build image...
    

    where requirements_test.txt contains e.g.

    -e git+ssh://git@gitlab.com/example/example.git@v0.2.2#egg=example

提交回复
热议问题