<commit_before><commit_msg>new blog post - gocd materials<commit_after>---
layout: post
title: How to Work with Materials in GoCD
type: post
author: <NAME>
excerpt: "In this post, we'll look at environment strategy including artifact promotion and leveraging modern infrastructure for dynamic environments."
summary_image: "/assets/images/blog/cd-hacks/cd-hacks-gocd-materials.jpg"
title_tag_of_header: "How to Works with Materials in GoCD | GoCD Blog"
meta_description: "You can start with thinking about what environments need to be created upfront and talking about the intended use cases for these environments. Different groups in your organization will have different competing needs. Your environment plan should accommodate the needs from different parties."
meta_keywords: "Microservices, continuous delivery, microservice environment, environment strategy, artifacts, provision environment"
tags: CD Hacks
---
<% content_for :banner do %>
  <figure>
    <img src="/assets/images/blog/cd-hacks/cd-hacks-gocd-materials.jpg" alt="Materials in GoCD"/>
  </figure>
<% end %>

In this new segment called #GoCDHacks, we introduce some quick and easy ways to get things done in GoCD. In this post, we’re going to cover how to use? materials in GoCD. 

A material is the 'cause' for a deployment pipeline to run. In most cases, it is a source code material repository (like Git, SVN, Mercurial, etc). A CD server continuously polls the configured materials and when a new change or commit is found, the corresponding pipelines are run or "triggered". A pipeline can even be configured with multiple materials.

The best way to control what changes get polled, and eventually trigger your pipeline, is by using the blacklisting/whitelisting feature for materials. 

##What is material blacklisting/whitelisting? 

The blacklisting feature for materials in GoCD allows users to specify a file or a set of files for GoCD to ignore when it polls for new changes. The advantage of this is that repository changesets which contain blacklisted file/s will not trigger a pipeline automatically. 

Sometimes, instead of ignoring files, you want to trigger a pipeline only for specific files or folder/s. GoCD allows you to invert the blacklisting feature to whitelist and poll only for changes to specific files 

##How does it work?

Let’s say you have a pipeline which has four different git materials.

Git - g1 : poll for new changes enabled and everything is blacklisted.
Git - g2 : poll for new changes enabled and files under misc folder is blacklisted.
Git - g3 : poll for new changes disabled with no blacklisting.
Git - g4 : poll for new changes enabled with no blacklisting.

    <img src="/assets/images/blog/cd-hacks/material-blacklisting.jpeg" alt="Blacklisting Materials in GoCD"/>
    
Here’s a breakdown of how your pipeline behaves when there are commits made to the source code materials. 

**At 10:00 AM:** Commit g1-c2 happens on g1.
Result: Pipeline does not trigger, because the material is fully blacklisted. But, the server sees that commit and will change its latest known commit to g1-c2.

**At 10:05 AM:** Commit g2-c2 happens on g2. 
All changes in the commit affect only misc/abc/README.txt and misc/def/hello.txt

**Result:** Pipeline does not trigger, because path misc/**/* is blacklisted. But, the server sees that commit and will change its latest known commit to g2-c2.

**At time 10:10 AM:** Commit g3-c2 happens on g3.

**Result:** Pipeline does not trigger, because polling is turned off (and the server does not see that commit at all).

**At time 10:15 AM:** Commit g4-c2 happens on g4.

**Result:** Pipeline triggers - with g1-c2, g2-c2, g3-c1 and g4-c2. Since polling is turned off, g3-c2 has not been seen.

**At time 10:20 AM:** Commit g2-c3 happens on g2. Changes in the commit affect misc/abc/README.txt and src/test.rb

**Result:** Pipeline triggers - with g1-c2, g2-c3, g3-c1 and g4-c2. This is because src/test.rb does not match the blacklist misc/**/*. 

**At time 10:25 AM:** Pipeline is manually forced (click on "Play" button)

**Result:** Pipeline triggers - with g1-c2, g2-c3, g3-c2 and g4-c2. A manual trigger will forcibly update every material it knows of to the latest revision and triggers the pipeline.

In order to perform whitelisting, you choose “Invert the file filter, e.g. a blacklist becomes a whitelist instead.” option as shown below.

INSERT IMAGE HERE

##Server-side Material Blacklisting vs Fetch Materials

In blacklisting, all of the decisions about which materials to use for a pipeline trigger are configured on the GoCD server side and gives advantage to skip a pipeline trigger for set of files and folder.  

The "Fetch Materials" option, on the other hand, affects only the Agent side. It's an option which allows the agent to not fetch / checkout / clone the materials at all, when run. By this time, the server has already decided which materials to use. The agent cannot control it.

INSERT IMAGE HERE

When Fetch Material is set to False.
`<stage name="stagename" fetchMaterials="false">`

“Fetch materials” option should be turned off only in cases where you don't care about the material at all. In some cases, you just want a timer trigger for a pipeline - and don't care about the revision of the checked out material - or you want to check out the material yourself for some reason. Then, you'd turn off "Fetch materials".


We believe incorporating the GoCD material blacklisting/whitelisting feature would help avoid undesirable pipeline builds and help save time.

(placeholder text, waiting for Ankit to finish editing this portion)
Check out our help documentation to know more about material blacklisting/whitelisting feature.
More info about blacklisting/whitelisting can be found here - https://docs.gocd.org/current/configuration/admin_add_material.html
{'fraction_non_alphanumeric': 0.0639863130881095, 'fraction_numerical': 0.012489307100085544, 'mean_word_length': 4.388018433179724, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4048411', 'n_tokens_mistral': 1599, 'n_tokens_neox': 1500, 'n_words': 850}