gitea_CICD/service/giteaService.sh

86 lines
2.5 KiB
Bash
Executable File

#!/bin/bash
set -e # Dừng script nếu có lỗi
source=/home/joseph/tr2/gitea_CICD/service_run/giteaService.conf
while read -r line; do
[[ -z "$line" || "$line" =~ ^# ]] && continue
if [[ "$line" =~ ^[A-Za-z_][A-Za-z0-9_]*= ]]; then
varname="${line%%=*}"
varvalue="${line#*=}"
export "$varname=$varvalue"
fi
done < $source
export HOME=$PROJECT_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://##')"
# 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
# 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
# # 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
# 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"
else
echo -e "\e[41mError: Repository $GIT_REPOSITORY does not exist.\e[0m"
exit 1
fi
# 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_BIN install &&
$NODE_BIN "$indexjs_path"
else
echo -e "\e[31mFile $indexjs_path not exists\e[0m"
exit 1
fi