Pipeline Stages: Source, Build, Deploy, Approval
AWS CodePipeline orchestrates CI/CD workflows with stages, each containing actions.
A typical pipeline:
Source stage: Pulls code from a repository. Use CodeCommit for AWS-hosted repos, or CodeStar Connections for GitHub/GitLab.
Build stage: Compiles code and runs tests using CodeBuild. The build produces artifacts stored in S3.
Test stage: Runs additional tests (integration, security, performance) as separate actions.
Deploy stage: Deploys artifacts to target environments using CodeDeploy, ECS, CloudFormation, or Lambda.
Approval stage: Manual approval before production deployment. Sends an SNS notification and waits for approval via the console or CLI.
aws pipeline create-pipeline --cli-input-json file://pipeline.json
Pipeline execution history shows each run with timestamps, status, and change details. CloudWatch Events triggers notifications on pipeline state changes.
CodeBuild: Buildspec, Environments, Artifacts
CodeBuild compiles code, runs tests, and produces artifacts. It uses a buildspec.yml file to define the build process.
version: 0.2
phases:
install:
runtime-versions:
nodejs: 20
commands:
- npm ci
pre_build:
commands:
- npm run lint
- npm test
build:
commands:
- npm run build
post_build:
commands:
- echo Build completed
artifacts:
files:
- dist/**/*
- package.json
cache:
paths:
- node_modules/**/*
Environments define the build runtime: compute type (BUILD_GENERAL1_SMALL), image (aws/codebuild/standard:7), and service role. Use custom Docker images for specialized build tools.
Artifacts are the build outputs stored in S3. CodePipeline passes artifacts between stages. Use artifact identifiers for multiple artifact groups.
Caching speeds up builds by storing dependencies between runs. CodeBuild supports S3 and local caching.
CodeCommit, CodeStar Connections, GitHub Integration
CodePipeline supports multiple source providers.
CodeCommit: AWS-managed Git repositories. IAM-based access, triggers for CloudWatch Events, and integration with CodePipeline and CodeBuild.
GitHub via CodeStar Connections: Connect to GitHub repos using OAuth. Create a connection in the CodeStar console, then reference it in your pipeline. Supports GitHub Enterprise Cloud.
{
"actionTypeId": {
"category": "Source",
"owner": "AWS",
"provider": "CodeStarSourceConnection",
"version": "1"
},
"configuration": {
"ConnectionArn": "arn:aws:codestar-connections:us-east-1:123456789012:connection/abc",
"FullRepositoryId": "myorg/myrepo",
"BranchName": "main"
}
}
S3: Store source artifacts in S3. Useful for packaging code before pipeline execution.
Source polling vs events: Pipelines can poll for changes (every few minutes) or use CloudWatch Events for immediate triggering. Events-based triggering is preferred for faster feedback.
Pipeline Variables, Cross-Region, and Cross-Account
CodePipeline supports advanced features for complex deployments.
Pipeline variables pass dynamic values between stages. Variables can come from CloudWatch Events, manual values, or variables defined in the pipeline.
Cross-region deployment deploys to resources in different AWS regions. Define stages that target Lambda functions or CloudFormation stacks in other regions.
Cross-account deployment deploys to resources in different AWS accounts. Use IAM roles in target accounts and assume them from the pipeline account.
Conditional execution uses conditions to skip stages based on branch, file changes, or manual input. Deploy to staging only on main branch, deploy to production only on release tags.
Parallel actions run multiple actions within a stage simultaneously. Run unit tests and linting in parallel during the build stage.
Stage transitions can be disabled manually to pause the pipeline. Use this during maintenance windows or incidents.
Quiz
1. What is the purpose of a buildspec.yml in CodeBuild?
2. How do you trigger a CodePipeline from GitHub?
3. What is the purpose of the approval action in CodePipeline?
Flashcards
Question
What is CodePipeline?
Click to reveal answer
Answer
AWS service for orchestrating CI/CD workflows with stages (source, build, test, deploy) and actions.
Question
What is CodeBuild?
Click to reveal answer
Answer
AWS service that compiles code, runs tests, and produces artifacts using a buildspec.yml configuration.
Question
What is a CodeStar Connection?
Click to reveal answer
Answer
An OAuth connection to external providers (GitHub, GitLab) for use as pipeline source actions.
Question
How does artifact passing work in CodePipeline?
Click to reveal answer
Answer
Each stage produces artifacts stored in S3, which are consumed by subsequent stages as input.
Revision Notes
Key Takeaways
- 1. CodePipeline stages: source, build, test, deploy, approval
- 2. CodeBuild uses buildspec.yml for build configuration
- 3. CodeStar Connections integrate GitHub with CodePipeline
- 4. Artifacts flow between stages via S3
Interview Tips
- • Design a multi-environment pipeline with approval gates
- • Explain how you would set up cross-account deployments
- • Describe CodeBuild caching strategies for faster builds
- • Discuss pipeline triggering options: polling vs events
Cheat Sheet
Pipeline: source -> build -> test -> deploy (approval gates). CodeBuild: buildspec.yml defines phases, artifacts, caching. Sources: CodeCommit, CodeStar Connections (GitHub), S3. Artifacts: S3-based, passed between stages. Advanced: cross-region, cross-account, conditional execution.