GitOps CI/CD Pipeline Integration

The Missing Piece: From Code to Production

I had GitOps working perfectly. ArgoCD watched my manifests repo and deployed changes automatically.

But there was a gap:

Developer pushes code β†’ ??? β†’ Manifests updated β†’ ArgoCD deploys
                         ↑
                    Missing piece!

The questions:

  1. How does the Docker image get built?

  2. How does the image tag get updated in Git?

  3. How do we test before deploying?

  4. How do we promote dev β†’ staging β†’ production?

The answer: CI/CD pipeline integrated with GitOps.

The Complete GitOps Workflow

spinner

Flow:

  1. Developer pushes code to app repo

  2. CI builds Docker image

  3. CI pushes image to registry

  4. CI updates image tag in config repo ← Key step

  5. ArgoCD detects config change

  6. ArgoCD deploys new version

Repository Structure

Two repositories (separation of concerns):

Application Repository (Code)

Config Repository (Manifests)

GitHub Actions CI Pipeline

Goal: Build image, push to registry, update config repo.

Complete CI Workflow

.github/workflows/ci.yaml:

What this does:

  1. Run tests

  2. Build Docker image

  3. Push to GitHub Container Registry

  4. Update config repo with new image tag

  5. ArgoCD syncs automatically

Creating Personal Access Token

For GITOPS_REPO_TOKEN:

  1. Go to GitHub Settings β†’ Developer settings β†’ Personal access tokens

  2. Generate new token (classic)

  3. Scopes: repo (full control)

  4. Copy token

  5. Add to app repo secrets: Settings β†’ Secrets β†’ Actions β†’ GITOPS_REPO_TOKEN

Environment Promotion Workflow

Dev β†’ Staging β†’ Production

Strategy: Use Pull Requests for staging/production.

Workflow:

Automated Promotion with GitHub Actions

.github/workflows/promote-staging.yaml (in config repo):

Usage:

  1. Go to Actions tab

  2. Select "Promote to Staging"

  3. Click "Run workflow"

  4. Enter image tag

  5. PR created automatically

  6. Review β†’ Merge β†’ ArgoCD syncs

Image Promotion Patterns

Pattern 1: Manual Promotion (Safest)

Process:

  1. Dev: Auto-deployed

  2. Staging: Create PR β†’ Merge β†’ Auto-synced

  3. Production: Create PR β†’ Merge β†’ Manual sync via ArgoCD UI

Pattern 2: Approval-Based Auto-Promotion

Use GitHub Environments:

Setup GitHub Environment:

  1. Repo Settings β†’ Environments β†’ New environment: "production"

  2. Add required reviewers

  3. PR to production β†’ Auto-triggers after approval

Pattern 3: Time-Based Auto-Promotion

Rollback Strategy

Git-Based Rollback

ArgoCD UI Rollback

  1. Open application

  2. Click "History and Rollback"

  3. Select previous sync

  4. Click "Rollback"

Note: This doesn't update Git! You should still commit the rollback.

Automated Rollback on Test Failure

If test fails:

  • Sync fails

  • SyncFail hooks trigger

  • Alert sent to Slack

  • Manually rollback

Complete Example: Production Pipeline

App Repo CI

Testing in CI/CD

Unit Tests

Integration Tests

E2E Tests (After Deployment)

Key Takeaways

  1. Separate app and config repos

    • App repo: Source code, Dockerfile, CI

    • Config repo: Kubernetes manifests, ArgoCD watches this

    • CI updates config repo with new image tags

  2. CI builds and updates manifests

    • Build Docker image

    • Push to registry

    • Update image tag in Git

    • ArgoCD syncs automatically

  3. Use PRs for promotion

    • Dev: Auto-deploy

    • Staging: PR β†’ Review β†’ Merge β†’ Auto-sync

    • Production: PR β†’ Review β†’ Approve β†’ Manual sync

    • Full audit trail

  4. Rollback via Git

    • git revert or git checkout

    • Commit rollback to Git

    • ArgoCD syncs old version

    • Declarative rollback

  5. Test at every stage

    • Unit tests in CI

    • Integration tests in CI

    • Post-deployment tests via ArgoCD hooks

    • E2E tests after sync

In the final article, we'll cover GitOps best practices: secret management, disaster recovery, monitoring, troubleshooting, and production lessons learned.


Previous: Advanced ArgoCD Features Next: GitOps Best Practices and Production Lessons

Last updated