backstage

Can Backstage Templates publish new files to existing repos?


When using Backstage, it seems all the demos on template creation are centered around the concept of creating a new Git repo from scratch.

Can a template be created that will add new content (files) to an existing repo without trying to create a new repo or branch? I would prefer to push instead of pull request, but will accept pull request if that is the only option.

Here is my template so far...

apiVersion: scaffolder.backstage.io/v1beta3
kind: Template
metadata:
  name: addfile
  title: "Generate MongoDB Cluster YAML"
  description: "Asks user for cluster details and publishes a YAML configuration file to a target GitHub repository."
spec:
  owner: steve@john.net
  type: configuration
  parameters:
    - title: Cluster Configuration
      required:
        - clusterName
        - clusterSize
        - comments
      properties:
        clusterName:
          type: string
          title: Cluster Name
        clusterSize:
          type: string
          title: Cluster Size
          enum:
            - Small
            - Medium
            - Large
        comments:
          title: Request Comments
          type: string
  steps:
    - id: fetch-repo
      name: Fetch repo
      action: fetch:template
      input:
        values:
          cluster_name: ${{ parameters.clusterName }}
          provider_instance_size_name: ${{ parameters.clusterSize }}
        url: ./skeleton
        targetPath: ./new_cluster_requests/${{ parameters.clusterName }}

    - id: write-to-file
      name: Overwrite File Or Create New
      action: roadiehq:utils:fs:write
      input:
        path: ./new_cluster_requests/${{ parameters.clusterName }}.yaml
        content: |
          clusterName: ${{ parameters.clusterName }}
          clusterSize: ${{ parameters.clusterSize }}

    - id: publish-to-github
      name: Publish to GitHub
      action: github:repo:push
      input:
        repoUrl: "github.com?owner=bongo&repo=testrepo"
        defaultBranch: main
        sourcePath: "new_cluster_requests/"
        gitCommitMessage: ${{ parameters.comments }}
        force: true

I receive an error trying to publish:

Push rejected because it was not a simple fast-forward. Use "force: true" to override. {data={"reason":"not-fast-forward"}}

The github repo exists, as does the branch. I just want to add the new file. Any ideas?


Solution

  • I am facing a similar problem. The issue lies where the github:repo:push action uses a helper that initializes a git repo, https://github.com/backstage/backstage/blob/master/plugins/scaffolder-backend-module-github/src/actions/github.ts#L275, and uses this helper function here: https://github.com/backstage/backstage/blob/master/plugins/scaffolder-node/src/actions/gitHelpers.ts#L49-L52 My suggestion is to follow the GitHub issue https://github.com/backstage/backstage/issues/28749 to allow an action like: github:branch:push.