Home > Mobile >  Insert text in YAML form, from a variable or file.yml to another YAML file
Insert text in YAML form, from a variable or file.yml to another YAML file

Time:03-11

I have a YAML file ( GitLab-ci.yml )

deploy-perf:
  extends: .deploy-np
  environment:
    name: perf
  variables:
    APP_NAME: $APP_NAME_PERF
    PCF_MF_FILE: 'manifest.perf.yml'
  rules:
    - if: '$CI_COMMIT_BRANCH =~ /^([fF]eature|[rR]elease).*$/'

[ INSERT HERE ]

validate-ci:
  extends: .healthcheck-v1
  variables:
    HEALTH_CHECK_URL: "https://[URL]/index.html"
  rules:
    - if: '$CI_COMMIT_BRANCH =~ /^([fF]eature).*$/'

validate-g1:
  extends: .healthcheck-v1
  variables:
    HEALTH_CHECK_URL: "https://[URL]/index.html"
  rules:
    - if: '$CI_COMMIT_BRANCH =~ /^([fF]eature).*$/'

I want to insert the below text or job in the above file at the position [ INSERT HERE ]

deploy-PROD:
  extends: .deploy-PROD
  environment:
    name: prod
  variables:
    APP_NAME: $APP_NAME_PROD
    PCF_MF_FILE: 'manifest.perf.yml'
  rules:
    - if: '$CI_COMMIT_BRANCH =~ /^([fF]eature|[rR]elease).*$/'

I am trying with sed but the indentation is not coming up accurately.

 sed -re '/validate-ci,*/i'"`echo $MYVAR`" cifile.yml

or

 sed -re '/validate-ci,*/i'"`cat prodjob.yml`" cifile.yml

If somebody could help me with this is much appreciated

I am fine if accomplished with python or bash and sed/awk combinations.

CodePudding user response:

You can use e to execute the cat command from within (gnu) sed

sed '/validate-ci:/e cat prodjob.yml; echo' cifile.yml

; echo is to add a blank line which you may also want.

CodePudding user response:

This might work for you (GNU sed & bash):

cat <<\! | sed '/validate-ci:/e cat /dev/stdin;echo' file
deploy-PROD:
  extends: .deploy-PROD
  environment:
    name: prod
  variables:
    APP_NAME: $APP_NAME_PROD
    PCF_MF_FILE: 'manifest.perf.yml'
  rules:
    - if: '$CI_COMMIT_BRANCH =~ /^([fF]eature|[rR]elease).*$/'  
!

N.B. This is @KarlT answer inlined.

CodePudding user response:

Using any awk in any shell on every Unix box no matter what characters your input files contain:

$ awk 'NR==FNR{new=new sep $0; sep=ORS; next} $0=="[ INSERT HERE ]"{$0=new} 1' file.yml GitLab-ci.yml
deploy-perf:
  extends: .deploy-np
  environment:
    name: perf
  variables:
    APP_NAME: $APP_NAME_PERF
    PCF_MF_FILE: 'manifest.perf.yml'
  rules:
    - if: '$CI_COMMIT_BRANCH =~ /^([fF]eature|[rR]elease).*$/'

deploy-PROD:
  extends: .deploy-PROD
  environment:
    name: prod
  variables:
    APP_NAME: $APP_NAME_PROD
    PCF_MF_FILE: 'manifest.perf.yml'
  rules:
    - if: '$CI_COMMIT_BRANCH =~ /^([fF]eature|[rR]elease).*$/'

validate-ci:
  extends: .healthcheck-v1
  variables:
    HEALTH_CHECK_URL: "https://[URL]/index.html"
  rules:
    - if: '$CI_COMMIT_BRANCH =~ /^([fF]eature).*$/'

validate-g1:
  extends: .healthcheck-v1
  variables:
    HEALTH_CHECK_URL: "https://[URL]/index.html"
  rules:
    - if: '$CI_COMMIT_BRANCH =~ /^([fF]eature).*$/'
  • Related