Automated Builds and Pipelines
# CHAPTER 6
Automated Builds and Pipelines
1. Introduction
Downloading code onto a CI runner is easy; making that code actually run is the challenge. Modern applications are not just a collection of text files. They require dependencies (libraries), compilation (turning code into machine language), and asset minification (shrinking CSS/JS files). The process of assembling all these pieces into a final, runnable application is called The Build. In this chapter, we will architect a multi-stage CI pipeline that automates the build process and securely stores the final, compiled application as a downloadable Artifact.2. Learning Objectives
By the end of this chapter, you will be able to:- Define the "Build Phase" of a CI/CD pipeline.
- Understand dependency management in automated workflows (e.g., Composer, NPM).
- Structure a pipeline into logical execution Stages.
- Define what a Build Artifact is and why it matters.
- Use GitHub Actions to upload and store Artifacts.
3. Beginner-Friendly Explanation
Imagine building a custom bicycle.- The Code (Git): The blueprints for the bike.
- Dependencies: Buying the wheels, the chain, and the gears from a supplier.
- The Build Phase: Actually bolting the wheels to the frame, painting the bike, and pumping the tires.
- The Artifact: The fully assembled, ready-to-ride bicycle.
You don't want to send a customer blueprints and a box of gears. You want to send them the Artifact. A CI pipeline does the bolting, painting, and pumping automatically.
4. The Build Phase Architecture
Regardless of the programming language, a Build pipeline usually follows three distinct steps:- 1. Setup Environment: Install the correct version of PHP, Node.js, or Java on the CI Runner.
-
2.
Install Dependencies: Run the package manager command (e.g.,
composer installfor PHP,npm installfor Node,mvn clean installfor Java).
-
3.
Compile / Assemble: Run the build script (e.g.,
npm run buildto minify CSS, orgo buildto create a binary executable).
5. What is a Build Artifact?
When a CI pipeline finishes running, the temporary cloud server (the Runner) is instantly destroyed. If the Runner spent 10 minutes compiling a massive application, that compiled application is deleted too! To save the application so we can deploy it later, we must extract it from the Runner before it dies. We call this packaged, final product an Artifact (usually a.zip file, a .tar.gz, or a Docker Image).
6. Mini Project: Build CI Pipeline for PHP App
Let's build a GitHub Actions workflow that installs PHP dependencies using Composer, packages the final application, and saves it as an Artifact.Step-by-Step Architecture Concept:
*When this pipeline finishes, you can go to the "Actions" tab in GitHub and physically download release.zip. This is your production-ready artifact!*
7. Real-World Scenarios
A development team used to build their React application locally on their laptops. Developer A was using Node.js version 14. Developer B was using Node.js version 18. When Developer A compiled the code, it worked. When Developer B compiled it, it broke. They spent hours arguing over whose computer was right. The DevOps team implemented a centralized CI Build Pipeline. The pipeline strictly definednode-version: '16'. Now, the build was "Deterministic." It didn't matter what the developers had on their laptops; the official production build was always executed in the exact same environment by the CI robot, eliminating the "It works on my machine" problem permanently.
8. Best Practices
-
Exclude Dev Dependencies: When running the build phase for a production application, you should never install "dev dependencies" (like testing frameworks or debugging tools). They bloat the artifact size and introduce security vulnerabilities. Notice in our mini-project we used
composer install --no-dev. This ensures the final artifact contains ONLY the code required to run the app.
9. Security Recommendations
-
Immutable Artifacts: Once an artifact is built and given a version number (e.g.,
v1.0.5), it must never be changed. If you need to fix a bug, you don't open therelease.zipand edit the file. You fix the code in Git, push it, and let the CI pipeline generate a brand new artifact (v1.0.6). This ensures absolute traceability.
10. Troubleshooting Tips
-
Missing Files in Artifact: If you use
actions/upload-artifactbut the resulting zip file is missing directories, check thepath:variable. If you specifypath: src/, the artifact will ONLY contain thesrcfolder. If you need the entire project, specifypath: .(the current directory) or zip the files manually before uploading, as demonstrated in the project.
11. Exercises
- 1. What is the operational purpose of a "Build Artifact" in a CI/CD pipeline?
-
2.
Explain why installing dependencies (like running
npm install) must occur *before* the build/compilation phase.
12. FAQs
Q: Where are artifacts stored? A: In GitHub Actions, they are stored directly on GitHub's servers (attached to the specific workflow run). In enterprise architectures, artifacts are often pushed to dedicated artifact repositories like JFrog Artifactory or AWS S3.13. Interview Questions
- Q: Describe the lifecycle of a CI pipeline from Code Checkout to Artifact Generation. Why is it critical to extract the artifact from the CI runner before the pipeline completes?
- Q: Explain the concept of "Deterministic Builds." How does utilizing a centralized CI pipeline resolve the "It works on my machine" discrepancy among developers?
14. Summary
In Chapter 6, we transformed raw code into a functional, deployable product. We established the standard architecture of the Build Phase, ensuring our pipeline provisions the correct environment and securely resolves third-party dependencies. Most importantly, we solved the ephemeral nature of CI runners by introducing Build Artifacts. By packaging our compiled application into a.zip archive and uploading it utilizing GitHub Actions, we successfully extracted a permanent, version-controlled, production-ready asset from a temporary cloud server.