Skip to content

Commit 146735e

Browse files
authored
Merge pull request #230 from codeharborhub/dev-1
added new docs for devOps
2 parents 17fdf03 + 74e7fb1 commit 146735e

File tree

6 files changed

+488
-0
lines changed

6 files changed

+488
-0
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"label": "Git & GitHub",
3+
"position": 6,
4+
"link": {
5+
"type": "generated-index",
6+
"description": "Go beyond simple commits. Learn how to use Git to automate deployments, protect your production environment, and manage infrastructure through code. Discover how to leverage GitHub's powerful features to collaborate effectively, automate workflows, and enhance your DevOps practices."
7+
}
8+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
---
2+
sidebar_position: 2
3+
title: "Branching Strategies: Organizing the Chaos"
4+
sidebar_label: "2. Branching Strategies"
5+
description: "Compare GitFlow, Trunk-Based Development, and GitHub Flow to find the best fit for your DevOps pipeline."
6+
---
7+
8+
A **Branching Strategy** is a set of rules that defines when and how code is moved from a developer's laptop to the production server. If everyone pushes directly to the `main` branch, the site will break every 5 minutes.
9+
10+
We need a "Traffic System" to ensure only tested, high-quality code reaches our users.
11+
12+
## 1. Trunk-Based Development (The High-Speed Choice)
13+
14+
In **Trunk-Based Development**, all developers work on a single branch (the "Trunk" or `main`). They make small, frequent updates and merge them multiple times a day.
15+
16+
```mermaid
17+
gitGraph
18+
commit id: "Initial"
19+
commit id: "Build"
20+
branch feature-1
21+
checkout feature-1
22+
commit id: "Fix-A"
23+
checkout main
24+
merge feature-1
25+
branch feature-2
26+
checkout feature-2
27+
commit id: "Fix-B"
28+
checkout main
29+
merge feature-2
30+
commit id: "Deploy-v1.1"
31+
```
32+
33+
* **Best for:** Senior teams with **Strong Automated Testing**.
34+
* **Why it's DevOps-friendly:** It prevents "Merge Hell" (where two people change the same file and can't fix it later) and allows for **Continuous Integration (CI)**.
35+
36+
## 2. GitFlow (The Structured Choice)
37+
38+
**GitFlow** is a more complex, "Industrial Level" strategy used by large teams. It uses specific branches for different purposes.
39+
40+
| Branch Name | Purpose |
41+
| :--- | :--- |
42+
| `main` | **Production Ready.** Only contains stable, released code. |
43+
| `develop` | **Integration.** Where features meet to be tested together. |
44+
| `feature/*` | **New Work.** Where individual tasks are built (e.g., `feature/login-ui`). |
45+
| `release/*` | **Polishing.** Final bug fixes before a big version launch. |
46+
| `hotfix/*` | **Emergency.** Quick fixes for bugs currently live on the site. |
47+
48+
### The Flow of Code:
49+
50+
$$Feature \rightarrow Develop \rightarrow Release \rightarrow Main$$
51+
52+
* **Best for:** Large teams with **Complex Release Cycles**.
53+
* **Why it's DevOps-friendly:** It provides a clear structure for managing different stages of development and allows for **Versioning** (e.g., v1.0, v1.1).
54+
55+
## 3. GitHub Flow (The Modern Standard)
56+
57+
This is the strategy we often use for **CodeHarborHub** open-source projects. It is simpler than GitFlow but safer than pure Trunk-based.
58+
59+
1. **Create a Branch:** Give it a descriptive name (e.g., `update-readme`).
60+
2. **Add Commits:** Save your work.
61+
3. **Open a Pull Request (PR):** Ask for a "Peer Review" and let the CI tests run.
62+
4. **Discuss & Review:** Other developers suggest changes.
63+
5. **Merge & Deploy:** Once the green checkmark appears, merge to `main` and deploy.
64+
65+
## Comparing the Strategies
66+
67+
| Strategy | Speed | Risk | Complexity | Recommended For |
68+
| :--- | :--- | :--- | :--- | :--- |
69+
| **Trunk-Based** | Blazing Fast | High | Low | Startups / Daily Deploys |
70+
| **GitHub Flow** | Balanced | Medium | Medium | Web Apps / SaaS |
71+
| **GitFlow** | Slower | Very Low | High | Banks / Medical Software |
72+
73+
## DevOps Best Practice: Protected Branches
74+
75+
Regardless of the strategy you choose, a DevOps engineer **never** allows direct pushes to `main`.
76+
77+
In GitHub, we use **Branch Protection Rules**:
78+
79+
* **Require a Pull Request:** No one can merge without a review.
80+
* **Require Status Checks:** The "Merge" button stays grey until the **Docker Build** and **Unit Tests** pass.
81+
* **Require Signed Commits:** Ensures the code actually came from a verified team member.
82+
83+
## Summary Checklist
84+
85+
* [x] I can explain why pushing directly to `main` is dangerous.
86+
* [x] I understand that **Trunk-Based** development requires great automated tests.
87+
* [x] I know that **GitFlow** is best for complex, versioned releases.
88+
* [x] I understand the 5-step process of **GitHub Flow**.
89+
90+
:::info Note
91+
If your team is small (2-3 people), start with **GitHub Flow**. It provides the perfect balance of speed and safety without the headache of managing 5 different branch types\!
92+
:::
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
---
2+
sidebar_position: 1
3+
title: "Git: The DevOps Backbone"
4+
sidebar_label: "1. Git for DevOps"
5+
description: "Learn why Git is the starting point for every automation pipeline and how it manages more than just source code."
6+
---
7+
8+
In your early coding days, you used Git to keep track of your `index.js` or `style.css`. In DevOps, we use Git to manage **everything**:
9+
* **Infrastructure:** Your AWS server settings (Terraform/CloudFormation).
10+
* **Configuration:** Your environment variables and API keys (Encrypted).
11+
* **Pipelines:** The scripts that build and deploy your app (GitHub Actions YAML).
12+
* **Containers:** The blueprints for your environment (Dockerfiles).
13+
14+
## 1. The "Control Tower" Analogy
15+
16+
Think of a busy airport.
17+
* The **Planes** are your application updates.
18+
* The **Runway** is your Production Server.
19+
* **Git is the Control Tower.** Nothing lands on the runway unless the Control Tower (Git) gives the green light. If a pilot (Developer) tries to land without permission, the system blocks them. If a plane crashes (A bug is deployed), the Control Tower can "Roll back" the runway to a safe state instantly.
20+
21+
## 2. Git as an Automation Trigger
22+
23+
In a manual world, you finish code and then manually upload it to a server. In a DevOps world, the moment you `git push`, a chain reaction starts.
24+
25+
```mermaid
26+
graph LR
27+
A[💻 Developer Push] --> B{🐙 GitHub}
28+
B --> C[🧪 Run Tests]
29+
C --> D[🐳 Build Docker Image]
30+
D --> E[🚀 Deploy to AWS/Azure]
31+
32+
style B fill:#f96,stroke:#333,stroke-width:2px,color:#000
33+
style C fill:#9cf,stroke:#333,stroke-width:2px,color:#000
34+
style D fill:#fc9,stroke:#333,stroke-width:2px,color:#000
35+
style E fill:#c9f,stroke:#333,stroke-width:2px,color:#000
36+
```
37+
38+
### The "Git-Flow" Logic:
39+
40+
We use specific events in Git to trigger different actions:
41+
42+
1. **Push to `feature` branch:** Triggers Unit Tests.
43+
2. **Pull Request to `main`:** Triggers Integration Tests and Peer Review.
44+
3. **Merge to `main`:** Triggers the final Deployment to the live website.
45+
46+
## 3. Audit Trails: The "Who, What, When"
47+
48+
One of the biggest requirements in "Industrial Level" DevOps is **Accountability**. If the **CodeHarborHub** website goes down at 2:00 AM, we don't guess what happened. We check the Git Log.
49+
50+
$$Change\_Log = \text{Author} + \text{Timestamp} + \text{Commit\_Hash} + \text{Diff}$$
51+
52+
Because every change to our infrastructure is committed to Git, we have a perfect "Time Machine." We can see exactly which line of code broke the server and revert it in seconds using:
53+
`git revert <commit_hash>`
54+
55+
## 4. Collaboration at Scale
56+
57+
DevOps is about breaking down silos between Developers and Operations. Git is the "Meeting Room" where this happens.
58+
59+
* **Developers** write the app code.
60+
* **Operations** write the Dockerfiles and Kubernetes manifests.
61+
* They both collaborate on the **same repository**, ensuring that the environment matches the code.
62+
63+
## Essential DevOps Git Commands
64+
65+
While you know `commit` and `push`, these are the "Power User" commands for DevOps:
66+
67+
| Command | Why DevOps use it |
68+
| :--- | :--- |
69+
| `git diff` | To see exactly what configuration changed before deploying. |
70+
| `git log --graph` | To visualize how different features are merging into production. |
71+
| `git tag -a v1.0` | To mark a specific "Release" point in time for the servers. |
72+
| `git stash` | To quickly clear the workspace when a production hotfix is needed. |
73+
| `git cherry-pick` | To grab a specific bug fix from one branch and apply it to another. |
74+
75+
## Summary Checklist
76+
77+
* [x] I understand that Git manages **Infrastructure** as well as **Code**.
78+
* [x] I know that a `git push` is often the trigger for a CI/CD pipeline.
79+
* [x] I understand how Git provides an audit trail for troubleshooting.
80+
* [x] I can explain why "Everything as Code" starts with Git.
81+
82+
:::info The "GitOps" Secret
83+
The ultimate goal of DevOps is **GitOps**. This means the state of your production server is *always* an exact reflection of what is in your Git repository. If you change a value in Git, the server changes itself automatically\!
84+
:::
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
---
2+
sidebar_position: 3
3+
title: "Git Hooks: The Automated Gatekeepers"
4+
sidebar_label: "3. Git Hooks Automation"
5+
description: "Learn how to use pre-commit and pre-push hooks to ensure only high-quality code leaves your machine."
6+
---
7+
8+
Have you ever committed code only to realize 5 minutes later that you left a `console.log` or a syntax error in the file? **Git Hooks** prevent these "silly mistakes" by running checks locally on your computer.
9+
10+
## 1. How Git Hooks Work
11+
12+
Inside every Git repository, there is a hidden folder called `.git/hooks`. This folder contains "Template" scripts for every stage of the Git lifecycle.
13+
14+
```mermaid
15+
graph TD
16+
A[Developer types: git commit] --> B{Pre-commit Hook}
17+
B -- "Fail (Bugs found)" --> C[Commit Rejected]
18+
B -- "Pass (Clean code)" --> D[Commit Successful]
19+
D --> E[Pre-push Hook]
20+
E -- "Tests Fail" --> F[Push Blocked]
21+
E -- "Tests Pass" --> G[Code sent to GitHub]
22+
```
23+
24+
### The "Gatekeeper" Logic:
25+
26+
If a script in the `hooks` folder returns an **Error**, Git will stop the action immediately. For example, if your pre-commit hook runs a linter and finds a formatting issue, it will return a non-zero exit code, and Git will reject the commit. This forces you to fix the issue before it can be saved in your Git history.
27+
28+
## 2. The Two Most Important Hooks
29+
30+
As a DevOps engineer, you will focus on these two:
31+
32+
### A. Pre-commit (The Linter)
33+
34+
This runs the moment you type `git commit` but **before** the commit message is saved.
35+
36+
* **Job:** Check for formatting (Prettier), syntax errors (ESLint), or "Secret Leaks" (like accidentally committing your AWS Password\!).
37+
* **Benefit:** Your Git history stays clean and professional.
38+
39+
### B. Pre-push (The Tester)
40+
41+
This runs when you type `git push`.
42+
43+
* **Job:** Run your **Unit Tests**.
44+
* **Benefit:** Ensures that you never break the "Main" build on GitHub because you forgot to run tests locally.
45+
46+
## The Mathematics of Prevention
47+
48+
The cost of fixing a bug increases the further it travels from the developer's laptop.
49+
50+
$$Cost\_of\_Fix = \text{Local} < \text{GitHub} < \text{Staging} < \text{Production}$$
51+
52+
By using Git Hooks, we catch errors at the **Local** stage, saving hours of debugging time and server costs.
53+
54+
## 3. Professional Tooling: "Husky"
55+
56+
Manually managing scripts in `.git/hooks` is difficult because that folder is not shared with your team. For **CodeHarborHub** projects, we use a tool called **Husky**.
57+
58+
Husky makes it easy to share hooks across your whole team via your `package.json`.
59+
60+
### How to set up Husky:
61+
62+
1. **Install:** `npm install husky --save-dev`
63+
2. **Initialize:** `npx husky install`
64+
3. **Add a Hook:**
65+
```bash
66+
npx husky add .husky/pre-commit "npm run lint"
67+
```
68+
69+
Now, every time a developer on your team tries to commit, Husky will automatically run the linter!
70+
71+
## 4. Best Practices
72+
73+
1. **Keep them Fast:** A pre-commit hook shouldn't take more than 5-10 seconds. If it's too slow, developers will get frustrated and skip it.
74+
2. **Don't Over-automate:** Use pre-commit for "Small" things (Linting) and pre-push for "Big" things (Testing).
75+
3. **Never Commit Secrets:** Use a hook like `gitleaks` to scan your code for passwords before they ever leave your machine.
76+
77+
## Summary Checklist
78+
79+
* [x] I know that Git Hooks live in the `.git/hooks` folder.
80+
* [x] I understand that a "Non-zero" exit code stops the Git action.
81+
* [x] I can explain the difference between **pre-commit** and **pre-push**.
82+
* [x] I know how to use **Husky** to share hooks with my team.
83+
84+
:::warning The "Skip" Button
85+
You can skip hooks using `git commit --no-verify`. Use this **ONLY** in extreme emergencies. If you skip hooks, you are bypassing the safety net of your project\!
86+
:::

0 commit comments

Comments
 (0)