Multiline

Example Go template with multiline strings.

Overview

From YAML Spec Scalars:

  • | - In Scalar literals, newlines are preserved
# Below YAML was taken from spec literal example
--- |
  \//||\/||
  // ||  ||__  

Sample

Let’s take a look at using a conditional with a variable in a template:

metadata:
  template: true

steps:

  {{ .test }}

  - name: build
    commands:
      - go build
    environment:
      CGO_ENABLED: '0'
      GOOS: linux
    image: golang:latest
    pull: always
    ruleset:
      event: [ push, pull_request ]

The caller of this template could look like:

version: "1"
templates:
  - name: sample
    source: github.com/<org>/<repo>/path/to/file/<template>.yml
    type: github

steps:
  - name: golang
    template:  
      name: sample
      vars:
        test: |
          - name: test
              commands:
                - go test ./...
              image: golang:latest
              pull: always
              ruleset:
               event: [ push, pull_request ]          

Which means the compiled pipeline for execution on a worker is:

version: "1"
steps:
  - name: sample_test
    commands:
      - go test ./...
    image: golang:latest
    pull: always
    ruleset:
      event: [ push, pull_request ]

  - name: sample_build
    commands:
      - go build
    image: golang:latest
    pull: always
    ruleset:
      event: [ push, pull_request ]