Embark on a journey of knowledge! Take the quiz and earn valuable credits.
Take A QuizChallenge yourself and boost your learning! Start the quiz now to earn credits.
Take A QuizUnlock your potential! Begin the quiz, answer questions, and accumulate credits along the way.
Take A Quiz
🔍 Introduction
Now that you understand the fundamentals of CI/CD and
GitHub Actions, it's time to get hands-on.
In this chapter, you will:
By the end of this chapter, you’ll have confidence to
automate simple build, test, and deployment tasks inside GitHub!
📋 Anatomy of a GitHub
Actions Workflow
A GitHub Actions workflow is a YAML file stored
inside your repository in:
bash
.github/workflows/
🔹 Key Components
| Component | Purpose | 
| Workflow | Top-level definition
  that groups all automation tasks | 
| Event | Trigger that
  starts the workflow | 
| Job | Collection of steps to
  perform tasks | 
| Step | Individual
  command or action | 
| Runner | Machine that runs the
  jobs (GitHub-hosted or self-hosted) | 
📜 Basic Structure Example
yaml
name:
Build and Test
on:
[push]
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
    - name: Checkout code
      uses: actions/checkout@v3
    - name: Install dependencies
      run: npm install
    - name: Run tests
      run: npm test
🚀 Step-by-Step: Creating
Your First Workflow
Step 1: Create the Workflow Directory
In your repository:
bash
mkdir
-p .github/workflows
Step 2: Create a Workflow File
Create a file named ci.yml:
bash
touch
.github/workflows/ci.yml
Step 3: Write a Simple Workflow
Paste the following content into ci.yml:
yaml
name:
CI Workflow
on:
  push:
    branches:
      - main
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
    - name: Checkout repository
      uses: actions/checkout@v3
    - name: Set up Node.js
      uses: actions/setup-node@v3
      with:
        node-version: 16
    - name: Install packages
      run: npm install
    - name: Run unit tests
      run: npm test
Step 4: Push the Workflow
Commit and push the workflow file:
bash
git
add .
git
commit -m "Add CI workflow"
git
push origin main
Step 5: Monitor Workflow Execution
Go to your GitHub repository → Click on the Actions
tab → See your workflow running live!
You can view:
🔔 Understanding Workflow
Triggers
Workflows are event-driven.
🔹 Common Events
| Event | Description | 
| push | Triggered on code push | 
| pull_request | Triggered
  when a PR is opened/updated | 
| schedule | Triggered on a cron
  schedule | 
| workflow_dispatch | Manual
  trigger via GitHub UI | 
| release | Triggered when a new
  release is published | 
📋 Example: Scheduled
Workflow
yaml
on:
  schedule:
    - cron: '0 0 * * *'  # Every day at midnight UTC
📋 Example: Manual
Workflow Dispatch
yaml
on:
  workflow_dispatch:
Manually trigger this workflow from GitHub’s UI!
⚙️ Setting up Different Runners
🔹 Default GitHub-hosted
Runners
GitHub provides ready-to-use virtual machines:
| OS | Label | 
| Ubuntu Linux | ubuntu-latest | 
| Windows | windows-latest | 
| macOS | macos-latest | 
🔹 Using a Specific Runner
yaml
runs-on:
ubuntu-latest
OR
yaml
runs-on:
windows-latest
🧰 Using Pre-built Actions
GitHub Actions has a Marketplace full of reusable Actions.
Popular ones:
| Action | Purpose | 
| actions/checkout | Checkout source code | 
| actions/setup-node | Set up
  Node.js environment | 
| actions/setup-python | Set up Python environment | 
| actions/cache | Cache
  dependencies | 
| docker/build-push-action | Build and push Docker
  images | 
📋 Example: Using a
Third-Party Action
yaml
-
name: Upload Artifact
  uses: actions/upload-artifact@v3
  with:
    name: build-artifact
    path: dist/
Uploads the build folder (dist/) for later use!
🌟 Advanced Customization
Basics
Once comfortable, you can:
📋 Example: Running Jobs
in Parallel
yaml
strategy:
  matrix:
    node: [14, 16, 18]
Tests on Node 14, 16, and 18 simultaneously!
📈 Best Practices for
First GitHub Workflows
| Best Practice | Why It Matters | 
| Keep workflows
  simple initially | Easier debugging | 
| Name steps clearly | Easier to
  trace logs | 
| Use secrets, not
  hardcoded passwords | Security | 
| Fail fast | Exit early on
  errors | 
| Modularize
  workflows as they grow | Maintainability | 
🚀 Real-World Example:
Node.js Build and Deploy Workflow
yaml
name:
CI/CD
on:
  push:
    branches: [main]
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v3
    - uses: actions/setup-node@v3
      with:
        node-version: 16
    - run: npm install
    - run: npm run build
  deploy:
    needs: build
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v3
    - name: Deploy to server
      run: echo "Deploying
application..."
Explanation:
📚 Common Issues When
Starting
| Issue | Reason | Fix | 
| Workflow not
  triggering | Wrong branch or event
  config | Double-check on:
  events | 
| Workflow fails immediately | Bad YAML
  syntax | Validate YAML
  formatting | 
| Steps fail
  unexpectedly | Wrong runner
  environment | Confirm runs-on is
  correct | 
| Permission denied errors | Secrets not
  configured | Set GitHub
  Secrets properly | 
🚀 Summary: What You
Learned in Chapter 2
Congratulations! You've successfully built your first
automated workflow with GitHub Actions!
Answer: GitHub Actions is a built-in automation tool
on GitHub that allows you to build, test, and deploy code directly from your
repositories by defining workflows triggered by events like pushes, pull
requests, and schedules.
Answer: A GitHub Actions workflow consists of workflows, jobs, steps, and actions:
Answer: Workflows can be triggered by:
Answer: Yes! GitHub Actions can automate deployments
to servers, Kubernetes clusters, serverless platforms, or cloud providers like
AWS, Azure, and GCP after successful builds and tests.
Answer: GitHub provides a Secrets management
system where sensitive data (like API keys, credentials) can be stored and
injected into workflows securely without exposing them in code.
Answer: GitHub Actions supports runners on:
Answer: Caching (using actions/cache) helps store and
reuse dependencies between workflow runs, significantly reducing build times
and improving pipeline efficiency.
Answer: You can create separate jobs or workflows for
each environment and control them with conditions (e.g., branch filters like
if: github.ref == 'refs/heads/prod') or use manual approvals for deployment
jobs.
Answer: Yes! You can use matrix builds in
GitHub Actions to test your application across multiple versions (e.g., Node.js
14, 16, and 18) at the same time, improving compatibility and quality
assurance.
Answer: GitHub Actions offers free usage with limits based on your account type:
 
                Please log in to access this content. You will be redirected to the login page shortly.
Login 
                        Ready to take your education and career to the next level? Register today and join our growing community of learners and professionals.
 
                        Your experience on this site will be improved by allowing cookies. Read Cookie Policy
Your experience on this site will be improved by allowing cookies. Read Cookie Policy
Comments(0)