How to automate Sanity GraphQL deploy in CircleCI without interactive prompts?

1 replies
Last updated: Jun 16, 2022
Hello everyone, I need your help.
We are trying to integrate sanity graphql to CircleCI for data migration.

My question is : How do you migrate your dataset through the CircleCI?

What I should do in this task:
- to deploy dataset to staging and production over CI/CD which CircleCI
- to compare dataset between the staging and prod and send a notification to SlackChannel.

Manually we are able to deploy to staging and prod as follows:
❯ sanity graphql deploy --dataset=staging
❯ sanity graphql deploy --dataset=prod

But it is asking some question as follow:

Checking for deployed API
? Do you want to enable a GraphQL playground? (Y/n)
How can we handle those answers on the CircleCI ? Is there any way set-up default answer as Y

For the Slack integration, it looks pretty straightforward..


Thanks,
Rasim
AI Update

Great question! Let me help you with both parts of your CircleCI integration.

Making GraphQL Deploy Non-Interactive

For the GraphQL playground prompt, you can use the --playground or --no-playground flags to avoid the interactive question. In your CircleCI config, use:

sanity graphql deploy --dataset=staging --playground
sanity graphql deploy --dataset=prod --playground

Or if you don't want the playground enabled:

sanity graphql deploy --dataset=staging --no-playground
sanity graphql deploy --dataset=prod --no-playground

According to the CLI documentation, the command supports these flags:

  • --playground - Deploy with the GraphQL playground interface
  • --no-playground - Deploy without the playground
  • --dataset <name> - Specify which dataset to deploy for
  • --force - Deploy without confirming breaking changes (useful for CI)

Authentication in CircleCI

For authentication in your CI environment, you'll need to set up a Sanity auth token as an environment variable. Create a token in your Sanity project settings and add it as SANITY_AUTH_TOKEN in your CircleCI environment variables. The CLI will automatically pick this up and won't prompt for login.

Comparing Datasets and Slack Notifications

For comparing datasets between staging and prod and sending Slack notifications, I'd strongly recommend using Sanity Functions rather than traditional webhooks. Functions are serverless compute that run directly on Sanity's infrastructure - no external hosting needed!

Here's why Functions are better for this:

  • Native integration: They run on Sanity's infrastructure, no need to deploy separate services
  • Event-driven: Automatically triggered on document changes (create, update, publish, delete)
  • Easy setup: Deploy via CLI from a Blueprint configuration
  • Built-in monitoring: Includes logging for debugging

You could set up a Function that:

  1. Triggers when documents are published in staging
  2. Compares the document with the prod dataset
  3. Sends a notification to Slack with the differences

Here's a basic example structure:

// In your sanity.blueprint.ts
export default defineBlueprint({
  functions: [
    defineFunction({
      name: 'notify-on-publish',
      trigger: 'document.publish',
      handler: async (event, context) => {
        // Compare datasets and send to Slack
        const slackWebhook = process.env.SLACK_WEBHOOK_URL;
        // Your comparison logic here
        await fetch(slackWebhook, {
          method: 'POST',
          body: JSON.stringify({
            text: `Changes detected: ${event.documentId}`
          })
        });
      }
    })
  ]
});

If you need to stick with webhooks for some reason (like if you already have external infrastructure), you can configure those in your Sanity project settings, but Functions are the modern, recommended approach for most automation tasks like this.

For your CircleCI workflow, you'd deploy both the GraphQL API and Functions as part of your pipeline:

- run: sanity graphql deploy --dataset=staging --playground --force
- run: sanity graphql deploy --dataset=prod --playground --force
- run: sanity blueprint deploy  # Deploy your Functions

Hope this helps! Let me know if you need clarification on any part.

Show original thread
1 reply
ok, i found it on the documentation : )

Sanity – Build the way you think, not the way your CMS thinks

Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.

Was this answer helpful?