Created
November 29, 2024 18:19
-
-
Save Dostonlv/b1861ca1f1abcab8fd675ab0c9025ef4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: CI/CD Pipeline for Go Services with PM2 | |
on: | |
push: | |
branches: | |
- feat/ci | |
jobs: | |
build-and-deploy: | |
runs-on: ubuntu-latest | |
steps: | |
# Checkout the code | |
- name: Checkout code | |
uses: actions/checkout@v3 | |
# Setup Go version 1.22.0 #test | |
- name: Setup Go 1.22.0 | |
uses: actions/setup-go@v3 | |
with: | |
go-version: '1.22.0' | |
#export go | |
- name: Export go | |
run: | | |
export PATH=$PATH:/usr/local/go/bin | |
export GO111MODULE="on" | |
# Deploy to Server | |
- name: Install Go dependencies and tidy modules for api_gateway | |
run: | | |
go mod tidy | |
# add .env file for api_gateway | |
- name: Create .env file for api_gateway | |
run: | | |
echo "ENVIRONMENT=$ENVIRONMENT" > ./monorepo/api_gateway/.env | |
echo "LOG_LEVEL=$LOG_LEVEL" >> ./monorepo/api_gateway/.env | |
echo "BASE_SERVICE_HOST=$BASE_SERVICE_HOST" >> ./monorepo/api_gateway/.env | |
echo "BASE_SERVICE_PORT=$BASE_SERVICE_PORT" >> ./monorepo/api_gateway/.env | |
echo "HTTP_PORT=$HTTP_PORT" >> ./monorepo/api_gateway/.env | |
# add .env file for base_service | |
- name: Create .env file for base_service | |
run: | | |
echo "ENVIRONMENT=$ENVIRONMENT" > ./monorepo/base_service/.env | |
echo "LOG_LEVEL=$LOG_LEVEL" >> ./monorepo/base_service/.env | |
echo "POSTGRES_HOST=$POSTGRES_HOST" >> ./monorepo/base_service/.env | |
echo "POSTGRES_PORT=$POSTGRES_PORT" >> ./monorepo/base_service/.env | |
echo "POSTGRES_USER=$POSTGRES_USER" >> ./monorepo/base_service/.env | |
echo "POSTGRES_PASSWORD=$POSTGRES_PASSWORD" >> ./monorepo/base_service/.env | |
echo "POSTGRES_DB=$POSTGRES_DB" >> ./monorepo/base_service/.env | |
echo "RPC_PORT=$RPC_PORT" >> ./monorepo/base_service/.env | |
echo "SERVICE_NAME=$SERVICE_NAME" >> ./monorepo/base_service/.env | |
#test | |
# Build Go services (api_gateway and base_service) | |
- name: Build Go services | |
run: | | |
# Build api_gateway | |
go build -o ./monorepo/dist/api_gateway ./monorepo/api_gateway/main.go | |
# Build base_service | |
go build -o ./monorepo/dist/base_service ./monorepo/base_service/main.go | |
- name: Connect to Server | |
run: | | |
echo "$SSH_KEY" > private_key.pem | |
chmod 600 private_key.pem | |
# Add the server host to known_hosts | |
mkdir -p ~/.ssh | |
echo "Attempting to connect to host: $SSH_HOST" | |
echo "Connecting as user: $SSH_USER" | |
ssh-keyscan -H $SSH_HOST >> ~/.ssh/known_hosts | |
# Send built files to the server (adjust paths if needed) | |
scp -i private_key.pem -r * $SSH_USER@$SSH_HOST:/root/dozzer_ci_cd_backend/ | |
# Install PM2 and start services | |
ssh -i private_key.pem $SSH_USER@$SSH_HOST << 'EOF' | |
- name: Pm2 Start Services | |
run: | | |
cd /root/dozzer_ci_cd_backend | |
# Start the services with PM2 | |
pm2 stop api_gateway || true | |
pm2 delete api_gateway || true | |
pm2 start ./monorepo/api_gateway.json --name api_gateway | |
pm2 stop base_service || true | |
pm2 delete base_service || true | |
pm2 start ./monorepo/base_service.json --name base_service | |
# Save PM2 processes | |
pm2 save | |
EOF | |
shell: /usr/bin/bash -e {0} | |
env: | |
SSH_KEY: ${{ secrets.SSH_PRIVATE_KEY }} | |
SSH_HOST: ${{ secrets.SSH_HOST }} | |
SSH_USER: ${{ secrets.SSH_USER }} | |
# get variables github secrets variables | |
ENVIRONMENT : ${{ vars.ENVIRONMENT }} | |
LOG_LEVEL : ${{ vars.LOG_LEVEL }} | |
BASE_SERVICE_HOST : ${{ vars.BASE_SERVICE_HOST }} | |
BASE_SERVICE_PORT : ${{ vars.BASE_SERVICE_PORT }} | |
HTTP_PORT : ${{ vars.HTTP_PORT }} | |
POSTGRES_HOST : ${{ vars.POSTGRES_HOST }} | |
POSTGRES_PORT : ${{ vars.POSTGRES_PORT }} | |
POSTGRES_USER : ${{ vars.POSTGRES_USER }} | |
POSTGRES_PASSWORD : ${{ vars.POSTGRES_PASSWORD }} | |
POSTGRES_DB : ${{ vars.POSTGRES_DB }} | |
RPC_PORT : ${{ vars.RPC_PORT }} | |
SERVICE_NAME : ${{ vars.SERVICE_NAME }} | |
# End of file ... x | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment