JTFDI Library
just the focus do it    •    
Creator Economy
       •       
Media
       •       
Marketing
       •       
Eating & Drinking
       •       
Commerce
       •       
Customer Relationships
       •       
Reading
       •       
Tech-stack
       •       
Productivity
       •       
Startup
       •       
Technology
       •       
Business
       •       
•    just the focus do it    •     just the focus do it    
Docs
Mag
Writing
Blog
Try it for Free

Optimize Netlify Build Minutes and DecapCMS by Skipping Unnecessary Builds

2024-02-27
, most recent update.
IterationBuildsAverage timeImprovement
Initial (measured over 30 days)1801m 40s
After 30 days of optimization
After 90 days of optimization

This TOML approach is a good way to selectively trigger builds based on commit message content to optimize Netlify build minutes and DecapCMS.

This method relies on the presence of a specific phrase in the commit message of the most recent commit, which should align well with your use case of triggering builds for merge commits identified by their commit messages.

This can help optimize build times, reduce unnecessary builds, and ensure that you don’t run out of build minutes while continuously deploying every push.

Optimize Netlify Build Minutes and DecapCMS by Skipping Unnecessary Builds

Playing around with Netfliy over the weekend ended up costing me 2 hours of build minutes on Netlify.

Your free Starter Plan on Netlify has 300 minutes.

Now I have 10 Minutes of build time left for the remaining 10 days.

And therefore I have to optimize build times, reduce unnecessary builds, and ensure that I don’t run out of build minutes while continuously deploying every push.

How-To Refrain from Triggering a Netlify Build on every Change

The “Active builds” option works on the push to your Git provider: "Netlify will build your site according to your continuous deployment settings when you push to your Git provider."

Meaning, Netlify's build process is triggered by changes pushed to your Git provider, such as GitHub, GitLab, or Bitbucket.

The build system is designed to respond to every push based on the continuous deployment settings you have configured. This means that, by default, any push to the branches you've set up for deployment will initiate a build process, including pushes from merge commits, direct commits, and other changes.

The challenge I am facing is refining this process so that only certain types of pushes—specifically, merge commits resulting from pull requests—are used to trigger builds, while others, like direct commits or updates from DecapCMS (formerly NetlifyCMS), do not trigger a build unless they meet specific criteria.

Change Build Trigger for Updating DecapCMS with Netlify.toml

The direct approach with the netlify.toml file's ignore command offers a simple way to control build triggers based on the presence or absence of file changes or specific text patterns in commit messages.

However, it does not directly support more complex git operations, like identifying merge commits based on their parent count or analyzing the nature of the commit (e.g., whether it's a merge from a pull request).

For more nuanced control over the build process, including distinguishing between merge commits and other types of commits, you would typically need to integrate additional tools or scripts, CI/CD Pipelines (e.g., GitHub Actions, GitLab CI), or Netlify Build Hooks) to manually trigger builds.

Netlify TOML Approach

Adding a condition to the [build] section in your netlify.toml file to control the build process based on the presence of "Merge pull request" in the latest commit message seems to be the best approach to ensure that builds are only triggered for merge commits.

[build]
  ignore = "bash -c 'if git log -1 --pretty=%B | grep -qE \"Merge\"; then exit 1; else exit 0; fi'"

This command works as follows:

  • git log -1 --pretty=%B fetches the commit message of the most recent commit.
  • grep -qE "Merge" searches for the phrase "Merge" within that commit message. The -q option tells grep to operate quietly (without outputting the match), and -E enables extended regular expression patterns.
  • If the phrase is found, grep exits with status 0, triggering the then branch of the if statement, which executes exit 1. In the context of the Netlify ignore command, an exit status of 1 means "do not ignore this commit," i.e., proceed with the build.
  • If the phrase is not found, the else branch is executed, leading to exit 0. An exit status of 0 signals Netlify to ignore the commit, i.e., skip the build.

Note:
A previous version used the phrase "Merge pull request" within commit messages that can lead to skipped builds when updating and publishing many things quickly one after another.

Just keep in mind that this method relies on the presence of a specific phrase in the commit message of the most recent commit, which should align well with your use case of triggering builds for merge commits identified by their commit messages.

Changing Building Preview in DecapCMS Config file

You might think of changing your config file for building and previews: https://decapcms.org/docs/configuration-options/

Problem: Ours changes every now and then and does so programmatically and therefore I can’t add it to gitignore.

Read next in DOCU

Optimize Netlify Build Minutes and DecapCMS by Skipping Unnecessary Builds

Optimize Netlify Build Minutes and DecapCMS by Skipping Unnecessary Builds

Read next in DOCU

Displaying Both the Publication and Update Dates on Webpages

Showing both the update and the publish date on your blog / web page seems to be difficult for Search Engines and therefore it seems like only displaying “last updated” is the safer way. If your blog shows more than one date, you are probably confusing Google.

Read next in DOCU

Localization: The "hreflang" Tag Accross Different Domains

John Mueller on how to use the hreflang tags across domains for the localization schemas: It doesn't matter if it's all on one domain or across multiple domains. It should just be one clear place per country and language.

Read next in DOCU

DecapCMS Markdown Guide: Handling URLs with Parentheses for Text Fragment Highlighting

Are broken links in your DecapCMS Markdown articles giving you a headache? never let parentheses break your links again: handle text fragment highlighting in Markdown. No more parentheses-induced troubles in your Github articles built with Netlify!

Read next in DOCU

Leveraging a Small JavaScript for a Quick Multilingual Strategy in Webflow

A button that appears when there is an alternate language defined for your content, making your cotent more relevant to your users experiences. Implementing a Dynamic Language Switcher Button with JavaScript

Read next in DOCU

Interactive URL Copy Feature in Webflow Using Clean JavaScript and Clipboard API

An interactive URL copy feature in Webflow. Using clean JavaScript and the powerful Clipboard API, making it easier for visitors to share your pages. Because who doesn’t appreciate a great user experience?

Read next in DOCU

An Extensive Dynamic Table of Contents Generator for Webflow CMS

Boost your Webflow Blog's readability and SEO with a dynamic Table of Contents (ToC) generator. It automatically creates a stylable, navigable ToC from your headings, and enhances user experience by highlighting the active section during scrolling.

Read next in DOCU

Styling Our Auto-Generated ToC in Webflow

Read next in DOCU

Creating or Changing Slugs in DecapCMS (formerly Netlify CMS)

Changing the URL Slug of your post in DecapCMS in config.yaml using the slug key and name key

Read next in DOCU

Smooth Scrolling to the Next Section with JavaScript and [foo](#next) markdown syntax

When I write [[`foo`](#next)`](#next)` markdown syntax in my content I want the reader to jump to the next section, the next anchor, on my webpage.

Read next in DOCU

Vimeo vs YouTube Embed for your Website

Consider this when you decide between YouTube or Vimeo for Website embeds

Read next in DOCU

Editorial Workflow in Decap CMs (formerly Netlify CMS)

Read next in DOCU

README for ChatGPT and How to Write the Best Prompts

When answering prompts, does it help ChatGPT to be more specific and precise if you give it a set of definitions upfront?

Read next in DOCU

Use Hash-symbol vs Triple-quotes Syntax for Commenting Python Code

Consider this when you choose the syntax for commenting code in Python

Read next in DOCU

Dynamically Generated ToC with Webflow CMS

Generating ToC’s based on h2 and h3 headings of a rich text CMS element with active states in a sticky Table of Contents

Read next in DOCU

Dynamic Social Media Share Buttons for Webflow CMS Blogposts

Add a Twitter button and a share on LinkedIn to your blogposts with sharing content generated from Webflow CMS

Read next in DOCU

Calculate Read Time from Webflow CMS

Calculated a read time from Webflow CMS and add to your blog template.

Read next in DOCU

Add Copy-able Code Blocks to Webflow CMS

Uses highlight.js and custom code to add code blocks where you can copy the code to rich text element of Webflow CMS

Hej. I am on Twitter, too.

Connect with me on Twitter and LinkedIn.