update logic check node, npm

This commit is contained in:
root 2025-05-14 11:11:38 +07:00
parent d8b569b5e8
commit 7d3f9b28d9
3 changed files with 64 additions and 37 deletions

View File

@ -1,5 +1,10 @@
#!/bin/bash
NODE_BIN=$(which node)
NPM_BIN=$(which npm)
echo $NODE_BIN
echo $NPM_BIN
if [ -d "/etc/systemd/system" ]; then
echo "|---------------------------|"
echo -e "|***** \e[33mINSTALL SERVICE\e[0m *****|"
@ -27,6 +32,12 @@ if [ -d "/etc/systemd/system" ]; then
sudo sed -i "s#HOOK_PATH=.*#HOOK_PATH=$escaped_pwd_install#" $pwd_install/service_run/giteaService.conf &&
echo -e "* \e[32mHOOK_PATH\e[0m *" &&
sudo sed -i "s#NODE_BIN=.*#NODE_BIN=$NODE_BIN#" $pwd_install/service_run/giteaService.conf &&
echo -e "* \e[32mNODE_BIN\e[0m *" &&
sudo sed -i "s#NPM_BIN=.*#NPM_BIN=$NPM_BIN#" $pwd_install/service_run/giteaService.conf &&
echo -e "* \e[32mNPM_BIN\e[0m *" &&
sudo sed -i "s#source .*#source $escaped_pwd_install/service_run/giteaService.conf#" $pwd_install/service_run/giteaHook.sh &&
echo -e "* \e[32mEXEC_FILE\e[0m *" &&
echo ""

View File

@ -2,7 +2,8 @@
# The variable is automatically filled in by the system
HOOK_PATH=/home/gitea_CICD
NODE_BIN=/path/to/node
NPM_BIN=/path/to/npm
# Git account (*)
GIT_USERNAME=<your_username>
GIT_PASSWORD=<your_password>

View File

@ -1,60 +1,75 @@
#!/bin/bash
set -e # Dừng script nếu có lỗi
source /home/phuc/gitea_CICD/service_run/giteaService.conf
export HOME=$PROJECT_PATH
# Đường dẫn tới tệp index.js
indexjs_path=$HOOK_PATH/index.js
project_path=$HOOK_PATH
indexjs_path="$HOOK_PATH/index.js"
project_path="$HOOK_PATH"
USERNAME=$(echo "$GIT_USERNAME" | sed 's/@/%40/g')
PASSWORD=$(echo "$GIT_PASSWORD" | sed 's/@/%40/g')
REPOSITORY_AUTH="https://$USERNAME:$PASSWORD@$(echo $GIT_REPOSITORY | sed 's#https://##')"
cd "$PROJECT_PATH" &&
# Debug PATH nếu cần
# echo "Current PATH: $PATH"
# # Thiết lập lại PATH nếu thiếu (thường gặp khi chạy từ systemd)
# if ! command -v node &> /dev/null; then
# echo "Node.js not found in PATH, attempting to locate..."
# NODE_PATH=$(find /usr /opt /home -type f -name "node" -executable 2>/dev/null | head -n 1)
# if [ -x "$NODE_PATH" ]; then
# export PATH=$(dirname "$NODE_PATH"):$PATH
# echo "Added $(dirname "$NODE_PATH") to PATH"
# fi
# fi
git config --global --add safe.directory $HOME &&
# if ! command -v node &> /dev/null; then
# echo -e "\e[41mNode.js is not installed or not in PATH\e[0m"
# exit 1
# else
# echo -e "\e[32mNode.js is installed!\e[0m"
# node -v
# fi
SET_ORIGIN=$(git remote set-url origin $REPOSITORY_AUTH) &&
# Ki?m tra xem Node.js đ? cài đ?t chưa
if ! command -v node &> /dev/null; then
# echo -e "\e[31mNode.js is not installed.\e[0m"
# echo -e "\e[32mInstall Node.js...\e[0m"
# curl -sL https://deb.nodesource.com/setup_16.x | sudo -E bash - &&
# sudo apt-get install nodejs -y &&
echo -e "\e[41mNodejs is not install\e[0m"
exit 1
else
echo -e "\e[32mNodejs is installed!\e[0m" &&
node -v
fi
# # Kiểm tra npm tương tự
# if ! command -v npm &> /dev/null; then
# echo "npm not found in PATH, attempting to locate..."
# NPM_PATH=$(find /usr /opt /home -type f -name "npm" -executable 2>/dev/null | head -n 1)
# if [ -x "$NPM_PATH" ]; then
# export PATH=$(dirname "$NPM_PATH"):$PATH
# echo "Added $(dirname "$NPM_PATH") to PATH"
# fi
# fi
# Ki?m tra xem npm đ? cài đ?t chưa
if ! command -v npm &> /dev/null; then
echo -e "\e[41mNPM is not install\e[0m"
exit 1
else
echo -e "\e[32mnpm is installed!\e[0m" &&
npm -v
fi
# if ! command -v npm &> /dev/null; then
# echo -e "\e[41mnpm is not installed or not in PATH\e[0m"
# exit 1
# else
# echo -e "\e[32mnpm is installed!\e[0m"
# npm -v
# fi
cd "$PROJECT_PATH"
git config --global --add safe.directory "$HOME"
git remote set-url origin "$REPOSITORY_AUTH"
if git ls-remote -q --exit-code "$REPOSITORY_AUTH" > /dev/null; then
echo -e "\e[32mOK: Repository $GIT_REPOSITORY exists\e[0m"
sleep 1
else
echo ""
echo -e "\e[41mError: Repository $GIT_REPOSITORY not exists.\e[0m"
echo ""
echo -e "\e[41mError: Repository $GIT_REPOSITORY does not exist.\e[0m"
exit 1
fi
# Ki?m tra xem t?p index.js có t?n t?i không
# Chạy index.js nếu tồn tại
if [ -f "$indexjs_path" ]; then
echo "Run file $indexjs_path..."
echo $NPM_BIN
echo $NODE_BIN
export PATH="$(dirname $NODE_BIN):$PATH"
cd "$project_path" &&
npm install &&
node "$indexjs_path"
$NPM_BIN install &&
$NODE_BIN "$indexjs_path"
else
echo -e "\e[31mFile $indexjs_path not exists\e[0m"
exit 1
fi