Day 2 : Understanding GitHub Actions: Example Scenario CI/CD Pipeline for a Web Application

Day 2 : Understanding GitHub Actions: Example Scenario CI/CD Pipeline for a Web Application

ยท

3 min read

Suppose we have a web application hosted on GitHub, and we want to create a CI/CD pipeline using GitHub Actions. Our pipeline will consist of the following steps:

  1. Linting and Testing: Check the code for syntax errors and run unit tests.

  2. Building: Compile the code and prepare it for deployment.

  3. Deployment: Deploy the application to a staging environment for testing.

Example Workflow File (explained with terminology):

name: CI/CD Pipeline

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

jobs:
  lint_test:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout code
        uses: actions/checkout@v2

      - name: Install dependencies
        run: npm install

      - name: Linting
        run: npm run lint

      - name: Testing
        run: npm test

  build:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout code
        uses: actions/checkout@v2

      - name: Build
        run: npm run build

  deploy:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout code
        uses: actions/checkout@v2

      - name: Deploy to staging
        uses: actions/aws/ecs-deploy@v1
        with:
          region: us-east-1
          cluster: staging-cluster
          service: web-app

Explanation of Workflow Components:

  1. Events:

    • on: Specifies the events that trigger the workflow.

      • push: Triggers the workflow when code is pushed to the main branch.

      • pull_request: Triggers the workflow when a pull request is opened or updated against the main branch.

  2. Jobs:

    • Define units of work within the workflow.

    • Three jobs: lint_test, build, and deploy.

    • Each job runs in isolation and can be configured separately.

  3. Steps:

    • steps: Contains a list of tasks to be executed in a job.

    • Each step performs a specific action or command.

    • Examples include checking out code (actions/checkout@v2), installing dependencies, linting, testing, building, and deploying.

  4. Runners:

    • runs-on: Specifies the execution environment for each job.

    • In this example, all jobs run on ubuntu-latest hosted runners provided by GitHub.

Workflow Execution:

  1. When code is pushed to the main branch or a pull request is created, the workflow is triggered.

  2. The lint_test job checks out the code, installs dependencies, performs linting, and runs tests.

  3. If the linting and testing pass, the build job is triggered, which checks out the code and builds the application.

  4. Finally, if the build is successful, the deploy job is triggered, deploying the application to a staging environment using AWS ECS.

Conclusion:

In this example, we've seen how each component of a GitHub Actions workflow file contributes to the automation of a CI/CD pipeline for a web application. By understanding events, jobs, steps, and runners, developers can define sophisticated workflows to streamline development processes and improve software quality and delivery.

GitHub Actions offers a powerful platform for automating repetitive tasks and orchestrating complex workflows, enabling teams to build, test, and deploy software efficiently and reliably.

Start leveraging GitHub Actions today to optimize your development workflow and accelerate your project delivery!

This example demonstrates the practical application of GitHub Actions terminology in a real-world scenario, illustrating how each component works together to achieve automation and streamline the software development lifecycle.

Did you find this article valuable?

Support AyushDabhi by becoming a sponsor. Any amount is appreciated!

ย