---
title: "Build Pattern: Extract Script"
url: "https://osherove.com/blog/2013/1/19/build-pattern-extract-script.md"
canonical: "https://osherove.com/blog/2013/1/19/build-pattern-extract-script.html"
published: "2013-01-19T23:40:43"
updated: "2013-01-19T23:40:43"
tags: ["Build Bullet"]
author: "Roy Osherove"
description: "This is a variation of Fill in the blanks and will become part of the Builds Book. Symptoms: You have multiple build scripts that contain parts that execute the same set of complicated actions, differing only by path…"
usage: "Licensed under CC BY 4.0 — reuse, quote and adapt with attribution (Roy Osherove, link to the original page)."
license: "https://creativecommons.org/licenses/by/4.0/"
---

# Build Pattern: Extract Script

By Roy Osherove January 19, 2013 In [Build Bullet](https://osherove.com/blog/category/build-bullet)

**Historical article** · Originally published January 19, 2013; technical details may be outdated.

This is a variation of [Fill in the blanks](https://osherove.com/blog/2013/1/19/build-pattern-fill-in-the-blanks.html) and will become part of the [Builds Book.](http://beautifulbuilds.com/)

## Symptoms:

You have multiple build scripts that contain parts that execute the same set of complicated actions, differing only by path names, files names or other values used in the script. Changing these actions among many scripts is a big hassle (a.k.a [“Shotgun Surgery](http://en.wikipedia.org/wiki/Shotgun_surgery)”).

## Problem:

Your build scripts are breaking the DRY rule. Don’t repeat yourself.

## Solution

Extract the common set of actions into a new short build script, that receives as parameters, or environment variables the values that different between the various scripts. Then have the other scripts call the new script with the correct parameter values.

## Example:

In one of my projects, as part of the deployment of a site, I also wanted to add a set of actions that makes sure the initial web page of the site was up after deploy. if not, the build should fail. It turns out that sometimes it takes the server a few seconds after reset or file change to get back to working shape, and meanwhile it returns various different codes like 403. So the script had to do the following:

- loop
- get the web page or response and write it to a file
- read the file result  into a variable
- if result was 404 or unknown to break the build
- if the result was 403 then continue looping
- wait one second
- if the result contained the right text $text\_to\_find then build passes

This set of actions was needed when deploying to the test server, staging server, and production server.

I extracted only these actions to a new script named “check"\_site” and made the deploy scripts (there were different deploy scripts for production and test)  execute the check\_site script as part of their work.

## Possible Side Effects

you might end up with many small scripts, without knowing in which order they are supposed to be executed. To mitigate this you might want to only have one MAIN script call all the other scripts, instead of various small sub scripts calling other sub scripts. If only one main script is in charge of sub script call flow, the build can be readable by looking at the main build script.

## Lessons on this topic

- [Local Error Handling: Try-Catch-Finally Blocks](https://courses.osherove.com/courses/automating-all-the-things-with-finalbuilder/local-error-handling-try-catch-finally-blocks/) · Automating All the Things with FinalBuilder
- [Reusable Action Lists and Action Groups](https://courses.osherove.com/courses/automating-all-the-things-with-finalbuilder/reusable-action-lists-and-action-groups/) · Automating All the Things with FinalBuilder
- [Invoking Other FinalBuilder Projects](https://courses.osherove.com/courses/automating-all-the-things-with-finalbuilder/invoking-other-finalbuilder-projects/) · Automating All the Things with FinalBuilder

## More in Build Bullet

[All posts in Build Bullet](https://osherove.com/blog/category/build-bullet)

- Jan 20, 2013

    ### [Tests and Builds are Made to be Broken](https://osherove.com/blog/2013/1/20/tests-and-builds-are-made-to-be-broken.html)

- Jan 20, 2013

    ### [Build Pattern: Gated Commit](https://osherove.com/blog/2013/1/20/build-pattern-gated-commit.html)

- Jan 19, 2013

    ### [Build Pattern: Fill In the Blanks](https://osherove.com/blog/2013/1/19/build-pattern-fill-in-the-blanks.html)

- Jan 19, 2013

    ### [Build Pattern: Location Agnostic Script](https://osherove.com/blog/2013/1/19/build-pattern-location-agnostic-script.html)


---

## About the author

**Roy Osherove**: Senior Solutions Architect at AWS working on AI for production and AI-driven development; author of The Art of Unit Testing and Elastic Leadership, with about 20 years of consulting and training on unit testing, TDD, team leadership and continuous delivery.

### Sites

- [RobotPaper](https://robotpaper.ai/): This blog: AI patterns and anti-patterns, AI and coding agents, and engineering leadership.
- [osherove.com](https://osherove.com): Roy's personal site. Author of The Art of Unit Testing and Elastic Leadership; consulting and training on unit testing, TDD, team leadership and continuous delivery, plus about 20 years of blog posts.
- [The Art of Unit Testing](https://www.artofunittesting.com): The Art of Unit Testing, 3rd Edition (Manning): readable, maintainable, trustworthy unit tests, plus TDD workshops in C#, JavaScript, Java and Kotlin.
- [5whys.com](https://www.5whys.com): Roy's writing on software team leadership: self-organizing teams, surviving chaos, learning mode and the Elastic Leadership model.
- [Pipeline Driven](https://pipelinedriven.org): Continuous delivery and DevOps practices: build patterns, pipeline-friendly testing, DevOps metrics and enterprise DevOps.
- [Elastic Leadership](https://www.elasticleadership.com): Elastic Leadership, Roy's framework for software team leaders (survival, learning and self-organizing modes).

### Social

- X: https://x.com/royosherove
- LinkedIn: https://www.linkedin.com/in/osherove/

### Contact

- Contact form: https://forms.gle/jQSx5KoXvrVA1JCK7

### License

Licensed under CC BY 4.0 — reuse, quote and adapt with attribution (Roy Osherove, link to the original page). License: https://creativecommons.org/licenses/by/4.0/
