Skip to content

Update Program.cs

Update Program.cs #37

Workflow file for this run

name: Build and Deploy to EC2
on:
push:
branches:
- main
jobs:
build-and-deploy:
runs-on: ubuntu-latest
steps:
# Checkout the code
- name: Checkout code
uses: actions/checkout@v2
# Set up .NET Core environment
- name: Setup .NET Core
uses: actions/setup-dotnet@v1
with:
dotnet-version: '7.0.309'
# Restore and Build the project
- name: Restore and Build Project
run: |
CORE_OUTPUT_DIR="./build/Floom.Core"
# Clean up previous build
rm -rf "./build"
mkdir -p "$CORE_OUTPUT_DIR/"
# Restore dependencies
echo "Restoring dependencies for Floom.Core..."
dotnet restore "Floom.Core/Floom.Core.csproj"
# Build and publish Floom.Core project
echo "Building and publishing the project..."
dotnet publish "Floom.Core/Floom.Core.csproj" -c Release -o "$CORE_OUTPUT_DIR"
# Zip the build artifacts
echo "Zipping the build artifacts..."
cd "./build"
zip -r "../floom_core_build.zip" "Floom.Core"
cd ..
# Validate the zip file to make sure it's not zero bytes
echo "Checking if the zip file is created and not empty..."
if [ ! -s floom_core_build.zip ]; then
echo "Error: The zip file is empty or not created."
exit 1
fi
# Check the size of the zip file (should be around 50MB)
echo "Checking if the zip file size is close to 50MB..."
ZIP_FILE_SIZE=$(du -m floom_core_build.zip | cut -f1) # File size in MB
echo "Zip file size: ${ZIP_FILE_SIZE} MB"
if [ "$ZIP_FILE_SIZE" -lt 45 ] || [ "$ZIP_FILE_SIZE" -gt 55 ]; then
echo "Error: Zip file size is not within the expected range (45MB - 55MB)."
exit 1
fi
# Check the contents of the zip file to ensure it has the Floom.Core directory
echo "Checking contents of the zip file..."
unzip -l floom_core_build.zip | grep "Floom.Core/" > /dev/null
if [ $? -ne 0 ]; then
echo "Error: Floom.Core directory not found in the zip file."
exit 1
fi
echo "Zip file created successfully and contains Floom.Core directory with the correct size."
# Upload the build artifact to the remote server
- name: Upload build artifact to EC2
run: |
REMOTE_HOST="${{ secrets.REMOTE_HOST }}"
PEM_FILE="floom-gateway-pair.pem"
# Create PEM file from secret
mkdir -p Certificates
echo "${{ secrets.PEM_FILE_CONTENT }}" > Certificates/$PEM_FILE
chmod 600 Certificates/$PEM_FILE
# Disable host key checking and copy the zip file to the remote EC2 server
echo "Copying the build archive to the remote server ($REMOTE_HOST)..."
scp -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -i "Certificates/$PEM_FILE" floom_core_build.zip "$REMOTE_HOST:~/"
# Deploy on the remote server (SSH)
- name: Deploy on EC2 Server
run: |
REMOTE_HOST="${{ secrets.REMOTE_HOST }}"
REMOTE_DIR="/var/floom"
SERVICE_NAME="floom.service"
BUILD_ARCHIVE="floom_core_build.zip"
PEM_FILE="floom-gateway-pair.pem"
echo "Connecting to the remote server and deploying..."
ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -i "Certificates/$PEM_FILE" "$REMOTE_HOST" bash -c "
set -e
echo 'Stopping the service...'
sudo systemctl stop $SERVICE_NAME
echo 'Cleaning up old files...'
sudo rm -rf $REMOTE_DIR/*
echo 'Creating necessary directories...'
sudo mkdir -p $REMOTE_DIR
echo 'Unzipping the new build...'
sudo unzip -o ~/$BUILD_ARCHIVE -d $REMOTE_DIR
echo 'Deployment completed successfully. Files are unzipped into Floom.Core.'
echo 'Removing the zip file...'
rm ~/$BUILD_ARCHIVE
echo 'Starting the service...'
sudo systemctl start $SERVICE_NAME
echo 'Deployment completed successfully.'
"
# Clean up local zip file
- name: Clean up local zip file
run: |
echo "Cleaning up local zip file..."
rm floom_core_build.zip