Skip to content

7. Continuous Integration

Ebenezer Emelogu edited this page Jul 19, 2024 · 5 revisions

CI Workflow Analysis

Overview

This CI workflow automates the build, test, and deployment processes for the hng_boilerplate_golang_web project, covering development, staging, and production environments.

Workflow Triggers

Development Workflow

Name: Build, Test, and Deploy for Development
Trigger: Runs on any push to the dev branch.

Staging Workflow

Name: Build, Test, and Deploy for Staging
Trigger: Runs on any push to the staging branch.

Production Workflow

Name: Build, Test, and Deploy for Production
Trigger: Runs on any push to the main branch.

Jobs

Development Workflow Jobs

1. Build Job

Environment: ubuntu-latest

Steps:

  1. Checkout Code: Checks out the repository code.
  2. Set Golang: Sets up Go version 1.22.1.
  3. Build Application: Compiles the Go application and outputs the binary as learnai_dev.

2. Test Job

Environment: ubuntu-latest, requires build job to complete

Environment Variables:

  • POSTGRES_USER: postgres
  • POSTGRES_PASSWORD: password
  • POSTGRES_DB: db_name

Services: PostgreSQL database setup

Steps:

  1. Checkout Code: Checks out the repository code.
  2. Create Config File: Copies app-sample.env to app.env.
  3. Run Application: Starts the application in the background.
  4. Wait for Application: Waits 30 seconds for the application to start.
  5. Test Reachability: Uses curl to check if the application is reachable at http://localhost:8019.
  6. Run Tests: Executes all Go tests with an extended timeout.

3. Deploy Job

Environment: ubuntu-latest, requires test job to complete

Environment Variables: Uses secrets for SSH connection and process management.

Steps:

  1. SSH and Deploy: Uses SSH to connect to the server and run deployment scripts.

Deployment Script Breakdown:

  1. Set Environment Variables: Defines paths for application root, Node.js, and Go binaries.
  2. Prepare Deployment Directory: Creates and navigates to the deployment directory.
  3. Git Operations:
    • Resets and pulls the latest changes if the Git repository exists.
    • Clones the repository if it does not exist.
  4. Build Application: Copies environment file and builds the Go application.
  5. Process Management:
    • Checks if the process is running with PM2.
    • Restarts or starts the process using PM2.

Staging Workflow Jobs

1. Build Job

Environment: ubuntu-latest

Steps:

  1. Checkout Code: Checks out the repository code.
  2. Set Golang: Sets up Go version 1.22.1.
  3. Build Application: Compiles the Go application and outputs the binary as learnai_staging.

2. Test Job

Environment: ubuntu-latest, requires build job to complete

Environment Variables:

  • POSTGRES_USER: postgres
  • POSTGRES_PASSWORD: password
  • POSTGRES_DB: db_name

Services: PostgreSQL database setup

Steps:

  1. Checkout Code: Checks out the repository code.
  2. Create Config File: Copies app-sample.env to app.env.
  3. Run Application: Starts the application in the background.
  4. Wait for Application: Waits 30 seconds for the application to start.
  5. Test Reachability: Uses curl to check if the application is reachable at http://localhost:8019.
  6. Run Tests: Executes all Go tests with an extended timeout.

3. Deploy Job

Environment: ubuntu-latest, requires test job to complete

Environment Variables: Uses secrets for SSH connection and process management.

Steps:

  1. SSH and Deploy: Uses SSH to connect to the server and run deployment scripts.

Deployment Script Breakdown:

  1. Set Environment Variables: Defines paths for application root, Node.js, and Go binaries.
  2. Prepare Deployment Directory: Creates and navigates to the deployment directory.
  3. Git Operations:
    • Resets and pulls the latest changes if the Git repository exists.
    • Clones the repository if it does not exist.
  4. Build Application: Copies environment file and builds the Go application.
  5. Process Management:
    • Checks if the process is running with PM2.
    • Restarts or starts the process using PM2.

Production Workflow Jobs

1. Build Job

Environment: ubuntu-latest

Steps:

  1. Checkout Code: Checks out the repository code.
  2. Set Golang: Sets up Go version 1.22.1.
  3. Build Application: Compiles the Go application and outputs the binary as learnai_prod.

2. Test Job

Environment: ubuntu-latest, requires build job to complete

Environment Variables:

  • POSTGRES_USER: postgres
  • POSTGRES_PASSWORD: password
  • POSTGRES_DB: db_name

Services: PostgreSQL database setup

Steps:

  1. Checkout Code: Checks out the repository code.
  2. Create Config File: Copies app-sample.env to app.env.
  3. Run Application: Starts the application in the background.
  4. Wait for Application: Waits 30 seconds for the application to start.
  5. Test Reachability: Uses curl to check if the application is reachable at http://localhost:8019.
  6. Run Tests: Executes all Go tests with an extended timeout.

3. Deploy Job

Environment: ubuntu-latest, requires test job to complete

Environment Variables: Uses secrets for SSH connection and process management.

Steps:

  1. SSH and Deploy: Uses SSH to connect to the server and run deployment scripts.

Deployment Script Breakdown:

  1. Set Environment Variables: Defines paths for application root, Node.js, and Go binaries.
  2. Prepare Deployment Directory: Creates and navigates to the deployment directory.
  3. Git Operations:
    • Resets and pulls the latest changes if the Git repository exists.
    • Clones the repository if it does not exist.
  4. Build Application: Copies environment file and builds the Go application.
  5. Process Management:
    • Checks if the process is running with PM2.
    • Restarts or starts the process using PM2.

Summary

These workflows ensure that every push to the dev, staging, and main branches triggers:

  • Building the application.
  • Running tests with PostgreSQL service.
  • Deploying the application via SSH.

By automating these steps, the workflows provide a reliable and repeatable process for development, staging, and production environments.