Monday, 23 June 2025

Github 1 : Complete CI/CD Workflow # Optional: Name of the workflow


name: Complete CI/CD Workflow  # Optional: Name of the workflow

on:  # Event triggers for workflow

  push:

    branches: [main, develop]  # Trigger on push to specific branches

    paths-ignore:

      - '**.md'  # Ignore pushes to markdown files

  pull_request:

    types: [opened, synchronize, reopened]

    branches: [main]

  schedule:

    - cron: '0 2 * * 1'  # Run every Monday at 2 AM

  workflow_dispatch:  # Manual trigger with optional inputs

    inputs:

      environment:

        description: 'Target environment'

        required: true

        default: 'staging'

env:  # Global environment variables for all jobs
  NODE_ENV: production
  CACHE_NAME: ci-cache

jobs:

  build:
    name: Build and Test Application
    
    runs-on: ubuntu-latest  # GitHub-hosted runner

    strategy:
      matrix:
        node-version: [14.x, 16.x]  # Run job for each version
        os: [ubuntu-latest, windows-latest]
      fail-fast: false  # Continue other matrix runs if one fails

    defaults:
      run:
        shell: bash  # Default shell for run steps

    steps:
      - name: Checkout repository
        uses: actions/checkout@v3

      - name: Set up Node.js
        uses: actions/setup-node@v4
        with:
          node-version: ${{ matrix.node-version }}

      - name: Restore NPM cache
        uses: actions/cache@v3
        with:
          path: ~/.npm
          key: ${{ runner.os }}-node-${{ matrix.node-version }}-${{ hashFiles('**/package-lock.json') }}
          restore-keys: |
            ${{ runner.os }}-node-${{ matrix.node-version }}-

      - name: Install dependencies
        run: npm ci

      - name: Run tests
        run: npm test

      - name: Upload test results
        uses: actions/upload-artifact@v3
        with:
          name: test-results
          path: test-results/

  lint:
    name: Run Linter
    runs-on: ubuntu-latest
    needs: build  # Wait until build is successful

    steps:
      - uses: actions/checkout@v3

      - name: Setup Node
        uses: actions/setup-node@v4
        with:
          node-version: 16

      - name: Install deps
        run: npm ci

      - name: Run ESLint
        run: npm run lint

  deploy:
    name: Deploy to ${{ github.event.inputs.environment || 'staging' }}
    runs-on: ubuntu-latest
    needs: lint
    if: github.ref == 'refs/heads/main' && github.event_name == 'workflow_dispatch'

    environment:
      name: ${{ github.event.inputs.environment || 'staging' }}
      url: https://example.com/${{ github.event.inputs.environment }}

    steps:
      - uses: actions/checkout@v3

      - name: Deploy with rsync (or use AWS CLI / Helm / Terraform etc.)
        run: |
          echo "Deploying to ${{ github.event.inputs.environment }}..."
          # Add actual deployment script here

      - name: Notify Slack
        if: success()
        uses: slackapi/slack-github-action@v1.24.0
        with:
          payload: |
            {
              "text": "Deployment to ${{ github.event.inputs.environment }} was successful!"
            }
        env:
          SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK }}

  cleanup:
    name: Clean up resources
    runs-on: ubuntu-latest
    if: always()  # Run regardless of job success/failure
    needs: [build, lint, deploy]

    steps:
      - run: echo "Cleaning up temporary files"

__________________________________________________________________________________


Use Case 

Explanation 

Build and Test 

Compile and run unit tests on push 

Linting & Formatting 

Run ESLint or Prettier checks 

Container Deployment 

Build Docker image and push to ECR/DockerHub 

Serverless Deployment 

Deploy to AWS Lambda or Azure Functions 

Infrastructure as Code 

Run Terraform plan/apply on PRs 

Notification 

Send Slack/email on job failure or success 

Version Bumping 

Auto-increment version and create GitHub release 

Scheduled Jobs 

Daily security scanning or backups using CRON 


No comments:

Post a Comment