Skip to content

Enable dynamic changesetTemplates by adding templating & steps outputs

Warren Gifford requested to merge mrn/dynamic-changeset-template into main

Created by: mrnugget

What?

This PR enables dynamic changesetTemplates in campaign specs by doing two things:

  1. it adds templating support to the title, body, branch and commit.message fields in changesetTemplate.
  2. it adds outputs to steps, 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:

screenshot_2021-01-14_15 38 19@2x

Meta

PR in sourcegraph/sourcegraph that adds the schema: https://github.com/sourcegraph/sourcegraph/pull/17243

What's missing?

Possible follow-ups

  • Can be a follow-up PR: making transformChanges.group.branch optional by making transform_changes.group available as a template variable in changesetTemplate.branch.

Merge request reports

Loading