What should a CI pipeline do on a pull request versus after merging to main?
IntermediateConceptCI/CD practices
Answer
In one line: On a pull request, run fast checks that tell the author "is this change safe to merge?"; after merging to main, build the official artifact and deliver it to environments.
On a pull request (feedback for the developer):
- Build / compile.
- Lint and formatting.
- Unit tests (and coverage on new code).
- Static analysis (SonarQube, Semgrep).
- Secret scanning and dependency scanning.
- IaC checks:
terraform fmt,validate,planposted as a PR comment, Checkov. - Optionally: build the Docker image (without pushing), and spin up a temporary preview environment.
- Results posted back to the PR; branch protection blocks the merge if anything fails.
Goal: fast (ideally under 10 minutes) and no side effects on shared environments.
After merge to main (delivery):
- Build the official versioned artifact (image tagged with the commit SHA or version).
- Scan and sign the image, generate an SBOM.
- Push it to the registry.
- Deploy automatically to dev/staging.
- Run integration, end-to-end and smoke tests there.
- Promote to production (automatically, or after approval).
- Notify the team and update tickets.
Why separate them:
- PR pipelines must be safe to run on untrusted code (for example forks) — they shouldn't get production secrets.
- Only reviewed, merged code becomes a release artifact.
- Shared environments don't get spammed by every PR.
Example (GitHub Actions): on: pull_request runs ci.yml (tests, scans, Terraform plan). on: push: branches: [main] runs release.yml (build, push, deploy to staging, then production with an environment approval).
Tip for interviews: mention that PR pipelines from forks should never have access to secrets — a classic security point.
More on CI/CD practices
What is "pipeline as code", and why is it better than configuring pipelines in a UI?BeginnerWhat is a build agent or runner?BeginnerWhat are pipeline triggers?BeginnerWhat is a quality gate in a CI/CD pipeline?BeginnerHow do you make a slow CI/CD pipeline faster?BeginnerWhat are secrets in CI/CD, and how should they be handled?Beginner