|
| 1 | +--- |
| 2 | +title: "Creating Your First Workflow" |
| 3 | +sidebar_label: "3. First Workflow" |
| 4 | +sidebar_position: 3 |
| 5 | +description: "Step-by-step guide to building your first automated pipeline with GitHub Actions. Learn how to set up a simple workflow that runs tests and checks your code environment every time you push to GitHub. Perfect for absolute beginners looking to get hands-on experience with CI/CD automation in their CodeHarborHub projects." |
| 6 | +tags: ["GitHub Actions", "CI/CD", "Automation", "DevOps", "CodeHarborHub"] |
| 7 | +keywords: ["GitHub Actions", "CI/CD", "Continuous Integration", "Automation", "DevOps", "CodeHarborHub", "MERN stack"] |
| 8 | +--- |
| 9 | + |
| 10 | +Welcome to the hands-on part of the **CodeHarborHub** DevOps track! If you've ever felt the stress of "I hope I didn't break anything" before pushing code, this lesson is for you. |
| 11 | + |
| 12 | +We are going to build a **CI (Continuous Integration)** workflow that automatically greets you and checks your code environment every time you push to GitHub. |
| 13 | + |
| 14 | +:::info Why This Workflow? |
| 15 | +This is a simple, beginner-friendly workflow that demonstrates the core concepts of GitHub Actions. It will help you understand how to structure your YAML files, use pre-built actions, and run shell commands in an automated environment. Plus, it's a fun way to see automation in action! |
| 16 | +::: |
| 17 | + |
| 18 | +## The "Chef" Analogy |
| 19 | + |
| 20 | +Think of a GitHub Action Workflow like a **Cooking Recipe**: |
| 21 | + |
| 22 | +| Technical Term | Recipe Equivalent | What it does | |
| 23 | +| :--- | :--- | :--- | |
| 24 | +| **Event** | Someone orders food | The trigger that starts the process. | |
| 25 | +| **Runner** | The Kitchen | The environment where the work happens. | |
| 26 | +| **Job** | The Chef | A specific worker assigned to a task. | |
| 27 | +| **Step** | A Recipe Instruction | A single action (e.g., "Boil water"). | |
| 28 | +| **Action** | A Pre-made Sauce | A reusable component that performs a common task (e.g., "Use pre-made tomato sauce"). | |
| 29 | + |
| 30 | +## Step 1: Preparing the Kitchen |
| 31 | + |
| 32 | +GitHub looks for workflows in a very specific folder. If you don't put them here, they won't run! |
| 33 | + |
| 34 | +1. Open your project in VS Code. |
| 35 | +2. Create a folder named `.github` (don't forget the dot!). |
| 36 | +3. Inside `.github`, create another folder named `workflows`. |
| 37 | +4. Inside `workflows`, create a file named `hello-world.yml`. |
| 38 | + |
| 39 | +## Step 2: Writing the YAML Code |
| 40 | + |
| 41 | +Copy and paste this code into your `hello-world.yml` file. Don't worry—we will break down exactly what each line does below. |
| 42 | + |
| 43 | +```yaml title="hello-world.yml" |
| 44 | +# The name of your automation |
| 45 | +name: CodeHarborHub First Automation |
| 46 | + |
| 47 | +# When should this run? (The Trigger) |
| 48 | +on: [push] |
| 49 | + |
| 50 | +# What should it actually do? |
| 51 | +jobs: |
| 52 | + say-hello: |
| 53 | + # Use a fresh Ubuntu Linux server provided by GitHub |
| 54 | + runs-on: ubuntu-latest |
| 55 | + |
| 56 | + steps: |
| 57 | + # Step 1: Download the code from your repo onto the runner |
| 58 | + - name: Checkout Repository |
| 59 | + uses: actions/checkout@v4 |
| 60 | + |
| 61 | + # Step 2: Run a simple terminal command |
| 62 | + - name: Greet the Developer |
| 63 | + run: echo "Hello CodeHarborHub Learner! Your automation is working! 🚀" |
| 64 | + |
| 65 | + # Step 3: Check the environment version |
| 66 | + - name: Check Node Version |
| 67 | + run: node -v |
| 68 | +``` |
| 69 | +
|
| 70 | +## Step 3: Understanding the "Why" |
| 71 | +
|
| 72 | +Let's look at the "Industrial Level" logic behind these lines: |
| 73 | +
|
| 74 | +### The `on: [push]` Trigger |
| 75 | + |
| 76 | +This tells GitHub: "The moment someone pushes code to *any* branch, start this engine." In professional settings, we often change this to `on: [pull_request]` so we only run tests when someone wants to merge code. |
| 77 | + |
| 78 | +### The `uses: actions/checkout@v4` |
| 79 | + |
| 80 | +This is a **Pre-built Action**. Imagine you are a chef, and instead of farming the wheat yourself, you just buy flour. This action "buys the flour" by automatically cloning your code into the virtual machine so the next steps can use it. |
| 81 | + |
| 82 | +### The `run:` command |
| 83 | + |
| 84 | +This is exactly like typing a command into your computer's Terminal or Command Prompt. Anything you can do in a terminal, you can do here! In a real-world scenario, this is where you would run your tests (`npm test`), build your app (`npm run build`), or deploy to a server. |
| 85 | + |
| 86 | +## Visualizing the Execution |
| 87 | + |
| 88 | +Once you push this file to GitHub, here is what happens behind the scenes: |
| 89 | + |
| 90 | +```mermaid |
| 91 | +sequenceDiagram |
| 92 | + participant U as You (Git Push) |
| 93 | + participant G as GitHub Registry |
| 94 | + participant R as Ubuntu Runner (The VM) |
| 95 | +
|
| 96 | + U->>G: Push .github/workflows/hello-world.yml |
| 97 | + G->>R: Spin up fresh Ubuntu VM |
| 98 | + R->>R: Step 1: Checkout Code |
| 99 | + R->>R: Step 2: Echo "Hello..." |
| 100 | + R->>R: Step 3: node -v |
| 101 | + R-->>G: Report "Success" (Green Checkmark ✅) |
| 102 | +``` |
| 103 | + |
| 104 | +## Step 4: Seeing it in Action |
| 105 | + |
| 106 | +1. **Commit and Push:** Run `git add .`, `git commit -m "Add first workflow"`, and `git push`. |
| 107 | +2. **Go to GitHub:** Open your repository in your browser. |
| 108 | +3. **Click the "Actions" Tab:** You will see a yellow circle (running) or a green checkmark (finished). |
| 109 | +4. **Click the Workflow:** Click on "CodeHarborHub First Automation" to see the logs. You can expand each step to see the output! |
| 110 | + |
| 111 | +## Common Mistakes for Beginners |
| 112 | + |
| 113 | + * **Indentation Matters:** YAML is very picky. If your `steps:` is not indented correctly under `jobs:`, the workflow will fail. Always use **spaces**, never tabs. |
| 114 | + * **Typing the Folder Name:** Ensure it is `.github/workflows`. If you name it `.github/workflow` (singular), it will not work. |
| 115 | + * **Case Sensitivity:** `on: push` is different from `On: Push`. Always use lowercase for keywords. |
| 116 | + |
| 117 | +:::tip Tip for Absolute Beginners |
| 118 | +Don't worry if it doesn't work the first time! Check the logs in the "Actions" tab to see what went wrong. The error messages are usually very descriptive and will guide you to the fix. This is how professional developers debug their CI/CD pipelines! |
| 119 | + |
| 120 | +In the industrial world, we use these logs to debug **MERN** applications. If your frontend build fails, the logs here will tell you exactly which line of code caused the error! It's like having a detective's magnifying glass to find the culprit in your code. |
| 121 | +::: |
0 commit comments