以 YAML 形式插入文本,从变量或 file.yml 到另一个 YAML 文件

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

我有一个 YAML 文件 ( 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).*$/'


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).*$/'

我想在上述文件中 'validate-ci:' 行

上方的位置插入以下文本或作业
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).*$/'

我正在尝试使用 sed,但缩进不准确。

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

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

如果有人能帮助我,我将不胜感激

如果用 python 或 bash 和 sed/awk 组合完成我很好。

这可能对你有用(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。这是内联的@KarlT 回答。

在每个 Unix 机器上使用任何 shell 中的任何 awk,无论您的输入文件包含什么字符:

$ awk 'NR==FNR{new=new sep [=10=]; sep=ORS; next} [=10=]=="validate-ci:"{print 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).*$/'