Skip to content

Commit

Permalink
fixed: deployment
Browse files Browse the repository at this point in the history
  • Loading branch information
eustahc committed Jul 19, 2024
1 parent 212547c commit eb31efe
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 64 deletions.
84 changes: 52 additions & 32 deletions .github/workflows/ci.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,95 +4,115 @@
PROJECT="ZentinelJS"
REPOSITORY="https://github.com/surelle-ha/ZentinelJS"
BRANCH="main"
DIRECTORY="/var/www/html"
DEPENDENCY="wget curl git btop nodejs npm figlet xclip"
DIRECTORY="/var/www/html/$PROJECT"
DEPENDENCIES=("wget" "curl" "git" "btop" "nodejs" "npm" "figlet" "xclip")

# Log Function
log() {
echo "[$(date '+%F_%H:%M:%S')] $1"
}

# Check if a command exists
command_exists() {
command -v "$1" &> /dev/null
}

# Install dependencies
install_dependencies() {
for dep in "${DEPENDENCIES[@]}"; do
if ! command_exists $dep; then
if ! sudo apt install -y $dep; then
log "Dependency installation failed: $dep"
exit 1
fi
fi
done
}

# PREPARE
if ! sudo apt install $DEPENDENCY; then
echo "[$( date '+%F_%H:%M:%S' )] Dependency installation failed."
exit 1
fi
install_dependencies

# CHECK IF GIT AUTHENTICATED
git ls-remote $REPOSITORY &> /dev/null
if [ $? -eq 0 ]; then
echo "[$( date '+%F_%H:%M:%S' )] Git is authenticated."
if git ls-remote $REPOSITORY &> /dev/null; then
log "Git is authenticated."
else
echo "[$( date '+%F_%H:%M:%S' )] Git is not authenticated."
log "Git is not authenticated."
exit 1
fi

# DEPLOYMENT
clear
figlet $PROJECT
echo "[$( date '+%F_%H:%M:%S' )] Deployment started"
log "Deployment started"

if [ ! -d "$DIRECTORY" ]; then
mkdir -p "$DIRECTORY"
echo "[$( date '+%F_%H:%M:%S' )] Directory $DIRECTORY created."
log "Directory $DIRECTORY created."
else
echo "[$( date '+%F_%H:%M:%S' )] Directory $DIRECTORY already exists."
log "Directory $DIRECTORY already exists."
fi
cd $DIRECTORY

# IF PROJECT NOT EXIST
if [ ! -d "$PROJECT" ]; then

echo "[$( date '+%F_%H:%M:%S' )] Clone project from the repository"
log "Cloning project from the repository"
if ! git clone $REPOSITORY; then
echo "[$( date '+%F_%H:%M:%S' )] Failed to clone repository"
log "Failed to clone repository"
exit 1
fi

cd $PROJECT

echo "[$( date '+%F_%H:%M:%S' )] NPM installation started"
log "NPM installation started"
if ! npm install; then
echo "[$( date '+%F_%H:%M:%S' )] NPM install failed"
log "NPM install failed"
exit 1
fi

echo "[$( date '+%F_%H:%M:%S' )] Create environment template"
if ! cp .env.example .env; then
echo "[$( date '+%F_%H:%M:%S' )] Creation environment template failed"
exit 1
log "Creating environment template"
if [ ! -f .env ]; then
if ! cp .env.example .env; then
log "Creating environment template failed"
exit 1
fi
fi

echo "[$( date '+%F_%H:%M:%S' )] Migration started"
log "Migration started"
if ! npm run db:up; then
echo "[$( date '+%F_%H:%M:%S' )] Migration failed"
log "Migration failed"
exit 1
fi

echo "[$( date '+%F_%H:%M:%S' )] Run Application"
log "Running Application"
if ! npm run dev; then
echo "[$( date '+%F_%H:%M:%S' )] Execution failed"
log "Execution failed"
exit 1
fi


# IF PROJECT ALREADY EXIST
else

cd $PROJECT

echo "[$( date '+%F_%H:%M:%S' )] Pull new resources from the Repository"
log "Pulling new resources from the Repository"
if ! git pull origin $BRANCH; then
echo "Failed to pull latest code"
log "Failed to pull latest code"
exit 1
fi

echo "[$( date '+%F_%H:%M:%S' )] NPM installation started"
log "NPM installation started"
if ! npm install; then
echo "NPM install failed"
log "NPM install failed"
exit 1
fi

log "Database migration started"
if ! npm run db:up; then
echo "Database migration failed"
log "Database migration failed"
exit 1
fi

fi

echo "[$( date '+%F_%H:%M:%S' )] Deployment Complete"
log "Deployment Complete"
84 changes: 52 additions & 32 deletions .github/workflows/deployment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,95 +27,115 @@ jobs:
PROJECT="ZentinelJS"
REPOSITORY="https://github.com/surelle-ha/ZentinelJS"
BRANCH="main"
DIRECTORY="/var/www/html"
DEPENDENCY="wget curl git btop nodejs npm figlet xclip"
DIRECTORY="/var/www/html/$PROJECT"
DEPENDENCIES=("wget" "curl" "git" "btop" "nodejs" "npm" "figlet" "xclip")
# Log Function
log() {
echo "[$(date '+%F_%H:%M:%S')] $1"
}
# Check if a command exists
command_exists() {
command -v "$1" &> /dev/null
}
# Install dependencies
install_dependencies() {
for dep in "${DEPENDENCIES[@]}"; do
if ! command_exists $dep; then
if ! sudo apt install -y $dep; then
log "Dependency installation failed: $dep"
exit 1
fi
fi
done
}
# PREPARE
if ! sudo apt install $DEPENDENCY; then
echo "[$( date '+%F_%H:%M:%S' )] Dependency installation failed."
exit 1
fi
install_dependencies
# CHECK IF GIT AUTHENTICATED
git ls-remote $REPOSITORY &> /dev/null
if [ $? -eq 0 ]; then
echo "[$( date '+%F_%H:%M:%S' )] Git is authenticated."
if git ls-remote $REPOSITORY &> /dev/null; then
log "Git is authenticated."
else
echo "[$( date '+%F_%H:%M:%S' )] Git is not authenticated."
log "Git is not authenticated."
exit 1
fi
# DEPLOYMENT
clear
figlet $PROJECT
echo "[$( date '+%F_%H:%M:%S' )] Deployment started"
log "Deployment started"
if [ ! -d "$DIRECTORY" ]; then
mkdir -p "$DIRECTORY"
echo "[$( date '+%F_%H:%M:%S' )] Directory $DIRECTORY created."
log "Directory $DIRECTORY created."
else
echo "[$( date '+%F_%H:%M:%S' )] Directory $DIRECTORY already exists."
log "Directory $DIRECTORY already exists."
fi
cd $DIRECTORY
# IF PROJECT NOT EXIST
if [ ! -d "$PROJECT" ]; then
echo "[$( date '+%F_%H:%M:%S' )] Clone project from the repository"
log "Cloning project from the repository"
if ! git clone $REPOSITORY; then
echo "[$( date '+%F_%H:%M:%S' )] Failed to clone repository"
log "Failed to clone repository"
exit 1
fi
cd $PROJECT
echo "[$( date '+%F_%H:%M:%S' )] NPM installation started"
log "NPM installation started"
if ! npm install; then
echo "[$( date '+%F_%H:%M:%S' )] NPM install failed"
log "NPM install failed"
exit 1
fi
echo "[$( date '+%F_%H:%M:%S' )] Create environment template"
if ! cp .env.example .env; then
echo "[$( date '+%F_%H:%M:%S' )] Creation environment template failed"
exit 1
log "Creating environment template"
if [ ! -f .env ]; then
if ! cp .env.example .env; then
log "Creating environment template failed"
exit 1
fi
fi
echo "[$( date '+%F_%H:%M:%S' )] Migration started"
log "Migration started"
if ! npm run db:up; then
echo "[$( date '+%F_%H:%M:%S' )] Migration failed"
log "Migration failed"
exit 1
fi
echo "[$( date '+%F_%H:%M:%S' )] Run Application"
log "Running Application"
if ! npm run dev; then
echo "[$( date '+%F_%H:%M:%S' )] Execution failed"
log "Execution failed"
exit 1
fi
# IF PROJECT ALREADY EXIST
else
cd $PROJECT
echo "[$( date '+%F_%H:%M:%S' )] Pull new resources from the Repository"
log "Pulling new resources from the Repository"
if ! git pull origin $BRANCH; then
echo "Failed to pull latest code"
log "Failed to pull latest code"
exit 1
fi
echo "[$( date '+%F_%H:%M:%S' )] NPM installation started"
log "NPM installation started"
if ! npm install; then
echo "NPM install failed"
log "NPM install failed"
exit 1
fi
log "Database migration started"
if ! npm run db:up; then
echo "Database migration failed"
log "Database migration failed"
exit 1
fi
fi
echo "[$( date '+%F_%H:%M:%S' )] Deployment Complete"
log "Deployment Complete"

0 comments on commit eb31efe

Please sign in to comment.