Exclude file from build based on front-matter variable value for a given environment

I would like to exclude certain files from the build based on the value of a variable defined in the front-matter of the files for a given environment.

For example, if the environment is staging, and the file front-matter contains a variable test with value true (test: true), I want to entirely exclude this file from the staging build only.

Is this possible?

I know about the exclude variable in the _config.yml and understand how it works, but I have many files in different folders and it’s a lot of effort to manually list them in the exclude variable and keep the list updated as new files are added. I need a simpler approach such as the one I described.

Thanks.

I think the only straightforward way to do this would be to write a plugin.

A more crooked way is to use a two stage build process. The first jekyll-build would use Liquid to generate a _staging_config.yml file that has a long exclude list of test pages. The second jekyll-build would use the generated exclude list to build the staging site (via --config _config.yml,_staging_config.yml). This might be a tolerable solution if it is all hidden inside a build script or Makefile (but still a bit squirrelly).

Good idea. It does the job for now. Thank you!