TechDocs How-To guides
This documentation is written for the new frontend system, which is the default in new Backstage apps. If your Backstage app still uses the old frontend system, read the old frontend system version of this guide instead.
How to migrate from TechDocs Basic to Recommended deployment approach?
The main difference between TechDocs Basic and Recommended deployment approach is where the docs are generated and stored. In Basic or the out-of-the-box setup, docs are generated and stored at the server running your Backstage instance. But the recommended setup is to generate docs on CI/CD and store the generated sites to an external storage (e.g. AWS S3 or GCS). TechDocs in your Backstage instance should turn into read-only mode. Read more details and the benefits in the TechDocs Architecture.
Here are the steps needed to switch from the Basic to Recommended setup -
1. Prepare a cloud storage
Choose a cloud storage provider like AWS, Google Cloud or Microsoft Azure. Follow the detailed instructions for using cloud storage in TechDocs.
2. Publish to storage from CI/CD
Start publishing your TechDocs sites from the CI/CD workflow of each repository containing the source markdown files. Read the detailed instructions for configuring CI/CD.
3. Switch TechDocs to read-only mode
In your Backstage instance's app-config.yaml, set techdocs.builder from
'local' to 'external'. By doing this, TechDocs will not try to generate
docs. Look at TechDocs configuration for reference.
How to understand techdocs-ref annotation values
If TechDocs is configured to generate docs, it will first download source files
based on the value of the backstage.io/techdocs-ref annotation defined in the
Entity's catalog-info.yaml file. This is also called the
Prepare step.
We strongly recommend that the backstage.io/techdocs-ref annotation in each
documented catalog entity's catalog-info.yaml be set to dir:. in almost all
situations. This is because TechDocs is aligned with the "docs like code"
philosophy, whereby documentation should be authored and managed alongside the
source code of the underlying software itself.
When you see dir:., you can translate it to mean:
- That the documentation source code lives in the same location as the
catalog-info.yamlfile. - That, in particular, the
mkdocs.ymlfile is a sibling ofcatalog-info.yaml(meaning, it is in the same directory) - And that all of the source content of the documentation would be available if one were to download the directory containing those two files (as well as all sub-directories).
The directory tree of the entity would look something like this:
├── catalog-info.yaml
├── mkdocs.yml
└── docs
└── index.md
If, for example, you wanted to keep a lean root directory, you could place your
mkdocs.yml file in a subdirectory and update the backstage.io/techdocs-ref
annotation value accordingly, e.g. to dir:./sub-folder:
├── catalog-info.yaml
└── sub-folder
├── mkdocs.yml
└── docs
└── index.md
In rare situations where your TechDocs source content is managed and stored in a
location completely separate from your catalog-info.yaml, you can instead
specify a URL location reference, the exact value of which will vary based on
the source code hosting provider. Notice that instead of the dir: prefix, the
url: prefix is used instead. For example:
- GitHub:
url:https://githubhost.com/org/repo/tree/<branch_name> - GitLab:
url:https://gitlabhost.com/org/repo/tree/<branch_name> - Bitbucket:
url:https://bitbuckethost.com/project/repo/src/<branch_name> - Azure:
url:https://azurehost.com/organization/project/_git/repository
Note, just as it's possible to specify a subdirectory with the dir: prefix,
you can also provide a path to a non-root directory inside the repository which
contains the mkdocs.yml file and docs/ directory. It is important that it is
suffixed with a '/' in order for relative path resolution to work consistently.
e.g.
url:https://github.com/backstage/backstage/tree/master/plugins/techdocs-backend/examples/documented-component/
Why is URL Reader faster than a git clone?
URL Reader uses the source code hosting provider to download a zip or tarball of the repository. The archive does not have any git history attached to it. Also it is a compressed file. Hence the file size is significantly smaller than how much data git clone has to transfer.
How to customize the TechDocs home page?
TechDocs uses a composability pattern similar to the Search and Catalog plugins in Backstage. The default TechDocs home page provides a table experience similar to the one provided by the Catalog plugin. TechDocs also comes with an alternative grid based layout and panel layout.
Customization of the TechDocs home page in the new frontend system is done by
overriding the page:techdocs extension. The TechDocs home page is a standard
page extension created using PageBlueprint, which means you can override it
just like any other page extension.
The simplest approach is to use the plugin.withOverrides method to provide a
replacement page extension. Since the TechDocs page extension has the ID
page:techdocs, you can override it by creating a new page extension under the
techdocs plugin namespace:
import { PageBlueprint } from '@backstage/frontend-plugin-api';
import techdocsPlugin from '@backstage/plugin-techdocs/alpha';
export default techdocsPlugin.withOverrides({
extensions: [
PageBlueprint.make({
params: {
path: '/docs',
routeRef: techdocsPlugin.routes.root,
loader: async () => {
const { CustomTechDocsHome } = await import('./CustomTechDocsHome');
return <CustomTechDocsHome />;
},
},
}),
],
});
Then install the overridden plugin in your app:
import { createApp } from '@backstage/frontend-defaults';
import techdocsPlugin from './techdocs/TechDocsHomePage';
const app = createApp({
features: [techdocsPlugin],
});
export default app.createRoot();
You can also use the .override(...) method on the original extension if you
want to customize the existing page without fully replacing it. For more details
on extension overrides and the different override patterns available, see the
extension overrides documentation.
How to customize the TechDocs reader page?
The TechDocs reader page can be configured through app-config.yaml. For
example, you can disable the in-context search or the header:
app:
extensions:
- page:techdocs/reader:
config:
withoutSearch: true
app:
extensions:
- page:techdocs/reader:
config:
withoutHeader: true
For more advanced customization of the reader page, you can override the page extension. See the extension overrides documentation for details.
How to migrate from TechDocs Alpha to Beta
This guide only applies to the "recommended" TechDocs deployment method (where an external storage provider and external CI/CD is used). If you use the "basic" or "out-of-the-box" setup, you can stop here! No action needed.
For the purposes of this guide, TechDocs Beta version is defined as:
- TechDocs Plugin: At least
v0.11.0 - TechDocs Backend Plugin: At least
v0.10.0 - TechDocs CLI: At least
v0.7.0
The beta version of TechDocs made a breaking change to the way TechDocs content
was accessed and stored, allowing pages to be accessed with case-insensitive
entity triplet paths (e.g. /docs/namespace/kind/name whereas in prior
versions, they could only be accessed at /docs/namespace/Kind/name). In order
to enable this change, documentation has to be stored in an external storage
provider using an object key whose entity triplet is lower-cased.
New installations of TechDocs since the beta version will work fine with no action, but for those who were running TechDocs prior to this version, a migration will need to be performed so that all existing content in your storage bucket matches this lower-case entity triplet expectation.
-
Ensure you have the right permissions on your storage provider: In order to migrate files in your storage provider, the
techdocs-clineeds to be able to read/copy/rename/move/delete files. The exact instructions vary by storage provider, but check the using cloud storage page for details. -
Run a non-destructive migration of files: Ensure you have the latest version of
techdocs-cliinstalled. Then run the following command, using the details relevant for your provider / configuration. This will copy all files from, e.g.namespace/Kind/name/index.htmltonamespace/kind/name/index.html, without removing the original files.
techdocs-cli migrate --publisher-type <awsS3|googleGcs|azureBlobStorage> --storage-name <bucket/container name> --verbose
-
Deploy the updated versions of the TechDocs plugins: Once the migration above has been run, you can deploy the beta versions of the TechDocs backend and frontend plugins to your Backstage instance.
-
Verify that your TechDocs sites are still loading/accessible: Try accessing a TechDocs site using different entity-triplet case variants, e.g.
/docs/namespace/KIND/nameor/docs/namespace/kind/name. Your TechDocs site should load regardless of the URL path casing you use. -
Clean up the old objects from storage: Once you've verified that your TechDocs site is accessible, you can clean up your storage bucket by re-running the
migratecommand on the TechDocs CLI, but with an additionalremoveOriginalflag passed:
techdocs-cli migrate --publisher-type <awsS3|googleGcs|azureBlobStorage> --storage-name <bucket/container name> --removeOriginal --verbose
- Update your CI/CD pipelines to use the beta version of the TechDocs CLI: Finally, you can update all of your CI/CD pipelines to use at least v0.x.y of the TechDocs CLI, ensuring that all sites are published to the new, lower-cased entity triplet paths going forward.
If you encounter problems running this migration, please report the issue. You can temporarily revert to pre-beta storage expectations with a configuration change:
techdocs:
legacyUseCaseSensitiveTripletPaths: true
How to implement your own TechDocs APIs
The TechDocs plugin provides implementations of two primary APIs by default: the TechDocsStorageApi, which is responsible for talking to TechDocs storage to fetch files to render, and TechDocsApi, which is responsible for talking to techdocs-backend.
There may be occasions where you need to implement these two APIs yourself, to customize them to your own needs. The purpose of this guide is to walk you through how to do that in two steps.
- Implement the
TechDocsStorageApiandTechDocsApiinterfaces according to your needs.
export class TechDocsCustomStorageApi implements TechDocsStorageApi {
// your implementation
}
export class TechDocsCustomApiClient implements TechDocsApi {
// your implementation
}
- Override the default API extensions by creating custom API extensions using
createApiExtensionfrom@backstage/frontend-plugin-api, and install them in your app. See the Utility APIs documentation for details on how to create and install custom API extensions.
How to add the documentation setup to your software templates
Software Templates in Backstage is a tool that can help your users to create new components out of already configured templates. It comes with a set of default templates to use, but you can also add your own templates.
If you have your own templates set up, we highly recommend that you include the required setup for TechDocs in those templates. When creating a new component, your users will then get a TechDocs site up and running automatically, ready for them to start writing technical documentation.
The purpose of this how-to guide is to walk you through how to add the required configuration and some default markdown files to your new template. You can use the react-ssr-template as a reference when walking through the steps.
Prerequisites:
- An existing software template including a
template.yamltogether with a skeleton folder including at least acatalog-info.yaml.
- Update your component's entity description by adding the following lines to
the
catalog-info.yamlin your skeleton folder.
annotations:
backstage.io/techdocs-ref: dir:.
The
backstage.io/techdocs-ref annotation
is used by TechDocs to download the documentation source files for generating an
entity's TechDocs site.
- Create an
mkdocs.ymlfile in the root of your skeleton folder with the following content:
site_name: ${{values.component_id}}
site_description: ${{values.description}}
nav:
- Introduction: index.md
plugins:
- techdocs-core
- Create a
/docsfolder in the skeleton folder with at least anindex.mdfile in it.
The docs/index.md can for example have the following content:
# ${{ values.component_id }}
${{ values.description }}
## Getting started
Start writing your documentation by adding more markdown (.md) files to this
folder (/docs) or replace the content in this file.
The values of site_name, component_id and site_description depends
on how you have configured your template.yaml.
Done! You now have support for TechDocs in your own software template!
Prevent download of Google fonts
If your Backstage instance does not have internet access, the generation will fail. TechDocs tries to download the Roboto font from Google. You can disable it by adding the following lines to mkdocs.yaml:
theme:
name: material
font: false
The addition name: material is necessary. Otherwise it will not work
How to enable iframes in TechDocs
TechDocs uses the DOMPurify library to sanitize HTML and prevent XSS attacks.
It's possible to allow some iframes based on a list of allowed hosts. To do
this, add the allowed hosts in the techdocs.sanitizer.allowedIframeHosts
configuration of your app-config.yaml.
For example:
techdocs:
sanitizer:
allowedIframeHosts:
- drive.google.com
This way, all iframes where the host in the src attribute is in the
sanitizer.allowedIframeHosts list will be displayed.
How to enable custom elements in TechDocs
TechDocs uses the DOMPurify library to sanitize HTML and prevent XSS attacks.
It's possible to allow custom elements based on a list of allowed patterns. To do
this, add the allowed elements and attributes in the techdocs.sanitizer.allowedCustomElementTagNameRegExp
and allowedCustomElementAttributeNameRegExp configuration of your app-config.yaml.
For example:
techdocs:
sanitizer:
allowedCustomElementTagNameRegExp: '^backstage-',
allowedCustomElementAttributeNameRegExp: 'attribute1|attribute2',
This way, custom element like <backstage-element attribute1="value"></backstage-element> will be allowed in the result HTML.
How to allow additional URI protocols in TechDocs
TechDocs uses the DOMPurify library to sanitize HTML and prevent XSS attacks.
It's possible to allow additional URI protocols based on a list of protocols. To do
this, add the allowed protocols in the techdocs.sanitizer.additionalAllowedURIProtocols
and additionalAllowedURIProtocols configuration of your app-config.yaml.
For example:
techdocs:
sanitizer:
additionalAllowedURIProtocols: ["vscode"],
This way, links like <a href="vscode://settings/">VSCode Settings<a> will be allowed in the result HTML
How to render PlantUML diagram in TechDocs
PlantUML allows you to create diagrams from plain text language. Each diagram description begins with the keyword - (@startXYZ and @endXYZ, depending on the kind of diagram). For UML Diagrams, Keywords @startuml & @enduml should be used. Further details for all types of diagrams can be found at PlantUML Language Reference Guide.
UML Diagram Details:-
Embedded PlantUML Diagram Example
Here, the markdown file itself contains the diagram description.
```plantuml
@startuml
title Login Sequence
ComponentA->ComponentB: Login Request
note right of ComponentB: ComponentB logs message
ComponentB->ComponentA: Login Response
@enduml
```
Referenced PlantUML Diagram Example
Here, the markdown file refers to another file (*.puml or *.pu) which contains the diagram description.
```plantuml
!include umldiagram.puml
```
Note: To refer external diagram files, we need to include the diagrams directory in the path. Please refer Dockerfile for details.
How to add Mermaid support in TechDocs
There are a few options for adding Mermaid support in TechDocs: using Kroki or markdown-inline-mermaid to generate the diagrams at build time, or the backstage-plugin-techdocs-addon-mermaid plugin to generate the diagram in the browser. We currently use backstage-plugin-techdocs-addon-mermaid plugin for the Mermaid example on the Demo site.