-
Notifications
You must be signed in to change notification settings - Fork 50
7. Continuous Integration
This CI workflow automates the build, test, and deployment processes for the hng_boilerplate_golang_web
project, covering development, staging, and production environments.
Name: Build, Test, and Deploy for Development
Trigger: Runs on any push to the dev
branch.
Name: Build, Test, and Deploy for Staging
Trigger: Runs on any push to the staging
branch.
Name: Build, Test, and Deploy for Production
Trigger: Runs on any push to the main
branch.
Environment: ubuntu-latest
Steps:
- Checkout Code: Checks out the repository code.
-
Set Golang: Sets up Go version
1.22.1
. -
Build Application: Compiles the Go application and outputs the binary as
learnai_dev
.
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:
- Checkout Code: Checks out the repository code.
-
Create Config File: Copies
app-sample.env
toapp.env
. - Run Application: Starts the application in the background.
- Wait for Application: Waits 30 seconds for the application to start.
-
Test Reachability: Uses
curl
to check if the application is reachable athttp://localhost:8019
. - Run Tests: Executes all Go tests with an extended timeout.
Environment: ubuntu-latest
, requires test
job to complete
Environment Variables: Uses secrets for SSH connection and process management.
Steps:
- SSH and Deploy: Uses SSH to connect to the server and run deployment scripts.
Deployment Script Breakdown:
- Set Environment Variables: Defines paths for application root, Node.js, and Go binaries.
- Prepare Deployment Directory: Creates and navigates to the deployment directory.
-
Git Operations:
- Resets and pulls the latest changes if the Git repository exists.
- Clones the repository if it does not exist.
- Build Application: Copies environment file and builds the Go application.
-
Process Management:
- Checks if the process is running with PM2.
- Restarts or starts the process using PM2.
Environment: ubuntu-latest
Steps:
- Checkout Code: Checks out the repository code.
-
Set Golang: Sets up Go version
1.22.1
. -
Build Application: Compiles the Go application and outputs the binary as
learnai_staging
.
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:
- Checkout Code: Checks out the repository code.
-
Create Config File: Copies
app-sample.env
toapp.env
. - Run Application: Starts the application in the background.
- Wait for Application: Waits 30 seconds for the application to start.
-
Test Reachability: Uses
curl
to check if the application is reachable athttp://localhost:8019
. - Run Tests: Executes all Go tests with an extended timeout.
Environment: ubuntu-latest
, requires test
job to complete
Environment Variables: Uses secrets for SSH connection and process management.
Steps:
- SSH and Deploy: Uses SSH to connect to the server and run deployment scripts.
Deployment Script Breakdown:
- Set Environment Variables: Defines paths for application root, Node.js, and Go binaries.
- Prepare Deployment Directory: Creates and navigates to the deployment directory.
-
Git Operations:
- Resets and pulls the latest changes if the Git repository exists.
- Clones the repository if it does not exist.
- Build Application: Copies environment file and builds the Go application.
-
Process Management:
- Checks if the process is running with PM2.
- Restarts or starts the process using PM2.
Environment: ubuntu-latest
Steps:
- Checkout Code: Checks out the repository code.
-
Set Golang: Sets up Go version
1.22.1
. -
Build Application: Compiles the Go application and outputs the binary as
learnai_prod
.
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:
- Checkout Code: Checks out the repository code.
-
Create Config File: Copies
app-sample.env
toapp.env
. - Run Application: Starts the application in the background.
- Wait for Application: Waits 30 seconds for the application to start.
-
Test Reachability: Uses
curl
to check if the application is reachable athttp://localhost:8019
. - Run Tests: Executes all Go tests with an extended timeout.
Environment: ubuntu-latest
, requires test
job to complete
Environment Variables: Uses secrets for SSH connection and process management.
Steps:
- SSH and Deploy: Uses SSH to connect to the server and run deployment scripts.
Deployment Script Breakdown:
- Set Environment Variables: Defines paths for application root, Node.js, and Go binaries.
- Prepare Deployment Directory: Creates and navigates to the deployment directory.
-
Git Operations:
- Resets and pulls the latest changes if the Git repository exists.
- Clones the repository if it does not exist.
- Build Application: Copies environment file and builds the Go application.
-
Process Management:
- Checks if the process is running with PM2.
- Restarts or starts the process using PM2.
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.