Course contentsShow
← Cloud and Infrastructure
Lesson 1824 of 2,98938. AWS Native CI/CDFree lesson

Buildspec File Structure and Syntax

Master the buildspec.yml file format including phases, commands, environment variables, and artifact definitions.

Buildspec File Structure and Syntax

You'll learn how to write and organize a buildspec.yml file that tells CodeBuild exactly what to do during your build process.

What Is a Buildspec File?

Think of the buildspec.yml file as a recipe for your build. Just as a recipe lists ingredients, preparation steps, and what you'll have at the end, the buildspec file defines environment settings, build commands organized by phase, and what artifacts to save. CodeBuild reads this YAML file from your repository root (or a custom location you specify) and executes your build accordingly.

Core Structure

The buildspec file uses a version declaration (currently 0.2) and organizes work into key sections:

Phases

Phases represent sequential stages of your build: install, pre_build, build, and post_build. Each phase contains commands that execute in order. You might install dependencies in install, authenticate in pre_build, compile code in build, and package artifacts in post_build.

Environment Variables

The env section defines variables available throughout your build. You can specify plain-text values or reference secrets from Parameter Store or Secrets Manager.

Artifacts

The artifacts section tells CodeBuild which files to preserve after the build completes—like compiled binaries or packaged applications. You specify files patterns and optionally a destination path.

Additional Sections

You can define cache paths for dependency caching between builds, and reports for test result integration.

Simple Structure

version: 0.2
env:
  variables:
    NODE_ENV: production
phases:
  install:
    commands:
      - echo Installing dependencies
  build:
    commands:
      - echo Building application
artifacts:
  files:
    - '**/*'

Key Takeaway: The buildspec.yml file uses a structured YAML format with phases, environment configuration, and artifact definitions to orchestrate your entire build process in CodeBuild.