55 Mins

12. Git in CI/CD — Automation & Deployment

Advanced

Git in CI/CD — Automation & Deployment

Modern software teams do not manually deploy code. Every push to a Git repository triggers automated pipelines that test, build, and deploy code without human intervention. Understanding how Git integrates with CI/CD (Continuous Integration / Continuous Deployment) is essential for senior engineers and anyone working on teams that ship frequently.

What CI/CD Actually Does

Developer pushes
git push
→
CI triggers
GitHub Actions
→
Tests run
Pass / Fail
→
Build created
Docker image
→
Deploy staging
Auto
→
Deploy production
On merge to main

GitHub Actions — CI/CD Built Into GitHub

GitHub Actions is GitHub's built-in CI/CD system. It runs automated workflows in response to Git events (push, pull request, merge). Workflows are defined in YAML files in .github/workflows/:

Bash
mkdir -p .github/workflows

The workflow YAML file lives at .github/workflows/ci.yml. A basic CI pipeline in YAML would:

  1. Trigger on every push and pull request
  2. Check out the code
  3. Set up Node.js
  4. Install dependencies (npm install)
  5. Run tests (npm test)
  6. Run linting (npm run lint)
  7. Build the app (npm run build)

If any step fails, the PR cannot be merged (due to branch protection rules).

Branch Protection Rules

Branch protection rules on GitHub prevent developers from pushing directly to main or force-pushing over history. Configure these in: GitHub repo → Settings → Branches → Add rule. Required settings for a professional team:

  • Require pull request before merging — No direct pushes to main
  • Require at least 1 approval — Another developer must review before merge
  • Require status checks to pass — CI pipeline must pass (tests green)
  • Require branches to be up to date — Branch must include latest main before merge
  • Do not allow force pushes — History is permanent; cannot be rewritten

Git Tags & Releases

Tags mark specific commits as important (usually version releases). Create a lightweight tag:

Bash
git tag v1.0.0

Create an annotated tag (with a message and tagger info):

Bash
git tag -a v1.0.0 -m "Release 1.0.0 — first stable version"

Push tags to remote (they don't push automatically with git push):

Bash
git push origin --tags

List all tags:

Bash
git tag

Checkout a specific version to inspect it:

Bash
git checkout v1.0.0

On GitHub, tags automatically create a Release page where you can attach release notes and downloadable build artefacts.

Knowledge Check

Ready to test your understanding of 12. Git in CI/CD — Automation & Deployment?

Back