Home > database >  How do I embed YAML inside a YAML as text in bash script?
How do I embed YAML inside a YAML as text in bash script?

Time:03-05

I am assembling a YAML build pipeline using bash as follows.

      cat <<EOT >> ${BUILD_SOURCESDIRECTORY}/azure-pipelines.yml
  - template: templates/deploy-to-env-ssh.yml@infrastructure
    parameters:
      dockerHostEndpoint: ${DOCKER_HOST}
      jobName: ${BASENAME}
      stackName: ${STACK_NAME}
      composeFile: ${STACK_NAME}.yml
      schedule: ???
        $(cat schedule.yml)
      tz: ${TZ}
EOT

What I want is to store the following YAML into schedule as a string which I can reuse in a another part of the pipeline.

version: 1.4
jobs:
  DockerJob:
    cmd: docker ps
    time: "*"
    notifyOnSuccess:
      - type: stdout
        data:
          - stdout

But it seems it needs to be indented.

CodePudding user response:

You can use the pr utility:

cat <<EOF >> ${BUILD_SOURCESDIRECTORY}/azure-pipelines.yml
  - template: templates/deploy-to-env-ssh.yml@infrastructure
    parameters:
      dockerHostEndpoint: ${DOCKER_HOST}
      jobName: ${BASENAME}
      stackName: ${STACK_NAME}
      composeFile: ${STACK_NAME}.yml
      schedule: $(printf "\n" && pr -to 8 schedule.yml)
      tz: ${TZ}
EOT

I use printf "\n" because you'd need to place the $(…) at the first column if you want to write $(…) on a new line, since every line including the first one will be offset by the given number of spaces.

  • Related