Back to Blog

Deploy Headless CMS-Powered Sites Effortlessly with Convox

Managing and deploying dynamic websites powered by headless CMS platforms like Contentful often involves navigating complex workflows and scaling challenges. By combining Gatsby's blazing-fast framework with Convox's streamlined deployment capabilities, you can simplify your content management and site deployment process—saving time and ensuring scalability.

This guide will walk you through building and deploying a Gatsby site integrated with Contentful. While our focus is on Contentful, the same process applies to other headless CMS platforms like Sanity, Strapi, and Prismic, making it an adaptable solution for any content-driven project.

Step 1: Create Your Gatsby Application

Start by scaffolding your Gatsby project using the Gatsby CLI. Choose a starter template that suits your project type, such as a blog, portfolio, or e-commerce site:

npx gatsby new gatsby-starter-blog https://github.com/gatsbyjs/gatsby-starter-blog

This creates a new Gatsby site based on the gatsby-starter-blog template. Customize it to align with your project's specific requirements.

Step 2: Set Up Contentful as Your CMS

Contentful's headless architecture simplifies content management, making it easy to deliver structured, reusable content across your site:

  • Create Your Content Models: Define structures like blog posts, pages, or products in Contentful.
  • Populate Your Content: Add the necessary data to your content models.
  • Retrieve API Credentials: Obtain your Space ID and Access Token from Contentful's API keys section.

For example:

Space ID: <your-space-id>
Access Token: <your-access-token>

The process is similar for other CMS platforms. For instance, Prismic offers flexible content modeling, and Sanity provides real-time collaboration features.

Step 3: Configure Your Gatsby App with Contentful

Install the gatsby-source-contentful plugin to fetch content from your Contentful space. Update your gatsby-config.js file with the necessary plugin configuration:

module.exports = {
  plugins: [
    {
      resolve: `gatsby-source-contentful`,
      options: {
        spaceId: process.env.CONTENTFUL_SPACE_ID,
        accessToken: process.env.CONTENTFUL_ACCESS_TOKEN,
      },
    },
  ],
};

Securely store your API credentials in an .env file:

CONTENTFUL_SPACE_ID=<your-space-id>
CONTENTFUL_ACCESS_TOKEN=<your-access-token>

For other CMS platforms, use their respective Gatsby source plugins (e.g., gatsby-source-sanity or gatsby-source-prismic) and follow similar setup steps.

Step 4: Create a Dockerfile for Your Gatsby App

Containerizing your app ensures consistency across environments and smooth deployments. Use the following Dockerfile, which securely integrates your Contentful credentials:

FROM node:18 as builder
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
ARG CONTENTFUL_SPACE_ID
ENV CONTENTFUL_SPACE_ID=${CONTENTFUL_SPACE_ID}
ARG CONTENTFUL_ACCESS_TOKEN
ENV CONTENTFUL_ACCESS_TOKEN=${CONTENTFUL_ACCESS_TOKEN}
RUN npm run build
FROM nginx:stable-alpine
COPY --from=builder /usr/src/app/public /usr/share/nginx/html
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]

This approach works with other CMS platforms by substituting environment variables and build steps specific to the chosen platform.

Step 5: Configure the Convox Manifest

The convox.yml file defines your app and infrastructure for seamless deployments. Here's an example configuration:

environment:
  - PORT=80
  - NODE_ENV=production
  - CONTENTFUL_SPACE_ID
  - CONTENTFUL_ACCESS_TOKEN

services:
  web:
    domain: ${HOST}
    build: .
    port: 80

This setup ensures your Contentful credentials are securely passed to the app and makes deployment scalable and efficient.

Step 6: Deploy Your Site with Convox

Convox simplifies deployments with its powerful workflows, enabling seamless management of staging, production, and review environments. By integrating your source control platform (GitHub or GitLab) with the Convox Console, you can automate deployments triggered by code changes or create review environments for testing pull requests.

Connecting Your Repository to Convox

To enable workflows, start by setting up a source integration in the Convox Console:

  • Enable the Integration: Navigate to the Integrations page in the Console and enable either the GitHub or GitLab integration.

Automating Deployments for Your Gatsby Site

Deployment workflows are ideal for keeping your staging or production environments up to date with the latest content and code changes. For a headless CMS like Contentful, this ensures that your site reflects the latest content updates without manual intervention.

Here's how a typical deployment workflow works for your Gatsby site:

  • Staging Deployments: When a branch merge triggers the workflow, Convox builds and deploys the updated site to your staging environment. This is particularly useful for previewing how new content, layouts, or features will look before they go live.
  • Production Deployments: For master/main branch merges, you can set up the workflow to deploy directly to production, either automatically or with manual promotion for greater control. Pre-promotion commands like database migrations or cache invalidations can be configured to ensure your site's readiness.

For headless CMS platforms like Prismic or Sanity, which emphasize content previews, Convox's deployment workflows can also handle the integration of preview servers or static build caches.

Testing Changes with Review Workflows

Review workflows allow you to validate changes tied to pull requests before merging them into your main codebase. With headless CMS platforms, this ensures that both structural changes (e.g., new content models) and content updates work harmoniously.

Here's how you can use review workflows for your Gatsby site:

  • When a pull request is opened, Convox builds your application and runs any tests specified in the test directive of your convox.yml. This guarantees that your site builds successfully with the latest updates.
  • You can optionally deploy a demo application tied to the pull request. This creates an isolated, temporary environment where stakeholders can preview updates and give feedback. For instance, a marketing team can review a new landing page built in Contentful, or a QA team can verify site behavior with changes made in Strapi.
  • Once the pull request is merged, the demo environment is automatically cleaned up, keeping your infrastructure tidy.

Tailoring Workflows for Headless CMS Use Cases

Convox workflows are flexible and adaptable to meet the unique demands of different headless CMS platforms:

  • Contentful: Automate production deployments as soon as new content is published by triggering workflows from a dedicated branch. This ensures that dynamic content updates are instantly reflected on your site.
  • Sanity: Take advantage of real-time collaboration by deploying updates to staging environments immediately after a content editor publishes changes.
  • Prismic: Use demo applications in review workflows to test new slices or components in a visually isolated environment, enabling your team to perfect the design before it goes live.
  • Strapi: Combine workflows with Convox's containerized infrastructure to deploy updates from self-hosted Strapi instances, ensuring scalability and robust testing for custom content solutions.

These tailored workflows ensure that your deployment pipeline is not just automated but optimized for the specific strengths and needs of your CMS.

Seamless Deployment with Convox

Whether you're deploying regular content updates or testing experimental features, Convox workflows make it easy to automate and manage your site's lifecycle. By connecting your GitHub or GitLab repository and configuring deployment and review workflows, you can create a smooth, scalable pipeline for your headless CMS-powered Gatsby site.

Start by integrating your repository in the Convox Console and see how automated workflows can transform your development process!

Why Use Convox with Contentful (or Any Headless CMS)?

Convox seamlessly integrates with Contentful and other popular headless CMS platforms, offering:

  • Scalable Infrastructure: Automatically adjusts resources to handle changing traffic patterns.
  • Secure Environment Management: Protects API credentials and ensures secure deployments.
  • Simplified Deployments: Streamlines delivery of content-driven sites with minimal effort.
  • CMS Flexibility: Supports Contentful, Sanity, Strapi, Prismic, ButterCMS, Storyblok, Contentstack, Kentico Kontent, and more.

Build with Contentful, Deploy with Convox

By combining Contentful's intuitive content management system with Convox's deployment power, you can deliver dynamic, high-performing websites effortlessly. Ready to get started? Clone our Gatsby example repository, integrate your Contentful credentials, and deploy your app with Convox today.

Let your team focus on what matters.