Processing of config

The following steps could describe the processing of a YAML configuration file:

  1. Reading werf.yaml and extra templates from .werf directory.
  2. Executing Go templates.
  3. Saving dump into .werf.render.yaml (this file remains after the command execution and will be removed automatically with GC procedure).
  4. Splitting rendered YAML file into separate config sections (part of YAML stream separated by three hyphens, https://yaml.org/spec/1.2/spec.html#id2800132).
  5. Validating each config section:
    • Validating YAML syntax (you could read YAML reference here).
    • Validating werf syntax.

Go templates

Go templates are available within YAML configuration. The following functions are supported:

  • Built-in Go template functions and other language features. E.g. using common variable:

    {{ $base_image := "golang:1.11-alpine" }}
    
    project: my-project
    configVersion: 1
    ---
    
    image: gogomonia
    from: {{ $base_image }}
    ---
    image: saml-authenticator
    from: {{ $base_image }}
    
  • Sprig functions. E.g. using environment variable:

    project: my-project
    configVersion: 1
    ---
    
    {{ $_ := env "SPECIFIC_ENV_HERE" | set . "GitBranch" }}
    
    image: ~
    from: alpine
    git:
    - url: https://github.com/company/project1.git
      branch: {{ .GitBranch }}
      add: /
      to: /app/project1
    - url: https://github.com/company/project2.git
      branch: {{ .GitBranch }}
      add: /
      to: /app/project2
    
  • include function with define for reusing configs:

    project: my-project
    configVersion: 1
    ---
    
    image: app1
    from: alpine
    ansible:
      beforeInstall:
      {{- include "(component) ruby" . }}
    ---
    image: app2
    from: alpine
    ansible:
      beforeInstall:
      {{- include "(component) ruby" . }}
    
    {{- define "(component) ruby" }}
      - command: gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3
      - get_url:
          url: https://raw.githubusercontent.com/rvm/rvm/master/binscripts/rvm-installer
          dest: /tmp/rvm-installer
      - name: "Install rvm"
        command: bash -e /tmp/rvm-installer
      - name: "Install ruby 2.3.4"
        raw: bash -lec {{`{{ item | quote }}`}}
        with_items:
        - rvm install 2.3.4
        - rvm use --default 2.3.4
        - gem install bundler --no-ri --no-rdoc
        - rvm cleanup all
    {{- end }}
    
  • tpl function to evaluate strings (either content of environment variable or project file) as Go templates inside a template: example with project files.

  • .Files.Get and .Files.Glob functions to work with project files:

    .Files.Get

    project: my-project
    configVersion: 1
    ---
    
    image: app
    from: alpine
    ansible:
      setup:
      - name: "Setup /etc/nginx/nginx.conf"
        copy:
          content: |
    {{ .Files.Get ".werf/nginx.conf" | indent 8 }}
          dest: /etc/nginx/nginx.conf
    

    .Files.Glob

    The function supports shell pattern matching + **. Results can be merged with merge sprig function (e.g {{ $filesDict := merge (.Files.Glob "*/*.txt") (.Files.Glob "app/**/*.txt") }})

    project: my-project
    configVersion: 1
    ---
    
    image: app
    from: alpine
    ansible:
      install:
      - raw: mkdir /app
      setup:
    {{ range $path, $content := .Files.Glob ".werf/files/*" }}
      - name: "Setup /app/{{ base $path }}"
        copy:
          content: |
    {{ $content | indent 8 }}
          dest: /app/{{ base $path }}
    {{ end }}
    

    .Files.Get

    project: my-project
    configVersion: 1
    ---
    
    image: app
    from: alpine
    shell:
      setup:
      - |
        head -c -1 <<'EOF' > /etc/nginx/nginx.conf
    {{ .Files.Get ".werf/nginx.conf" | indent 4 }}
        EOF
    

    .Files.Glob

    The function supports shell pattern matching + **. Results can be merged with merge sprig function (e.g {{ $filesDict := merge (.Files.Glob "*/*.txt") (.Files.Glob "app/**/*.txt") }})

    project: my-project
    configVersion: 1
    ---
    
    image: app
    from: alpine
    shell:
      install: mkdir /app
      setup:
    {{ range $path, $content := .Files.Glob ".werf/files/*" }}
      - |
        head -c -1 <<EOF > /app/{{ base $path }}
    {{ $content | indent 4 }}
        EOF
    {{ end }}