0

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


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 above 'validate-ci:' line

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.

Venu Reddy
  • 39
  • 8

2 Answers2

1

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.

potong
  • 55,640
  • 6
  • 51
  • 83
1

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=="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).*$/'
Ed Morton
  • 188,023
  • 17
  • 78
  • 185