Enable dynamic changesetTemplates by adding templating & steps outputs
Created by: mrnugget
What?
This PR enables dynamic changesetTemplates
in campaign specs by doing two things:
- it adds templating support to the
title
,body
,branch
andcommit.message
fields inchangesetTemplate
. - it adds
outputs
tosteps
, which are values that are set after the step has been executed and that have access to template variables, including the current step's output.
This solves the problem of not being able to customize the changesetTemplate
per repository (only published
could be configured per repository).
Show me
With this PR, the following campaign spec works:
name: dynamic-changeset-template
description: Hello Outputs
on:
- repository: github.com/sourcegraph/src-cli
steps:
- run: echo "Hello there!" >> message.txt && cat message.txt
container: alpine:3
# NEW:
outputs:
friendlyMessage:
value: "${{ step.stdout }}"
# format: text is implicit
- run: cat .goreleaser.yml >&2
container: alpine:3
# NEW:
outputs:
goreleaserConfig:
value: "${{ step.stderr }}"
# we will parse the value here as YAML, meaning that we can access it as an object
format: yaml
goreleaserConfigExists:
# we can use the power of Go's text/template engine to dynamically produce complex values
value: "exists: ${{ gt (len step.stderr) 0 }}"
format: yaml
changesetTemplate:
# title allows templating
title: "The friendly message is: `${{ outputs.friendlyMessage }}`"
# body allows templating
body: |
This repository has a `gorelaserConfig`: ${{ outputs.goreleaserConfigExists.exists }}.
(Since these are just Go `text/templates` and `goreleaserConfig` was parsed as YAML we can access every field)
The `goreleaser.yml` defines the following `before.hooks`:
${{ range $index, $hook := outputs.goreleaserConfig.before.hooks }}
- `${{ $hook }}`
${{ end }}
These files were added by the steps:
```
${{ join steps.added_files " " }}
```
# branch allows templating
branch: thorsten/variables-${{ outputs.goreleaserConfig.dist }}
published: draft
commit:
# commit message allows templating
message: "The friendly message is: `${{ outputs.friendlyMessage }}`"
It created this pull request:
Meta
PR in sourcegraph/sourcegraph
that adds the schema: https://github.com/sourcegraph/sourcegraph/pull/17243
What's missing?
-
BIG FAT TODO: caching! we need to cache the outputs too now, not just the diff. -
Documentation. I want to add docs for this in https://github.com/sourcegraph/sourcegraph/pull/17243 and also better document templating support, since we now lean on it more heavily.PR is here: https://github.com/sourcegraph/sourcegraph/pull/17286
Possible follow-ups
-
Can be a follow-up PR: making transformChanges.group.branch
optional by makingtransform_changes.group
available as a template variable inchangesetTemplate.branch
.