<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Foundation SDK on Grafana Labs</title><link>https://grafana.com/docs/grafana/v12.4/as-code/observability-as-code/foundation-sdk/</link><description>Recent content in Foundation SDK on Grafana Labs</description><generator>Hugo -- gohugo.io</generator><language>en</language><atom:link href="/docs/grafana/v12.4/as-code/observability-as-code/foundation-sdk/index.xml" rel="self" type="application/rss+xml"/><item><title>Automate dashboard provisioning with CI/CD</title><link>https://grafana.com/docs/grafana/v12.4/as-code/observability-as-code/foundation-sdk/dashboard-automation/</link><pubDate>Fri, 03 Apr 2026 12:35:46 -0500</pubDate><guid>https://grafana.com/docs/grafana/v12.4/as-code/observability-as-code/foundation-sdk/dashboard-automation/</guid><content><![CDATA[&lt;h1 id=&#34;automate-dashboard-provisioning-with-cicd&#34;&gt;Automate dashboard provisioning with CI/CD&lt;/h1&gt;
&lt;h2 id=&#34;introduction&#34;&gt;Introduction&lt;/h2&gt;
&lt;p&gt;Managing Grafana dashboards manually can be inefficient and error-prone. As you saw in the Getting Started guide, we can define dashboards using strongly typed code with the Grafana Foundation SDK. We can then commit them to version controls, and automatically deploy them using GitHub Actions.&lt;/p&gt;
&lt;p&gt;This guide walks through:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Generating a Grafana dashboard as code&lt;/li&gt;
&lt;li&gt;Formatting it for Kubernetes-style deployment&lt;/li&gt;
&lt;li&gt;Using GitHub Actions to deploy the dashboard&lt;/li&gt;
&lt;li&gt;Checking if the dashboard exists and updating it if needed&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;By the end, every change to your dashboard code will be automatically created or updated in your Grafana instance without manual intervention.&lt;/p&gt;


  &lt;div
    class=&#34;youtube-lazyload responsive-video&#34;
    data-embed=&#34;cFnO8kVOaAI&#34;
    data-url=&#34;https://www.youtube.com/embed/cFnO8kVOaAI?autoplay=1&#34;
    data-title=&#34;YouTube Video&#34;
  &gt;
    &lt;div class=&#34;play-button&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;
&lt;p&gt;You can find the full example source code in the &lt;a href=&#34;https://github.com/grafana/intro-to-foundation-sdk/tree/main/github-actions-example&#34; target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;intro-to-foundation-sdk repository&lt;/a&gt;.&lt;/p&gt;
&lt;h2 id=&#34;1-generating-the-dashboard-json&#34;&gt;1. Generating the dashboard JSON&lt;/h2&gt;
&lt;p&gt;Before deploying a dashboard, we need to define it in code using the Grafana Foundation SDK. We ran through an example of this in the Getting Started guide, however, in order to comply with the Kubernetes resource compatible API that Grafana exposes, we’ll make some changes to the code to output the dashboard JSON in the appropriate format.&lt;/p&gt;



  

  






  

  



  &lt;div class=&#34;code&#34; x-data=&#34;app_code([&amp;#34;go&amp;#34;,&amp;#34;typescript&amp;#34;], false)&#34; x-init=&#34;init()&#34; data-codetoggle=&#34;true&#34;&gt;
    &lt;div class=&#34;toggle-toolbar &#34;&gt;
      &lt;div&gt;&lt;button class=&#34;toggle-toolbar__item&#34; :class=&#34;{ &#39;toggle-toolbar__item-active&#39;: active === &#39;go&#39; }&#34; @click=&#34;$store.code.language = &#39;go&#39;&#34;&gt;
              &lt;span&gt;Go&lt;/span&gt;
            &lt;/button&gt;&lt;button class=&#34;toggle-toolbar__item&#34; :class=&#34;{ &#39;toggle-toolbar__item-active&#39;: active === &#39;typescript&#39; }&#34; @click=&#34;$store.code.language = &#39;typescript&#39;&#34;&gt;
              &lt;span&gt;typescript&lt;/span&gt;
            &lt;/button&gt;&lt;/div&gt;
      &lt;div class=&#34;d-flex&#34;&gt;&lt;span class=&#34;code-clipboard&#34; x-ref=&#34;tooltip&#34;&gt;
          &lt;button @click=&#34;copy()&#34;&gt;
            &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
            &lt;span&gt;Copy&lt;/span&gt;
          &lt;/button&gt;
        &lt;/span&gt;
      &lt;/div&gt;
      &lt;div class=&#34;toggle-toolbar__border&#34;&gt;&lt;/div&gt;
    &lt;/div&gt;
    
    &lt;div class=&#34;code-rendered&#34; &gt;
      
&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;Go&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-go&#34;&gt;package main

import (
	&amp;#34;encoding/json&amp;#34;
	&amp;#34;log&amp;#34;
	&amp;#34;os&amp;#34;

	&amp;#34;github.com/grafana/grafana-foundation-sdk/go/cog&amp;#34;
	&amp;#34;github.com/grafana/grafana-foundation-sdk/go/common&amp;#34;
	&amp;#34;github.com/grafana/grafana-foundation-sdk/go/dashboard&amp;#34;
)

type DashboardWrapper struct {
	APIVersion string              `json:&amp;#34;apiVersion&amp;#34;`
	Kind       string              `json:&amp;#34;kind&amp;#34;`
	Metadata   Metadata            `json:&amp;#34;metadata&amp;#34;`
	Spec       dashboard.Dashboard `json:&amp;#34;spec&amp;#34;`
}

type Metadata struct {
	Name string `json:&amp;#34;name&amp;#34;`
}

func main() {
	builder := dashboard.NewDashboardBuilder(&amp;#34;My Dashboard&amp;#34;).
		Uid(&amp;#34;my-dashboard&amp;#34;).
		Tags([]string{&amp;#34;generated&amp;#34;, &amp;#34;foundation-sdk&amp;#34;, &amp;#34;go&amp;#34;}).
		Refresh(&amp;#34;5m&amp;#34;).
		Time(&amp;#34;now-1h&amp;#34;, &amp;#34;now&amp;#34;).
		Timezone(common.TimeZoneBrowser).
		WithRow(dashboard.NewRowBuilder(&amp;#34;Overview&amp;#34;))

	dashboard, err := builder.Build()
	if err != nil {
		log.Fatalf(&amp;#34;failed to build dashboard: %v&amp;#34;, err)
	}

	dashboardWrapper := DashboardWrapper{
		APIVersion: &amp;#34;dashboard.grafana.app/v1beta1&amp;#34;,
		Kind:       &amp;#34;Dashboard&amp;#34;,
		Metadata: Metadata{
			Name: *dashboard.Uid,
		},
		Spec: dashboard,
	}

	dashboardJson, err := json.MarshalIndent(dashboardWrapper, &amp;#34;&amp;#34;, &amp;#34;  &amp;#34;)
	if err != nil {
		log.Fatalf(&amp;#34;failed to marshal dashboard: %v&amp;#34;, err)
	}

	err = os.WriteFile(&amp;#34;dashboard.json&amp;#34;, dashboardJson, 0644)
	if err != nil {
		log.Fatalf(&amp;#34;failed to write dashboard to file: %v&amp;#34;, err)
	}

	log.Printf(&amp;#34;Dashboard JSON:\n%s&amp;#34;, dashboardJson)
}&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;typescript&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-typescript&#34;&gt;import { DashboardBuilder, RowBuilder } from &amp;#39;@grafana/grafana-foundation-sdk/dashboard&amp;#39;;
import * as fs from &amp;#39;fs&amp;#39;;

// Generate the dashboard JSON
const dashboard = new DashboardBuilder(&amp;#39;My Dashboard&amp;#39;)
  .uid(&amp;#39;my-dashboard&amp;#39;)
  .tags([&amp;#39;generated&amp;#39;, &amp;#39;foundation-sdk&amp;#39;, &amp;#39;typescript&amp;#39;])
  .refresh(&amp;#39;5m&amp;#39;)
  .time({ from: &amp;#39;now-1h&amp;#39;, to: &amp;#39;now&amp;#39; })
  .timezone(&amp;#39;browser&amp;#39;)
  .withRow(new RowBuilder(&amp;#39;Overview&amp;#39;))
  .build();

// Convert to Kubernetes-style format
const dashboardWrapper = {
  apiVersion: &amp;#34;dashboard.grafana.app/v1beta1&amp;#34;,
  kind: &amp;#34;Dashboard&amp;#34;,
  metadata: {
    name: dashboard.uid!
  },
  spec: dashboard
};

// Save the formatted JSON to a file
const dashboardJSON = JSON.stringify(dashboardWrapper, null, 2);
fs.writeFileSync(&amp;#39;dashboard.json&amp;#39;, dashboardJSON, &amp;#39;utf8&amp;#39;);

console.log(`Dashboard JSON:\n${}`);&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;

    &lt;/div&gt;
  &lt;/div&gt;


&lt;p&gt;This script:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Generates a Grafana dashboard JSON file&lt;/li&gt;
&lt;li&gt;Wraps it in a Kubernetes-style API format (&lt;code&gt;apiVersion&lt;/code&gt;, &lt;code&gt;kind&lt;/code&gt;, &lt;code&gt;metadata&lt;/code&gt;, &lt;code&gt;spec&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;Saves it as &lt;code&gt;dashboard.json&lt;/code&gt; for deployment&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&#34;2-automating-deployment-with-github-actions&#34;&gt;2. Automating deployment with GitHub Actions&lt;/h2&gt;
&lt;p&gt;Next, we’ll set up GitHub Actions to:
Extract the dashboard name from &lt;code&gt;dashboard.json&lt;/code&gt;
Check if the dashboard already exists within our Grafana instance
Update it if it does, create it if it doesn’t&lt;/p&gt;


&lt;div class=&#34;admonition admonition-note&#34;&gt;&lt;blockquote&gt;&lt;p class=&#34;title text-uppercase&#34;&gt;Note&lt;/p&gt;&lt;p&gt;The following GitHub Action configuration assumes you are using a Go-based dashboard generator. If you are using one of the other languages that the Foundation SDK supports, please modify the &lt;strong&gt;Generate Dashboard JSON&lt;/strong&gt; step accordingly.&lt;/p&gt;&lt;/blockquote&gt;&lt;/div&gt;

&lt;p&gt;&lt;code&gt;.github/workflows/deploy-dashboard.yml&lt;/code&gt;&lt;/p&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;YAML&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-yaml&#34;&gt;name: Deploy Grafana Dashboard

on:
  push:
    branches:
      - main

jobs:
  deploy:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout code
        uses: actions/checkout@v3

      - name: Set up Go
        uses: actions/setup-go@v5
        with:
          go-version: 1.24.6

      - name: Verify Go version
        run: go version

      - name: Download and Extract grafanactl
        run: |
          curl -L -o grafanactl-x86_64.tar.gz &amp;#34;https://github.com/grafana/grafanactl/releases/download/${{ vars.GRAFANACTL_VERSION }}/grafanactl_Linux_x86_64.tar.gz&amp;#34;
          tar -xzf grafanactl-x86_64.tar.gz
          chmod &amp;#43;x grafanactl
          sudo mv grafanactl /usr/local/bin/grafanactl

      - name: Generate Dashboard JSON
        working-directory: ./github-actions-example
        run: go run main.go

      - name: Deploy Dashboard with grafanactl
        env:
          GRAFANA_SERVER: ${{ vars.GRAFANA_SERVER }}
          GRAFANA_STACK_ID: ${{ vars.GRAFANA_STACK_ID }}
          GRAFANA_TOKEN: ${{ secrets.GRAFANA_TOKEN }}
        run: |
          if [ -f dashboard.json ]; then
            echo &amp;#34;dashboard.json exists, deploying dashboard.&amp;#34;
            grafanactl resources push dashboards --path ./dashboard.json
          else
            echo &amp;#34;dashboard.json does not exist.&amp;#34;
            exit 1
          fi
        working-directory: ./github-actions-example&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;h2 id=&#34;3-explaining-this-github-action&#34;&gt;3. Explaining this GitHub Action&lt;/h2&gt;
&lt;p&gt;This GitHub Action automates the deployment of a Grafana dashboard using the Foundation SDK and the &lt;code&gt;grafanactl&lt;/code&gt; CLI tool.&lt;/p&gt;
&lt;h3 id=&#34;1-checkout-and-set-up-go&#34;&gt;1. Checkout and set up Go&lt;/h3&gt;
&lt;p&gt;The first few steps:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Check out the repository to access the project code.&lt;/li&gt;
&lt;li&gt;Install Go 1.24.6 using the &lt;code&gt;actions/setup-go&lt;/code&gt; action.&lt;/li&gt;
&lt;li&gt;Verify Go is properly installed.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&#34;2-download-and-install-grafanactl&#34;&gt;2. Download and install &lt;code&gt;grafanactl&lt;/code&gt;&lt;/h3&gt;
&lt;p&gt;This step downloads the &lt;code&gt;grafanactl&lt;/code&gt; CLI from GitHub using a version defined in &lt;code&gt;vars.GRAFANACTL_VERSION&lt;/code&gt;. It unpacks the tarball, makes it executable, and moves it to a location in the system &lt;code&gt;PATH&lt;/code&gt;.&lt;/p&gt;
&lt;h3 id=&#34;3-generate-the-dashboard-json&#34;&gt;3. Generate the dashboard JSON&lt;/h3&gt;
&lt;p&gt;Runs the dashboard generator (&lt;code&gt;main.go&lt;/code&gt;) from the &lt;code&gt;./github-actions-example&lt;/code&gt; directory. This should produce a &lt;code&gt;dashboard.json&lt;/code&gt; file that contains the Grafana dashboard definition.&lt;/p&gt;
&lt;h3 id=&#34;4-deploy-the-dashboard-with-grafanactl&#34;&gt;4. Deploy the dashboard with &lt;code&gt;grafanactl&lt;/code&gt;&lt;/h3&gt;
&lt;p&gt;If &lt;code&gt;dashboard.json&lt;/code&gt; exists, it is deployed to your Grafana instance using:&lt;/p&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;Bash&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-bash&#34;&gt;grafanactl resources push dashboards --path ./dashboard.json&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;This command authenticates against Grafana using the following environment variables:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;GRAFANA_SERVER&lt;/code&gt;: Your Grafana instance URL&lt;/li&gt;
&lt;li&gt;&lt;code&gt;GRAFANA_STACK_ID&lt;/code&gt;: Your Grafana stack ID&lt;/li&gt;
&lt;li&gt;&lt;code&gt;GRAFANA_TOKEN&lt;/code&gt;: A Grafana service account token with sufficient permissions&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&#34;github-variables-and-secrets-used&#34;&gt;GitHub variables and secrets used&lt;/h3&gt;
&lt;p&gt;These are configured in your repository under &lt;strong&gt;Settings → Security → Secrets and variables → Actions&lt;/strong&gt;:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;vars.GRAFANACTL_VERSION&lt;/code&gt;: Version of &lt;code&gt;grafanactl&lt;/code&gt; to install&lt;/li&gt;
&lt;li&gt;&lt;code&gt;vars.GRAFANA_SERVER&lt;/code&gt;: The URL of your Grafana instance&lt;/li&gt;
&lt;li&gt;&lt;code&gt;vars.GRAFANA_STACK_ID&lt;/code&gt;: The stack ID in Grafana&lt;/li&gt;
&lt;li&gt;&lt;code&gt;secrets.GRAFANA_TOKEN&lt;/code&gt;: Grafana API token&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This action ensures that every push to &lt;code&gt;main&lt;/code&gt; will regenerate and deploy your latest dashboard definition to Grafana.&lt;/p&gt;
&lt;h3 id=&#34;why-automate-this&#34;&gt;Why automate this?&lt;/h3&gt;
&lt;p&gt;Automating Grafana dashboard deployment eliminates the need for manual dashboard creation and updates, ensuring that dashboards remain consistent across environments. By defending dashboards as code and managing them through CI/CD such as GitHub Actions, we gain full version control, making it easy to track changes over time and roll back if needed. This also prevents duplication, as the workflow intelligently checks whether a dashboard exists before deciding to create or update it. With this fully automated CI/CD pipeline, developers can focus on improving their dashboards rather than manually uploading JSON files to Grafana.&lt;/p&gt;
&lt;h3 id=&#34;conclusion&#34;&gt;Conclusion&lt;/h3&gt;
&lt;p&gt;By integrating the Grafana Foundation SDK with GitHub Actions, we have successfully automated the entire lifecycle of Grafana dashboards. This setup allows us to define dashboards programmatically, convert them into a Kubernetes-compatible format, and deploy them automatically. With each push to the repository, the workflow ensures that dashboards are either created or updated as needed. This not only improves the efficiency but also guarantees that all deployed dashboards are always in sync with the latest code changes, reducing manual effort and potential errors.&lt;/p&gt;
]]></content><description>&lt;h1 id="automate-dashboard-provisioning-with-cicd">Automate dashboard provisioning with CI/CD&lt;/h1>
&lt;h2 id="introduction">Introduction&lt;/h2>
&lt;p>Managing Grafana dashboards manually can be inefficient and error-prone. As you saw in the Getting Started guide, we can define dashboards using strongly typed code with the Grafana Foundation SDK. We can then commit them to version controls, and automatically deploy them using GitHub Actions.&lt;/p></description></item></channel></rss>