github-workflow.yml

GitHub Actions Workflow (Multi-Stage CI/CD)

GitHub Actions allows automation of CI/CD workflows using YAML-based configuration. Below is a detailed multi-stage workflow, including best practices and scenario-based questions.


1. Basic Multi-Stage GitHub Actions Workflow

name: CI/CD Pipeline

on:
  push:
    branches:
      - main
  pull_request:
    branches:
      - main

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout Code
        uses: actions/checkout@v4

      - name: Set up Node.js
        uses: actions/setup-node@v4
        with:
          node-version: 18

      - name: Install Dependencies
        run: npm install

      - name: Run Linter
        run: npm run lint

      - name: Run Tests
        run: npm test

  docker-build:
    runs-on: ubuntu-latest
    needs: build
    steps:
      - name: Checkout Code
        uses: actions/checkout@v4

      - name: Log in to Docker Hub
        uses: docker/login-action@v3
        with:
          username: ${{ secrets.DOCKER_USERNAME }}
          password: ${{ secrets.DOCKER_PASSWORD }}

      - name: Build and Push Docker Image
        run: |
          docker build -t mydockerhub/myapp:latest .
          docker push mydockerhub/myapp:latest

  deploy:
    runs-on: ubuntu-latest
    needs: docker-build
    environment: production
    if: github.ref == 'refs/heads/main'
    steps:
      - name: Checkout Code
        uses: actions/checkout@v4

      - name: Deploy Application
        run: ./deploy.sh

2. Breakdown of Workflow Sections

2.1 on (Triggers)

  • Defines when the workflow runs.

  • Example: Run on push to main and pull requests:

  • Best Practice: Avoid running deployments on pull_request.


2.2 Jobs & Dependencies

  • jobs define separate execution units.

  • needs ensures jobs execute in sequence.

Job
Purpose
Dependency

build

Installs dependencies, runs linter & tests

None

docker-build

Builds and pushes Docker image

build

deploy

Deploys app to production

docker-build


2.3 runs-on

  • Specifies the runner environment.

  • Options:

    • ubuntu-latest, macos-latest, windows-latest

    • Self-hosted runners for private deployments.


2.4 Docker Authentication

  • Logs in to Docker Hub securely:

  • Best Practice: Use GitHub Secrets instead of hardcoding credentials.


2.5 Conditional Deployment

  • Deploy only on main branch:

  • Best Practice: Use environments for different deployments:


3. Optimized Multi-Stage GitHub Actions Workflow


4. GitHub Actions Best Practices

Use Caching for Faster Builds

Use Matrix Builds for Parallel Execution

Use Encrypted Secrets for Credentials

Run Linter & Tests Before Deployment

Use if: for Conditional Execution


5. Scenario-Based Interview Questions

Q1: How do you prevent a deployment on pull requests?

Answer: Use if: github.ref == 'refs/heads/main'


Q2: How can you cache dependencies in GitHub Actions?

Answer: Use actions/cache


Q3: How do you securely store API keys in GitHub Actions?

Answer: Use GitHub Secrets


Q4: How do you ensure that only the main branch can deploy to production?

Answer: Use if: condition with github.ref


6. Summary

Topic
Key Takeaways

Triggers (on)

Defines when workflows run (push, pull_request)

Jobs (jobs)

Independent tasks executed in sequence or parallel

Secrets

Store credentials securely

Parallel Execution

Use matrix to test multiple environments

Docker Caching

Use actions/cache to speed up builds

Conditional Execution

Use if: to control execution


This GitHub Actions CI/CD guide covers best practices, optimizations, and interview questions! 🚀 Let me know if you need more details. 😊

Last updated