GCP DevOps Project

Sprint 01

Task 3Setup Github repo according to DevOps best practice

The image contains text on a blue gradient background that reads, "Task 3: Setting up GitHub repo according to DevOps best practices."

In this guide, we'll walk through configuring your GitHub repository to align with DevOps best practices. You'll learn how to protect your main branch, enforce pull request reviews, and adopt a scalable workflow for collaborative development.

Current Workflow and Its Drawbacks

Most teams begin with a simple process:

  1. Clone the central repo.
  2. Make changes directly on main.
  3. Push updates back to main.

While straightforward, this method introduces two critical issues:

  • Unreviewed code: Bugs or security flaws can reach production unvetted.
  • Frequent merge conflicts: Multiple direct pushes to main often collide.

To solve these problems, we’ll enable branch protection and mandate pull requests.

Enabling Branch Protection Rules

Branch protection rules block direct pushes to critical branches (like main) and enforce quality checks before merging.

The image features a stack of boxes with a shield icon and the text "Branch Protection" next to it.

Key Branch Protection Settings

RuleDescriptionBenefit
Require pull request reviewsPrevents direct commits to mainEnsures code is peer-reviewed
Enforce status checksCI/CD pipelines must passAvoids broken or failing builds
Dismiss stale approvalsForces fresh reviews after changesKeeps feedback up to date

Note

Configure branch protection under Settings > Branches in your GitHub repository. For details, see GitHub Branch Protection.

With these rules enabled:

  • Direct pushes to main are blocked.
  • All changes must go through a pull request.
  • Required CI/CD checks must be green before merging.

Adopt a feature-branch workflow to scale collaboration:

  1. Clone the repository locally.
  2. Create a new feature branch: feature/your-feature-name.
  3. Commit work to the feature branch.
  4. Push the branch and open a pull request against main.
  5. Request reviews and address feedback.
  6. Merge when approvals and checks are complete.
# 1. Clone the repo
git clone https://github.com/your-org/your-repo.git
cd your-repo

# 2. Create and switch to a feature branch
git checkout -b feature/your-feature-name

# 3. Stage and commit your changes
git add .
git commit -m "Add description of your feature or fix"

# 4. Push and publish the branch
git push -u origin feature/your-feature-name

After pushing, navigate to the GitHub UI to open a pull request and assign reviewers.

The image is a flowchart illustrating a GitHub workflow, showing steps from the main branch to cloning, creating a feature branch, and making a pull request, which is then reviewed by an engineer.

Warning

Avoid emergency fixes directly on main. Even urgent patches should follow the pull request process to maintain auditability.

What’s Next?

In the next article, we’ll demonstrate how to apply branch protection rules in the GitHub settings and manage pull requests step by step.

The image shows a search bar with the query "How to enable Branch Protection?" and a copyright notice for KodeKloud.

Watch Video

Watch video content

Previous
Task 2 Clone the repo and setup our Editor