ServerlessBase Blog
  • Source Control Integration in CI/CD

    Learn how to integrate source control systems with CI/CD pipelines for automated builds and deployments.

    Source Control Integration in CI/CD

    You've probably spent hours manually running tests, building artifacts, and deploying to staging environments. Every time you push code, you repeat the same steps. This is where source control integration transforms your workflow from tedious repetition into automated, reliable deployments.

    Source control integration connects your version control system directly to your CI/CD pipeline, triggering builds automatically when code changes. This eliminates manual steps, reduces human error, and ensures every deployment comes from a known, tested state.

    How Source Control Triggers Work

    Modern CI/CD platforms monitor your repository for changes and react accordingly. When you push code to a branch, the platform detects the event and initiates the pipeline.

    The process typically follows these steps:

    1. Webhook Reception: Your repository sends a webhook to the CI/CD server when code is pushed
    2. Event Parsing: The CI/CD server parses the webhook payload to identify the repository, branch, and commit details
    3. Pipeline Trigger: The server creates a new pipeline run for that specific commit
    4. Artifact Retrieval: The pipeline checks out the code from the specified commit
    5. Execution: The pipeline runs through all configured stages (build, test, deploy)

    This automation happens in seconds, removing the need for manual pipeline initiation.

    Git Branching Strategies for CI/CD

    Your branching strategy determines how CI/CD pipelines interact with your codebase. Different strategies serve different development workflows.

    Feature Branch Workflow

    Developers create new branches for features, merge them into a main branch, and CI/CD pipelines validate each merge.

    # Create a feature branch
    git checkout -b feature/user-authentication
     
    # Make changes and commit
    git add .
    git commit -m "Add user authentication flow"
     
    # Push to remote
    git push origin feature/user-authentication

    When you push, the CI/CD pipeline runs automatically. If tests pass, the branch is ready for review. If tests fail, you get immediate feedback without waiting for a manual trigger.

    GitFlow Workflow

    GitFlow provides a structured approach with dedicated branches for different purposes:

    • main - Production-ready code
    • develop - Integration branch for features
    • feature/* - Individual feature branches
    • release/* - Preparation for releases
    • hotfix/* - Emergency fixes to production
    # Create a release branch
    git checkout develop
    git checkout -b release/v1.2.0
     
    # Make release preparations
    git commit -m "Prepare release v1.2.0"
     
    # Merge back to main and develop
    git checkout main
    git merge release/v1.2.0
    git checkout develop
    git merge release/v1.2.0

    CI/CD pipelines can be configured to run on release branches, ensuring release candidates are thoroughly tested before merging.

    Trunk-Based Development

    Developers work directly on the main branch with short-lived feature branches. This approach requires frequent integration and automated testing.

    # Create a feature branch
    git checkout -b feature/new-api-endpoint
     
    # Make changes
    git add .
    git commit -m "Add new API endpoint"
     
    # Create a pull request
    git push origin feature/new-api-endpoint
     
    # Merge via pull request
    # CI/CD runs tests automatically

    Trunk-based development works best with continuous integration practices, where every commit is tested and integrated frequently.

    Repository Webhooks Explained

    Webhooks are HTTP callbacks that notify your CI/CD server when events occur in your repository. Instead of the CI/CD server polling the repository, the repository pushes events to the server.

    Common Webhook Events

    EventTriggerPipeline Behavior
    pushCode pushed to branchRun pipeline for that branch
    pull_requestPull request created/updatedRun pipeline for PR branch
    tagNew tag createdRun pipeline for tag (release)
    deleteBranch/tag deletedStop running pipelines for that branch

    Webhook Payload Structure

    The webhook payload contains metadata about the event:

    {
      "repository": {
        "id": 12345,
        "name": "my-app",
        "full_name": "username/my-app",
        "default_branch": "main"
      },
      "sender": {
        "id": 67890,
        "login": "developer"
      },
      "ref": "refs/heads/feature/new-feature",
      "before": "a1b2c3d4e5f6",
      "after": "f6e5d4c3b2a1",
      "created": false,
      "deleted": false
    }

    Your CI/CD server uses this information to determine which pipeline to run and which commit to build.

    Configuring CI/CD Triggers

    Most CI/CD platforms allow you to configure triggers based on repository events. Here's how to set them up in common platforms.

    GitHub Actions

    GitHub Actions uses workflow files in .github/workflows/ to define pipelines. Triggers are specified in the on section:

    name: CI Pipeline
     
    on:
      push:
        branches: [ main, develop ]
      pull_request:
        branches: [ main ]
      workflow_dispatch:  # Manual trigger
     
    jobs:
      build:
        runs-on: ubuntu-latest
        steps:
          - uses: actions/checkout@v3
          - name: Run tests
            run: npm test
          - name: Build application
            run: npm run build

    This configuration triggers the pipeline on pushes to main or develop branches, pull requests targeting main, and allows manual execution via the GitHub UI.

    GitLab CI/CD

    GitLab uses .gitlab-ci.yml files in your repository root:

    stages:
      - build
      - test
      - deploy
     
    variables:
      NODE_ENV: test
     
    build:
      stage: build
      script:
        - npm install
        - npm run build
      artifacts:
        paths:
          - dist/
     
    test:
      stage: test
      script:
        - npm test
      dependencies:
        - build
     
    deploy:
      stage: deploy
      script:
        - npm run deploy
      only:
        - main

    GitLab automatically triggers pipelines when you push code or create merge requests. The only section restricts deployment to the main branch.

    Jenkins

    Jenkins uses pipeline scripts (either declarative or scripted) to define workflows:

    pipeline {
        agent any
     
        triggers {
            pollSCM('H/5 * * * *')  // Poll every 5 minutes
            genericTrigger(
                causeString: 'Triggered by generic webhook',
                token: 'my-webhook-token',
                tokenCredentialsId: 'webhook-token-credentials'
            )
        }
     
        stages {
            stage('Build') {
                steps {
                    sh 'npm install'
                    sh 'npm run build'
                }
            }
     
            stage('Test') {
                steps {
                    sh 'npm test'
                }
            }
        }
    }

    Jenkins can poll your repository periodically or receive webhooks for real-time triggering.

    Branch Protection Rules

    Branch protection enforces best practices by requiring certain conditions before code can be merged into protected branches.

    Common Protection Rules

    RulePurpose
    Require pull request reviewsCode review before merging
    Require status checks to passTests must pass before merge
    Require branches to be up to datePrevent merge conflicts
    Disallow force pushesMaintain commit history integrity
    Require linear historyPrevent merge commits

    Example Configuration

    # GitHub branch protection example
    branches:
      - name: main
        protection:
          required_pull_request_reviews:
            required_approving_review_count: 2
          required_status_checks:
            strict: true
            contexts:
              - test
              - build
          enforce_admins: true
          restrictions:
            users:
              - maintainer
              - senior-developer

    With these rules in place, your CI/CD pipeline becomes a gatekeeper. Code cannot be merged into main until all tests pass and at least two reviewers approve.

    Pull Request Integration

    Pull requests provide a collaborative environment where code is reviewed before merging. CI/CD pipelines run on pull request branches, giving immediate feedback to contributors.

    PR Workflow

    # Create a feature branch
    git checkout -b feature/new-feature
     
    # Make changes and commit
    git add .
    git commit -m "Add new feature"
     
    # Push to remote
    git push origin feature/new-feature
     
    # Create pull request
    # CI/CD pipeline runs automatically
    # Reviewers can comment and request changes
    # Once approved and tests pass, merge

    PR Comments and Feedback

    CI/CD platforms can post automated comments on pull requests:

    • Test results summary
    • Code coverage percentage
    • Build artifacts links
    • Deployment status

    This feedback loop helps reviewers understand the impact of proposed changes without leaving the PR interface.

    Tag-Based Releases

    Tags mark specific points in your repository's history, typically used for releases. CI/CD pipelines can be configured to run on tag creation.

    Tagging Workflow

    # Create a version tag
    git tag -a v1.2.0 -m "Release version 1.2.0"
     
    # Push the tag to remote
    git push origin v1.2.0
     
    # CI/CD pipeline runs automatically for the tag

    Release Pipeline Configuration

    # GitHub Actions example
    name: Release Pipeline
     
    on:
      push:
        tags:
          - 'v*.*.*'
     
    jobs:
      release:
        runs-on: ubuntu-latest
        steps:
          - uses: actions/checkout@v3
          - name: Create Release
            uses: actions/create-release@v1
            env:
              GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
            with:
              tag_name: ${{ github.ref }}
              release_name: Release ${{ github.ref }}
              draft: false
              prerelease: false

    Tag-based pipelines often include additional steps like building release artifacts, creating GitHub releases, and notifying stakeholders.

    Handling Merge Conflicts

    Merge conflicts disrupt CI/CD pipelines. When conflicts occur, the pipeline fails and you must resolve them before the pipeline can succeed.

    Conflict Detection

    Most CI/CD platforms detect merge conflicts during the pipeline run:

    Error: Merge conflict detected in src/components/Header.tsx
    Please resolve conflicts before continuing.

    Conflict Resolution Workflow

    # Pull latest changes
    git fetch origin main
    git pull origin main
     
    # Resolve conflicts manually
    # Edit conflicted files
    git add .
    git commit -m "Resolve merge conflicts"
     
    # Push to trigger pipeline again
    git push origin feature/new-feature

    CI/CD pipelines can be configured to skip merge conflict checks during development and enforce them only on protected branches.

    Environment-Specific Triggers

    Different environments require different deployment strategies. CI/CD pipelines can be configured to deploy to specific environments based on branch or tag.

    Environment Configuration

    # GitLab CI example
    stages:
      - build
      - test
      - deploy_staging
      - deploy_production
     
    deploy_staging:
      stage: deploy_staging
      script:
        - npm run deploy:staging
      only:
        - develop
     
    deploy_production:
      stage: deploy_production
      script:
        - npm run deploy:production
      only:
        - main
        - tags

    This configuration ensures staging deployments happen on the develop branch and production deployments happen on main or tags.

    Monitoring Pipeline Triggers

    Understanding when and why pipelines run helps you optimize your CI/CD workflow.

    Pipeline Execution Log

    Most CI/CD platforms provide detailed logs:

    Pipeline #12345 triggered by push to feature/new-feature
      Commit: a1b2c3d4e5f6 (Add new feature)
      Author: developer@example.com
      Branch: feature/new-feature
      Started: 2026-03-13 10:30:00 UTC
      Duration: 5m 23s
    
      Jobs:
        - build (passed)
        - test (passed)
        - deploy (passed)

    Trigger Analytics

    Track trigger patterns to identify optimization opportunities:

    • Most frequently triggered branches
    • Average pipeline duration per branch
    • Failed trigger frequency
    • Peak trigger times

    This data helps you adjust scheduling, optimize pipelines, and reduce unnecessary runs.

    Best Practices for Source Control Integration

    1. Use Protected Branches

    Protect your main branch to enforce code quality standards and prevent accidental deployments.

    2. Require Status Checks

    Configure pipelines as required status checks so code cannot be merged without passing tests.

    3. Use Descriptive Commit Messages

    Clear commit messages help reviewers understand changes and make debugging easier.

    4. Keep Feature Branches Short

    Short-lived branches reduce merge conflicts and make CI/CD pipelines more efficient.

    5. Test Early and Often

    Run tests on every commit to catch issues early in the development process.

    6. Use Semantic Versioning

    Tag releases with semantic versioning (v1.2.3) to maintain consistent release management.

    7. Automate Everything

    Configure webhooks and triggers to eliminate manual pipeline initiation.

    8. Monitor Pipeline Health

    Track pipeline success rates and durations to identify and address bottlenecks.

    Conclusion

    Source control integration transforms your CI/CD pipeline from a manual process into an automated, reliable system. By connecting your version control system to your deployment pipeline, you eliminate human error, reduce deployment time, and ensure consistent, tested deployments.

    The key takeaways are: use appropriate branching strategies, configure webhooks for real-time triggering, enforce branch protection rules, and monitor pipeline performance. These practices create a robust CI/CD workflow that scales with your team.

    Platforms like ServerlessBase simplify this integration by providing pre-configured pipelines and automated deployments. You can focus on writing code while the platform handles the complex orchestration of builds, tests, and deployments.

    Start by integrating source control with your CI/CD pipeline today. Your future self will thank you when deployments happen automatically and reliably.

    Leave comment