Once a template grows past a handful of jobs, keeping everything in one file gets unwieldy. ytt lets you split a
template across multiple files, then render them all together as if they were one.
The Files
template.yml — the entry point. It loads two library files and calls their functions to assemble the
pipeline:
#@ load("@ytt:data", "data")#@ load("jobs.lib.yml", "unit_tests", "build_rc_image", "deploy")#@ load("resources.lib.yml", "resources")jobs:-#@ unit_tests()-#@ build_rc_image()-#@ deploy("dev")-#@ deploy("prod")resources:#@ resources(data.values.branch_name, data.values.artifact_slug)#! TODO: temp fix to ensure usage of the latest mock resourceresource_types:-name:mocktype:registry-imagesource:repository:concourse/mock-resource
jobs.lib.yml — a library file exporting one function per job. deploy() takes an environment name as an
argument and branches its passed constraints depending on whether it's called for dev or prod:
Unlike the single-file example, this one is rendered by pointing ytt at the whole directory rather than naming
files individually, since template.yml loads its library files by relative path:
ytt-f./>rendered.yml
What Gets Rendered
The four function calls in template.yml expand into four concrete jobs, chained together by passed
constraints, alongside the four resources built by resources():
resources:-name:repoicon:gittype:mocksource:initial_version:repo-at-feature-1024create_files:branch.txt:feature-1024-name:image-rcicon:ocitype:mocksource:initial_version:ft1024-rccreate_files:oci_image:ft1024-rc-name:dev-envicon:wrenchtype:mock-name:prod-envicon:cloud-checktype:mockjobs:-name:unit-testsbuild_log_retention:builds:50plan:-get:repotrigger:true-task:run-unit-testsconfig:platform:linuximage_resource:type:mocksource:mirror_self:trueinputs:-name:reporun:path:shargs:--c-|echo running the unit tests...cat repo/branch.txtsleep 4echo tests passed!-name:build-imagebuild_log_retention:builds:50plan:-get:repotrigger:truepassed:[unit-tests]-task:build-imageconfig:platform:linuximage_resource:type:mocksource:mirror_self:trueinputs:-name:repooutputs:-name:imagerun:path:shargs:--c-|echo building the image...date +%Y-%m-%d > image/versionsleep 2echo image built!-put:image-rcparams:file:image/version-name:deploy-devplan:-in_parallel:-get:repopassed:[build-image]-get:image-rcpassed:[build-image]-put:dev-envparams:file:image-rc/oci_image-name:deploy-prodplan:-in_parallel:-get:repopassed:[deploy-dev]-get:image-rcpassed:[deploy-dev]-put:prod-envparams:file:image-rc/oci_imageresource_types:-name:mocktype:registry-imagesource:repository:concourse/mock-resource