Last active
October 27, 2024 19:27
-
-
Save gbzarelli/f7da511d6a937cadc81720c7f68fc873 to your computer and use it in GitHub Desktop.
Backstage – Transformando e Reutilizando variáveis em seu template com uma action customizada #helpdev-blog
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// The action | |
// path: packages/backend/src/plugins/scaffolder/actions | |
import { CatalogApi } from '@backstage/catalog-client'; | |
import { parseEntityRef } from '@backstage/catalog-model'; | |
import { | |
createTemplateAction, | |
} from '@backstage/plugin-scaffolder-backend'; | |
export const recoveryVariables = (catalogClient: CatalogApi) => { | |
const catalog = catalogClient; | |
// create your inputs | |
return createTemplateAction<{ name: string; squad: string; type: string }>({ | |
id: 'helpdev:template:variables', | |
schema: { | |
input: { | |
required: ['squad', 'name', 'type'], | |
type: 'object', | |
properties: { | |
name: { | |
type: 'string', | |
title: 'Project Name', | |
}, | |
squad: { | |
type: 'string', | |
title: 'Squad Name', | |
}, | |
type: { | |
type: 'string', | |
title: 'Project Type', | |
}, | |
}, | |
}, | |
}, | |
async handler(ctx) { | |
// find owners (squad/tribe/vertical) | |
const squadName = ctx.input.squad; | |
const squadRef = parseEntityRef({kind: "Group", namespace: "default", name: squadName}) | |
const squadEntity = await catalog.getEntityByRef(squadRef) | |
const tribeInSquad = squadEntity?.relations?.find(ref=>{ | |
return ref?.targetRef.startsWith('group:default/tribe-'); | |
}) | |
const tribeEntity = await catalog.getEntityByRef(tribeInSquad?.targetRef || ''); | |
const verticalInTribe = tribeEntity?.relations?.find(ref=>{ | |
return ref?.targetRef.startsWith('group:default/vertical-'); | |
}) | |
const verticalEntity = await catalog.getEntityByRef(verticalInTribe?.targetRef || '') | |
// make owners | |
const tribeName = tribeEntity?.metadata.name || "undefined" | |
const verticalName = verticalEntity?.metadata.name || "undefined" | |
// make project variables | |
const packageName = ctx.input.name.trim().replace(" ","").toLowerCase(); | |
const projectType = ctx.input.type.toLowerCase(); | |
const artifactId = ctx.input.name.trim().replace(" ","-").toLowerCase()+"-"+projectType; | |
//write in output | |
ctx.output('squad', squadName) | |
ctx.output('tribe', tribeName) | |
ctx.output('vertical', verticalName) | |
ctx.output('artifactId', artifactId) | |
ctx.output('packageName', packageName) | |
}, | |
}); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Include Action | |
// path: packages/backend/src/plugins/ | |
import { CatalogClient } from '@backstage/catalog-client'; | |
import { createRouter } from '@backstage/plugin-scaffolder-backend'; | |
import { Router } from 'express'; | |
import type { PluginEnvironment } from '../types'; | |
import { createBuiltinActions } from '@backstage/plugin-scaffolder-backend'; | |
import { ScmIntegrations } from '@backstage/integration'; | |
//Here include your custom action: | |
import { recoveryVariables } from './scaffolder/actions/template'; | |
export default async function createPlugin( | |
env: PluginEnvironment, | |
): Promise<Router> { | |
const catalogClient = new CatalogClient({ | |
discoveryApi: env.discovery, | |
}); | |
const integrations = ScmIntegrations.fromConfig(env.config); | |
// Need add the builtin actions in this case | |
const builtInActions = createBuiltinActions({ | |
integrations: integrations, | |
catalogClient: catalogClient, | |
config: env.config, | |
reader: env.reader, | |
}); | |
// add all actions here: | |
const actions = [...builtInActions, recoveryVariables(catalogClient)]; | |
return await createRouter({ | |
actions: actions, | |
logger: env.logger, | |
config: env.config, | |
database: env.database, | |
reader: env.reader, | |
catalogClient: catalogClient, | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[...] | |
steps: | |
- id: data | |
name: Recovery Infos | |
action: helpdev:template:variables | |
input: | |
squad: ${{ parameters.squad }} | |
name: ${{ parameters.name }} | |
type: ${{ parameters.type }} | |
- id: fetchCicdTemplate | |
name: Fetch Template | |
action: fetch:template | |
input: | |
url: ./skeleton | |
values: | |
name: ${{ parameters.name }} | |
description: ${{ parameters.description }} | |
artifactId: ${{ steps.data.output.artifactId }} | |
packageName: ${{ steps.data.output.packageName }} | |
squadName: ${{ steps.data.output.squad }} | |
tribeName: ${{ steps.data.output.tribe }} | |
verticalName: ${{ steps.data.output.vertical }} | |
[...] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment