Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
The diff you're trying to view is too large. We only load the first 3000 changed files.
458 changes: 458 additions & 0 deletions .github/workflows/ci.yml

Large diffs are not rendered by default.

6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,8 @@
* [Networking & SSH](./week3_practice/Networking%20&%20SSH.md)

# Part 4
* [Advanced Git & GitHub](./week4/github_practice.md)
* [Advanced Git & GitHub](./week4/github_practice.md)

# Part 5
* [CI/CD with GitHub Action](./week5/CICD_with_GitHub_Actions.md)

91 changes: 91 additions & 0 deletions week5/CICD_with_GitHub_Actions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
# CI/CD with GitHub Actions

## Questions and Answers

### What is a GitHub Action?

A **GitHub Action** is a custom application that performs a frequently repeated task in your software development workflow. GitHub Actions are the individual units of functionality that can be combined to create workflows. They can be:

- **Pre-built actions** from the GitHub Marketplace (created by GitHub or the community)
- **Custom actions** that you create yourself
- **Docker container actions** or **JavaScript actions**

Actions can perform tasks like:
- Building and testing code
- Deploying applications
- Sending notifications
- Interacting with APIs
- Managing issues and pull requests

**Where do GitHub Actions run?**
GitHub Actions run on **GitHub-hosted runners** (virtual machines in the cloud), not on your local machine. GitHub provides runners with different operating systems:
- **Ubuntu Linux** (most common)
- **Windows Server**
- **macOS**

You can also use **self-hosted runners** if you need to run actions on your own infrastructure for security, performance, or specific environment requirements.

### What is the difference between a job and a step?

**Job:**
- A **job** is a set of steps that execute on the same runner (virtual machine)
- Jobs run in parallel by default (unless dependencies are specified)
- Each job runs in a fresh virtual environment
- Jobs can have dependencies on other jobs using the `needs` keyword
- Example: You might have separate jobs for "build", "test", and "deploy"

**Step:**
- A **step** is an individual task within a job
- Steps run sequentially within a job
- Each step can run commands or use actions
- Steps share the same runner environment and file system
- Steps can pass data between each other within the same job

**Hierarchy:**
```
Workflow
├── Job 1
│ ├── Step 1
│ ├── Step 2
│ └── Step 3
└── Job 2
├── Step 1
└── Step 2
```

### What triggers a workflow?

GitHub workflows can be triggered by various **events**:

**1. Push Events:**
- `push` - When code is pushed to specific branches
- `pull_request` - When a pull request is opened, updated, or closed

**2. Scheduled Events:**
- `schedule` - Run workflows on a schedule using cron syntax
- Example: `cron: '0 0 * * *'` (daily at midnight)

**3. Manual Events:**
- `workflow_dispatch` - Manually trigger workflows from GitHub UI
- `repository_dispatch` - Trigger via API calls

**4. Repository Events:**
- `issues` - When issues are created, edited, etc.
- `release` - When releases are published
- `fork` - When the repository is forked

**5. External Events:**
- `webhook` - External webhooks
- API calls to trigger workflows

**Example trigger configuration:**
```yaml
on:
push:
branches: [ main, develop ]
pull_request:
branches: [ main ]
schedule:
- cron: '0 2 * * 1' # Every Monday at 2 AM
workflow_dispatch: # Manual trigger
```
168 changes: 168 additions & 0 deletions week5/DISCORD_SETUP.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
# Discord Webhook Setup for CI/CD Notifications

This guide explains how to set up Discord webhook notifications for your GitHub Actions CI/CD pipeline.

## 🔧 Setting Up Discord Webhook

### Step 1: Create a Discord Webhook

1. Open Discord and navigate to your server
2. Right-click on the channel where you want notifications
3. Select **Edit Channel** → **Integrations** → **Webhooks**
4. Click **Create Webhook**
5. Give it a name like "CI/CD Notifications"
6. Copy the webhook URL

### Step 2: Add Webhook to GitHub Secrets

1. Go to your GitHub repository
2. Navigate to **Settings** → **Secrets and variables** → **Actions**
3. Click **New repository secret**
4. Name: `DISCORD_WEBHOOK_URL`
5. Value: Paste your Discord webhook URL
6. Click **Add secret**

## 📋 Notification Features

The enhanced CI/CD pipeline now includes:

### 🎯 Individual Job Notifications
- **Success notifications** for each job completion (backend/frontend)
- **Failure notifications** with direct links to logs
- **Matrix strategy support** - separate notifications for each Node.js version

### 📊 Rich Embed Information
Each notification includes:
- Job name and status
- Node.js version being tested
- Repository and branch information
- Commit hash with clickable link
- Timestamp
- Direct links to workflow logs (for failures)

### 🏆 Deployment Summary
- Final summary notification after all jobs complete
- Overall status (success/failure)
- Individual job status breakdown
- Links to full workflow run

## 🎨 Notification Types

### ✅ Success Notifications
- **Green color** (3066993)
- Checkmark emoji
- Job completion timestamp
- All relevant metadata

### ❌ Failure Notifications
- **Red color** (15158332)
- X emoji
- Direct link to workflow logs
- Debugging information

### 📈 Summary Notifications
- **Color based on overall status**
- Complete pipeline overview
- Individual job results
- Workflow run links

## 🔄 Workflow Structure

```
CI Pipeline
├── backend-tests (Node.js 16, 18)
│ ├── Run tests
│ ├── Upload artifacts
│ ├── Validate deployment
│ └── Send Discord notification
├── frontend-tests (Node.js 16, 18)
│ ├── Run tests
│ ├── Upload artifacts
│ ├── Validate deployment
│ └── Send Discord notification
└── deployment-summary
└── Send overall status to Discord
```

## 🧪 Testing the Setup

1. **Push a commit** to trigger the workflow
2. **Check Discord** for notifications
3. **Verify all matrix jobs** send individual notifications
4. **Confirm summary** notification appears after all jobs complete

## 🛠️ Customization Options

### Modify Notification Content
Edit the JSON payload in the workflow file to customize:
- Embed titles and descriptions
- Colors and emojis
- Field information
- Links and formatting

### Add More Notification Triggers
You can add notifications for:
- Deployment starts
- Specific test failures
- Performance metrics
- Security scan results

### Channel Routing
Create multiple webhooks for different channels:
- Success notifications → #ci-success
- Failure notifications → #ci-alerts
- Summary notifications → #deployments

## 🔍 Troubleshooting

### Common Issues

1. **Webhook URL not working**
- Verify the URL is correct in GitHub secrets
- Check Discord webhook is still active

2. **Notifications not appearing**
- Check workflow logs for curl errors
- Verify JSON syntax in webhook payload

3. **Missing information in notifications**
- Ensure all GitHub context variables are available
- Check matrix strategy is properly configured

### Debug Commands

Test webhook manually:
```bash
curl -H "Content-Type: application/json" \
-X POST \
-d '{"content": "Test notification"}' \
YOUR_WEBHOOK_URL
```

## 📝 Example Notification

Here's what a successful backend test notification looks like:

```
✅ Backend Tests Passed
Backend tests for Node.js 18 completed successfully

Job: backend-tests
Node.js Version: 18
Repository: username/repo-name
Branch: main
Commit: abc123def (linked)
Duration: Job completed at 2024-01-15T10:30:00Z

GitHub Actions
```

## 🚀 Next Steps

With Discord notifications set up, you now have:
- Real-time CI/CD status updates
- Quick access to failure logs
- Complete pipeline visibility
- Team collaboration improvements

The pipeline will automatically notify your Discord channel for every push, pull request, or manual workflow trigger!
Loading