From 7f892158e128969bdc296f5c3a791d6bc4feb909 Mon Sep 17 00:00:00 2001 From: Joseph Le Date: Wed, 11 Oct 2023 15:37:55 +1100 Subject: [PATCH] update command, auto fill, folder_temp, .gitignore --- index.js | 138 ++++++++++-------- install.sh | 187 +++++++++++-------------- node_modules/.package-lock.json | 2 +- service/giteaHook.service | 8 +- service/giteaHook.sh | 62 +++++--- service/giteaService | 27 ---- service/giteaService.conf | 42 ++++++ service_temp/.gitignore | 1 + service_temp/giteaHook.service | 13 ++ service_temp/giteaHook.sh | 110 +++++++++++++++ service_temp/giteaService.conf | 42 ++++++ service_temp/giteaService.sh | 36 +++++ service_temp/service/giteaHook.service | 13 ++ service_temp/service/giteaHook.sh | 110 +++++++++++++++ service_temp/service/giteaService.conf | 42 ++++++ service_temp/service/giteaService.sh | 36 +++++ 16 files changed, 659 insertions(+), 210 deletions(-) delete mode 100755 service/giteaService create mode 100755 service/giteaService.conf create mode 100644 service_temp/.gitignore create mode 100644 service_temp/giteaHook.service create mode 100755 service_temp/giteaHook.sh create mode 100755 service_temp/giteaService.conf create mode 100755 service_temp/giteaService.sh create mode 100644 service_temp/service/giteaHook.service create mode 100755 service_temp/service/giteaHook.sh create mode 100755 service_temp/service/giteaService.conf create mode 100755 service_temp/service/giteaService.sh diff --git a/index.js b/index.js index f3a54a6..4837637 100644 --- a/index.js +++ b/index.js @@ -1,59 +1,81 @@ - const fs = require("fs"); - const express = require("express"); - const app = express(); - const { exec } = require("child_process"); - const nodeMailer = require("nodemailer"); - - app.use(express.json()); - - app.post("/git/gitea-webhook", async(req, res) => { - const contentFile = await fs.readFileSync("./service/giteaService", "utf8"); - const checkSendMail = contentFile.split("\n").filter((i)=>i.includes("SEND_EMAIL"))[0]?.split("=")[1].trim() - const emailAddress = contentFile.split("\n").filter((i)=>i.includes("EMAIL_ADDRESS"))[0]?.split("=")[1].trim() - const giteaEvent = req.headers["x-gitea-event"]; - console.log("New event: "+giteaEvent) - const body = req.body - //console.log(body) - res.status(200).send({mess: "The event has been received!", data:req.body}) - - exec( - "./service/giteaHook.sh", - (error, stdout, stderr) => { - if (error) { - console.log(`Error executing command: ${error}`); -// res.status(500).send("PULL ERROR") - } - console.log(stdout) +const fs = require("fs"); +const express = require("express"); +const app = express(); +const { exec } = require("child_process"); +const nodeMailer = require("nodemailer"); - if(checkSendMail==="True"){ - const transporter = nodeMailer.createTransport({ - - pool: true, - host: "mail.ipsupply.com.au", - port: 465, - secure: true, - auth: { - user: "admin@apactech.io", - pass: "BGK!dyt6upd2eax1bhz", - }, - }); - - const options = { - from: "admin@apactech.io", - to: emailAddress, - subject: "New Git envent: "+giteaEvent , - html: "

*** "+giteaEvent+" event ***

Committer: "+req.body.commits[0]?.committer.name+"

Message: "+req.body.commits[0]?.message+"

Branch: "+req.body.ref+"

Link: "+req.body.commits[0]?.url+"

Process output:

" - }; - - return transporter.sendMail(options); - } - }); - - }); - - const PORT = 5000; - app.listen(PORT, () => { - console.log("Server is running on port ", PORT); - }); +app.use(express.json()); + +app.post("/git/gitea-webhook", async (req, res) => { + const contentFile = await fs.readFileSync( + "./service_temp/giteaService.conf", + "utf8" + ); + const checkSendMail = contentFile + .split("\n") + .filter((i) => i.includes("SEND_EMAIL"))[0] + ?.split("=")[1] + .trim(); + const emailAddress = contentFile + .split("\n") + .filter((i) => i.includes("EMAIL_ADDRESS"))[0] + ?.split("=")[1] + .trim(); + const giteaEvent = req.headers["x-gitea-event"]; + console.log("New event: " + giteaEvent); + const body = req.body; + //console.log(body) + res + .status(200) + .send({ mess: "The event has been received!", data: req.body }); + + exec("./service_temp/giteaHook.sh", (error, stdout, stderr) => { + if (error) { + console.log(`Error executing command: ${error}`); + // res.status(500).send("PULL ERROR") + } + console.log(stdout); + + if (checkSendMail === "True") { + const transporter = nodeMailer.createTransport({ + pool: true, + host: "mail.ipsupply.com.au", + port: 465, + secure: true, + auth: { + user: "admin@apactech.io", + pass: "BGK!dyt6upd2eax1bhz", + }, + }); + + const options = { + from: "admin@apactech.io", + to: emailAddress, + subject: "New Git envent: " + giteaEvent, + html: + "

*** " + + giteaEvent + + " event ***

Committer: " + + req.body.commits[0]?.committer.name + + "

Message: " + + req.body.commits[0]?.message + + "

Branch: " + + req.body.ref + + "

Link: " + + req.body.commits[0]?.url + + "

Process output:

", + }; + + return transporter.sendMail(options); + } + }); +}); + +const PORT = 5000; +app.listen(PORT, () => { + console.log("Server is running on port ", PORT); +}); diff --git a/install.sh b/install.sh index 02cd988..41b9b40 100755 --- a/install.sh +++ b/install.sh @@ -1,109 +1,90 @@ #!/bin/bash - if [ -d "/etc/systemd/system" ]; then - echo "|---------------------------|" - echo -e "|***** \e[33mINSTALL SERVICE\e[0m *****|" - echo "|---------------------------|" - - sleep 2 && - - pwd_install=$(pwd) && - escaped_pwd_install=$(printf '%s\n' "$pwd_install" | sed -e 's/[\/&]/\\&/g') && - - echo "" - echo "|------------------------------|" - echo -e "|***** \e[33mUPDATE ENVIRONMENT\e[0m *****|" - echo "|------------------------------|" - echo "" - - sleep 2 && - - sudo sed -i "s#ExecStart=.*#ExecStart=$escaped_pwd_install/service/giteaService.sh#" $pwd_install/service/giteaHook.service && - echo -e "* \e[32mExecStart\e[0m *" && - sleep 1 && - - sudo sed -i "s#WorkingDirectory=.*#WorkingDirectory=$escaped_pwd_install/service#" $pwd_install/service/giteaHook.service && - echo -e "* \e[32mWorkingDirectory\e[0m *" && - sleep 1 && - - sudo sed -i "s#EnvironmentFile=.*#EnvironmentFile=$escaped_pwd_install/service/giteaService#" $pwd_install/service/giteaHook.service && - echo -e "* \e[32mEnvironmentFile\e[0m *" && - sleep 1 && - - sudo sed -i "s#HOOK_PATH=.*#HOOK_PATH=$escaped_pwd_install#" $pwd_install/service/giteaService && - echo -e "* \e[32mHOOK_PATH\e[0m *" && - sleep 1 && - - sudo sed -i "s#source .*#source $escaped_pwd_install/service/giteaService#" $pwd_install/service/giteaHook.sh && - echo -e "* \e[32mEXEC_FILE\e[0m *" && - echo "" - sleep 1 && - - systemctl stop giteaHook.service - - input_file="$pwd_install/service/giteaService" && - - #Doc va xu ly tung dong - while IFS= read -r line; do - # Kiem tra xem d?ng c ch?a "_PATH" khng - if [[ $line == *"_PATH"* ]]; then - # Lay string sau dau "=" - path="${line#*=}" - # Loai bo khoang trong trong duong dan - path=$(echo "$path" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//') - # Kiem tra duong dan ton tai - if [ ! -e "$path" ]; then - echo "" - echo -e "\e[41mError: $path not exists.\e[0m" - echo "" - exit 1 # D?ng chng tr?nh - else - echo -e "\e[32mOK: $path exists\e[0m" - sleep 1 - fi - fi - done < "$input_file" && - - cp $pwd_install/service/giteaHook.service /etc/systemd/system/giteaHook.service && - echo "" && - echo -e "* \e[32mCopy service file\e[0m *" && - - systemctl daemon-reload && - - echo "" && - echo "|----------------------|" && - echo -e "|*** \e[33mENABLE SERVICE\e[0m ***|" && - echo "|----------------------|" && - echo "" && - - systemctl enable giteaHook && - - sleep 2 && - - echo "|---------------------|" && - echo -e "|*** \e[33mSTART SERVICE\e[0m ***|" && - echo "|---------------------|" && - echo "" && - - systemctl start giteaHook && - - sleep 2 && - - echo "|------------|" && - echo -e "|*** \e[33mDONE\e[0m ***|" && - echo "|------------|" && - echo "" && - - sleep 1 && - - echo "|--------------|" && - echo -e "|** \e[33mGOODLUCK\e[0m **|" && - echo "|--------------|" && - echo "" && - - journalctl -u giteaHook -f + echo "|---------------------------|" + echo -e "|***** \e[33mINSTALL SERVICE\e[0m *****|" + echo "|---------------------------|" + + sleep 2 && + pwd_install=$(pwd) && + escaped_pwd_install=$(printf '%s\n' "$pwd_install" | sed -e 's/[\/&]/\\&/g') && + echo "" + echo "|------------------------------|" + echo -e "|***** \e[33mUPDATE ENVIRONMENT\e[0m *****|" + echo "|------------------------------|" + echo "" + cp -rf service service_temp && + + sleep 2 && + sudo sed -i "s#ExecStart=.*#ExecStart=$escaped_pwd_install/service_temp/giteaService.sh#" $pwd_install/service_temp/giteaHook.service && + echo -e "* \e[32mExecStart\e[0m *" && + sleep 1 && + sudo sed -i "s#WorkingDirectory=.*#WorkingDirectory=$escaped_pwd_install/service_temp#" $pwd_install/service_temp/giteaHook.service && + echo -e "* \e[32mWorkingDirectory\e[0m *" && + sleep 1 && + sudo sed -i "s#EnvironmentFile=.*#EnvironmentFile=$escaped_pwd_install/service_temp/giteaService.conf#" $pwd_install/service_temp/giteaHook.service && + echo -e "* \e[32mEnvironmentFile\e[0m *" && + sleep 1 && + sudo sed -i "s#HOOK_PATH=.*#HOOK_PATH=$escaped_pwd_install#" $pwd_install/service_temp/giteaService.conf && + echo -e "* \e[32mHOOK_PATH\e[0m *" && + sleep 1 && + sudo sed -i "s#source .*#source $escaped_pwd_install/service_temp/giteaService.conf#" $pwd_install/service_temp/giteaHook.sh && + echo -e "* \e[32mEXEC_FILE\e[0m *" && + echo "" + sleep 1 && + systemctl stop giteaHook.service + + input_file="$pwd_install/service_temp/giteaService.conf" && + + #Doc va xu ly tung dong + while IFS= read -r line; do + # Kiem tra xem dong c� chua "_PATH" kh�ng + if [[ $line == *"_PATH"* && $line != "#"* && $line != *"_COMMAND"* ]]; then + # Lay string sau dau "=" + path="${line#*=}" + # Loai bo khoang trong trong duong dan + path=$(echo "$path" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//') + # Kiem tra duong dan ton tai + if [ ! -e "$path" ]; then + echo "" + echo -e "\e[41mError: $path not exists.\e[0m" + echo "" + exit 1 # Dung chuong trinh + else + echo -e "\e[32mOK: $path exists\e[0m" + sleep 1 + fi + fi + done <"$input_file" && + cp $pwd_install/service_temp/giteaHook.service /etc/systemd/system/giteaHook.service && + echo "" && + echo -e "* \e[32mCopy service file\e[0m *" && + systemctl daemon-reload && + echo "" && + echo "|----------------------|" && + echo -e "|*** \e[33mENABLE SERVICE\e[0m ***|" && + echo "|----------------------|" && + echo "" && + systemctl enable giteaHook && + sleep 2 && + echo "|---------------------|" && + echo -e "|*** \e[33mSTART SERVICE\e[0m ***|" && + echo "|---------------------|" && + echo "" && + systemctl start giteaHook && + sleep 2 && + echo "|------------|" && + echo -e "|*** \e[33mDONE\e[0m ***|" && + echo "|------------|" && + echo "" && + sleep 1 && + echo "|--------------|" && + echo -e "|** \e[33mGOODLUCK\e[0m **|" && + echo "|--------------|" && + echo "" && + journalctl -u giteaHook -f + else - echo "/etc/systemd/system directory does not exist. Please check!" + echo "/etc/systemd/system directory does not exist. Please check!" fi diff --git a/node_modules/.package-lock.json b/node_modules/.package-lock.json index 994f489..48cc82a 100644 --- a/node_modules/.package-lock.json +++ b/node_modules/.package-lock.json @@ -1,7 +1,7 @@ { "name": "gitea_cicd", "version": "1.0.0", - "lockfileVersion": 2, + "lockfileVersion": 3, "requires": true, "packages": { "node_modules/abbrev": { diff --git a/service/giteaHook.service b/service/giteaHook.service index 5c4137b..c296215 100644 --- a/service/giteaHook.service +++ b/service/giteaHook.service @@ -3,13 +3,11 @@ Description=Gitea Hook Service After=network.target [Service] -ExecStart=/home/joseph/gitea_CICD/service/giteaService.sh +ExecStart=/home/gitea_CICD/service/giteaService.sh Restart=always -WorkingDirectory=/home/joseph/gitea_CICD/service -User=root -Group=root +WorkingDirectory=/home/gitea_CICD/service Restart=on-failure -EnvironmentFile=/home/joseph/gitea_CICD/service/giteaService +EnvironmentFile=/home/gitea_CICD/service/giteaService.conf [Install] WantedBy=multi-user.target diff --git a/service/giteaHook.sh b/service/giteaHook.sh index 11ff4a7..649bdd0 100755 --- a/service/giteaHook.sh +++ b/service/giteaHook.sh @@ -1,7 +1,7 @@ #!/bin/bash #File chua bien moi truong -source /home/joseph/gitea_CICD/service/giteaService +source /home/gitea_CICD/service/giteaService.conf #URL project project=$PROJECT_PATH @@ -10,10 +10,7 @@ project=$PROJECT_PATH fe_path=$(basename "$FE_PROJECT_PATH") #URL folder BE -be_path=$BE_PROJECT_PATH - -#URL other folder -#other="..." +be_path=$BE_PROCESS_PATH #Username git username=$GIT_USERNAME @@ -26,6 +23,21 @@ branch=$GIT_BRANCH cd $project +function run_commands() { + local name_path_folder="$1" + local command_name=$(echo "${name_path_folder}_COMMAND") + #echo $command_name && + # Tìm command line + command_line=$(grep "$command_name" "$input_file") && + command="${command_line#*=}" && + echo "" && + echo "$command" && + echo "" && + cd $path && + echo -e $(eval "$command") + echo "" +} + result=$(expect -c " set timeout 10 spawn git pull origin $GIT_BRANCH @@ -44,37 +56,55 @@ result=$(expect -c " catch wait result exit [lindex \$result 3] ") && - -echo "$result" && - -if [[ $result == *$fe_path* ]]; then + echo "$result" && + if [[ $result == *$fe_path* ]]; then echo "|--------------------------------------------------------|" echo "|***** THERE ARE CHANGES INSIDE FOLDER $fe_path *****|" echo "|--------------------------------------------------------|" - + echo "|---------------|" echo "|*** INSTALL ***|" echo "|---------------|" cd $project/$fe_path && - npm install + npm install echo "|-------------|" echo "|*** BUILD ***|" echo "|-------------|" npm run build && - cp -rf $project/$fe_path/build/* $FE_ROOT_FOLDER_PATH -fi + cp -rf $project/$fe_path/build/* $FE_ROOT_FOLDER_PATH + fi echo "|----------------------|" echo "|*** UPDATE LIBRARY ***|" echo "|----------------------|" - cd $be_path && - npm install - sleep 10 +input_file="$HOOK_PATH/service_temp/giteaService.conf" && + # Doc va xu ly tung dong + while IFS= read -r line; do + # Kiem tra xem dong có chua "_PATH" không + if [[ $line == *"_PROCESS_PATH"* && $line != "#"* && $line != *"_COMMAND"* ]]; then + IFS="=" read -r variable value <<<"$line" + variable=$(echo "$variable" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//') + path=$(echo "$value" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//') + + # Loai bo khoang trong trong duong dan + path=$(echo "$path" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//') + # Kiem tra duong dan ton tai + if [ ! -e "$path" ]; then + echo "" + echo -e "\e[41mError: $path not exists.\e[0m" + echo "" + exit 1 # Dung chuong trinh + else + echo -e "\e[32mProcessing in\e[0m: $path" + run_commands "$variable" + fi + fi + done <"$input_file" && echo "|------------|" echo "|*** DONE ***|" echo "|------------|" diff --git a/service/giteaService b/service/giteaService deleted file mode 100755 index b487909..0000000 --- a/service/giteaService +++ /dev/null @@ -1,27 +0,0 @@ - -#Luu y: (*) La nhung bien moi truong can khai bao - -#Duong dan thu muc, index.js file de chay dich vu giteaHook (cac bien do he thong tu dong cap nhat) -HOOK_PATH=/home/joseph/gitea_CICD - -#Tai khoan git (*) -GIT_USERNAME=joseph.le@apactech.io -GIT_PASSWORD=Maypjtkh0ng -GIT_BRANCH=dev - -#Email -EMAIL_ADDRESS=joseph@apactech.io -#Send mail: True or False -SEND_EMAIL=False - -#Duong dan goc cua project (*) -PROJECT_PATH=/home/joseph/Demo_Git_Webhook - -#Duong dan folder FE (*) -FE_PROJECT_PATH=/home/joseph/Demo_Git_Webhook/website/ - -#Duong dan folder BE (*) -BE_PROJECT_PATH=/home/joseph/Demo_Git_Webhook/server - -#Duong dan folder public FE (*) -FE_ROOT_FOLDER_PATH=/var/www/html diff --git a/service/giteaService.conf b/service/giteaService.conf new file mode 100755 index 0000000..dac44aa --- /dev/null +++ b/service/giteaService.conf @@ -0,0 +1,42 @@ +# Luu y: (*) La nhung bien moi truong can khai bao + +# Duong dan thu muc, index.js file de chay dich vu giteaHook (cac bien do he thong tu dong cap nhat) +HOOK_PATH=/home/gitea_CICD + +# Tai khoan git (*) +GIT_USERNAME=joseph.le@apactech.io +GIT_PASSWORD=Maypjtkh0ng +GIT_BRANCH=main + +# Email (*) +EMAIL_ADDRESS=joseph@apactech.io +# Send mail: True or False (*) +SEND_EMAIL=True + +# Duong dan goc cua project (*) +PROJECT_PATH=/home/Log_service + +# Duong dan folder FE (*) +FE_PROJECT_PATH=/home/Log_service/manage-view + +# Duong dan folder public FE (*) +FE_ROOT_FOLDER_PATH=/var/www/html + +# +# Cac duong dan khac can thuc thi khi pull. Co the them nhieu duong dan voi cu phap khai bao: +# +# Duong dan: _PROCESS_PATH= +# Cau lenh thuc thi: _PROCESS_PATH_COMMAND= && ... +# + +# Duong dan folder BE (*) +BE_PROCESS_PATH=/home/Log_service +BE_PROCESS_PATH_COMMAND=npm install && echo "\n" && echo "BACK_END" + +# Duong dan folder Socket (*) +SOCKET_PROCESS_PATH=/home/Log_service +SOCKET_PROCESS_PATH_COMMAND=npm install && echo "\n" && echo "SOCKET" + +# Duong dan folder khac (*) +KHAC_PROCESS_PATH=/home/Log_service +KHAC_PROCESS_PATH_COMMAND=npm install && echo "\n" && echo "KHAC" diff --git a/service_temp/.gitignore b/service_temp/.gitignore new file mode 100644 index 0000000..9c558e3 --- /dev/null +++ b/service_temp/.gitignore @@ -0,0 +1 @@ +. diff --git a/service_temp/giteaHook.service b/service_temp/giteaHook.service new file mode 100644 index 0000000..fe333bb --- /dev/null +++ b/service_temp/giteaHook.service @@ -0,0 +1,13 @@ +[Unit] +Description=Gitea Hook Service +After=network.target + +[Service] +ExecStart=/home/gitea_CICD/service_temp/giteaService.sh +Restart=always +WorkingDirectory=/home/gitea_CICD/service_temp +Restart=on-failure +EnvironmentFile=/home/gitea_CICD/service_temp/giteaService.conf + +[Install] +WantedBy=multi-user.target diff --git a/service_temp/giteaHook.sh b/service_temp/giteaHook.sh new file mode 100755 index 0000000..4877a3b --- /dev/null +++ b/service_temp/giteaHook.sh @@ -0,0 +1,110 @@ +#!/bin/bash + +#File chua bien moi truong +source /home/gitea_CICD/service_temp/giteaService.conf + +#URL project +project=$PROJECT_PATH + +#URL folder FE +fe_path=$(basename "$FE_PROJECT_PATH") + +#URL folder BE +be_path=$BE_PROCESS_PATH + +#Username git +username=$GIT_USERNAME + +#Password git +password=$GIT_PASSWORD + +#Git branch +branch=$GIT_BRANCH + +cd $project + +function run_commands() { + local name_path_folder="$1" + local command_name=$(echo "${name_path_folder}_COMMAND") + #echo $command_name && + # Tìm command line + command_line=$(grep "$command_name" "$input_file") && + command="${command_line#*=}" && + echo "" && + echo "$command" && + echo "" && + cd $path && + echo -e $(eval "$command") + echo "" +} + +result=$(expect -c " + set timeout 10 + spawn git pull origin $GIT_BRANCH + expect { + \"Username for *\" { + send \"$username\r\" + exp_continue + } + \"Password for *\" { + sleep 2 + send \"$password\r\r\" + exp_continue + } + eof + } + catch wait result + exit [lindex \$result 3] +") && + echo "$result" && + if [[ $result == *$fe_path* ]]; then + echo "|--------------------------------------------------------|" + echo "|***** THERE ARE CHANGES INSIDE FOLDER $fe_path *****|" + echo "|--------------------------------------------------------|" + + echo "|---------------|" + echo "|*** INSTALL ***|" + echo "|---------------|" + + cd $project/$fe_path && + npm install + + echo "|-------------|" + echo "|*** BUILD ***|" + echo "|-------------|" + + npm run build && + cp -rf $project/$fe_path/build/* $FE_ROOT_FOLDER_PATH + fi + +echo "|----------------------|" +echo "|*** UPDATE LIBRARY ***|" +echo "|----------------------|" + +input_file="$HOOK_PATH/service_temp/giteaService.conf" && + + # Doc va xu ly tung dong + while IFS= read -r line; do + # Kiem tra xem dong có chua "_PATH" không + if [[ $line == *"_PROCESS_PATH"* && $line != "#"* && $line != *"_COMMAND"* ]]; then + IFS="=" read -r variable value <<<"$line" + variable=$(echo "$variable" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//') + path=$(echo "$value" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//') + + # Loai bo khoang trong trong duong dan + path=$(echo "$path" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//') + # Kiem tra duong dan ton tai + if [ ! -e "$path" ]; then + echo "" + echo -e "\e[41mError: $path not exists.\e[0m" + echo "" + exit 1 # Dung chuong trinh + else + echo -e "\e[32mProcessing in\e[0m: $path" + run_commands "$variable" + fi + fi + done <"$input_file" && +echo "|------------|" +echo "|*** DONE ***|" +echo "|------------|" diff --git a/service_temp/giteaService.conf b/service_temp/giteaService.conf new file mode 100755 index 0000000..dac44aa --- /dev/null +++ b/service_temp/giteaService.conf @@ -0,0 +1,42 @@ +# Luu y: (*) La nhung bien moi truong can khai bao + +# Duong dan thu muc, index.js file de chay dich vu giteaHook (cac bien do he thong tu dong cap nhat) +HOOK_PATH=/home/gitea_CICD + +# Tai khoan git (*) +GIT_USERNAME=joseph.le@apactech.io +GIT_PASSWORD=Maypjtkh0ng +GIT_BRANCH=main + +# Email (*) +EMAIL_ADDRESS=joseph@apactech.io +# Send mail: True or False (*) +SEND_EMAIL=True + +# Duong dan goc cua project (*) +PROJECT_PATH=/home/Log_service + +# Duong dan folder FE (*) +FE_PROJECT_PATH=/home/Log_service/manage-view + +# Duong dan folder public FE (*) +FE_ROOT_FOLDER_PATH=/var/www/html + +# +# Cac duong dan khac can thuc thi khi pull. Co the them nhieu duong dan voi cu phap khai bao: +# +# Duong dan: _PROCESS_PATH= +# Cau lenh thuc thi: _PROCESS_PATH_COMMAND= && ... +# + +# Duong dan folder BE (*) +BE_PROCESS_PATH=/home/Log_service +BE_PROCESS_PATH_COMMAND=npm install && echo "\n" && echo "BACK_END" + +# Duong dan folder Socket (*) +SOCKET_PROCESS_PATH=/home/Log_service +SOCKET_PROCESS_PATH_COMMAND=npm install && echo "\n" && echo "SOCKET" + +# Duong dan folder khac (*) +KHAC_PROCESS_PATH=/home/Log_service +KHAC_PROCESS_PATH_COMMAND=npm install && echo "\n" && echo "KHAC" diff --git a/service_temp/giteaService.sh b/service_temp/giteaService.sh new file mode 100755 index 0000000..4e4cedd --- /dev/null +++ b/service_temp/giteaService.sh @@ -0,0 +1,36 @@ +#!/bin/bash + +# Đường dẫn tới tệp index.js +indexjs_path=$HOOK_PATH/index.js +project_path=$HOOK_PATH + +# 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 && + node -v +else + echo -e "\e[32mNodejs is installed!\e[0m" && + node -v +fi + +# Kiểm tra xem npm đã cài đặt chưa +if ! command -v npm &> /dev/null; then + echo -e "\e[31mnpm is not installed. Install npm...\e[0m" + sudo apt-get install npm -y +else + echo -e "\e[32mnpm is installed!\e[0m" && + npm -v +fi + +# Kiểm tra xem tệp index.js có tồn tại không +if [ -f "$indexjs_path" ]; then + echo "Run file $indexjs_path..." + cd "$project_path" && + npm install && + node "$indexjs_path" +else + echo -e "\e[31mFile $indexjs_path not exists\e[0m" +fi diff --git a/service_temp/service/giteaHook.service b/service_temp/service/giteaHook.service new file mode 100644 index 0000000..c296215 --- /dev/null +++ b/service_temp/service/giteaHook.service @@ -0,0 +1,13 @@ +[Unit] +Description=Gitea Hook Service +After=network.target + +[Service] +ExecStart=/home/gitea_CICD/service/giteaService.sh +Restart=always +WorkingDirectory=/home/gitea_CICD/service +Restart=on-failure +EnvironmentFile=/home/gitea_CICD/service/giteaService.conf + +[Install] +WantedBy=multi-user.target diff --git a/service_temp/service/giteaHook.sh b/service_temp/service/giteaHook.sh new file mode 100755 index 0000000..649bdd0 --- /dev/null +++ b/service_temp/service/giteaHook.sh @@ -0,0 +1,110 @@ +#!/bin/bash + +#File chua bien moi truong +source /home/gitea_CICD/service/giteaService.conf + +#URL project +project=$PROJECT_PATH + +#URL folder FE +fe_path=$(basename "$FE_PROJECT_PATH") + +#URL folder BE +be_path=$BE_PROCESS_PATH + +#Username git +username=$GIT_USERNAME + +#Password git +password=$GIT_PASSWORD + +#Git branch +branch=$GIT_BRANCH + +cd $project + +function run_commands() { + local name_path_folder="$1" + local command_name=$(echo "${name_path_folder}_COMMAND") + #echo $command_name && + # Tìm command line + command_line=$(grep "$command_name" "$input_file") && + command="${command_line#*=}" && + echo "" && + echo "$command" && + echo "" && + cd $path && + echo -e $(eval "$command") + echo "" +} + +result=$(expect -c " + set timeout 10 + spawn git pull origin $GIT_BRANCH + expect { + \"Username for *\" { + send \"$username\r\" + exp_continue + } + \"Password for *\" { + sleep 2 + send \"$password\r\r\" + exp_continue + } + eof + } + catch wait result + exit [lindex \$result 3] +") && + echo "$result" && + if [[ $result == *$fe_path* ]]; then + echo "|--------------------------------------------------------|" + echo "|***** THERE ARE CHANGES INSIDE FOLDER $fe_path *****|" + echo "|--------------------------------------------------------|" + + echo "|---------------|" + echo "|*** INSTALL ***|" + echo "|---------------|" + + cd $project/$fe_path && + npm install + + echo "|-------------|" + echo "|*** BUILD ***|" + echo "|-------------|" + + npm run build && + cp -rf $project/$fe_path/build/* $FE_ROOT_FOLDER_PATH + fi + +echo "|----------------------|" +echo "|*** UPDATE LIBRARY ***|" +echo "|----------------------|" + +input_file="$HOOK_PATH/service_temp/giteaService.conf" && + + # Doc va xu ly tung dong + while IFS= read -r line; do + # Kiem tra xem dong có chua "_PATH" không + if [[ $line == *"_PROCESS_PATH"* && $line != "#"* && $line != *"_COMMAND"* ]]; then + IFS="=" read -r variable value <<<"$line" + variable=$(echo "$variable" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//') + path=$(echo "$value" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//') + + # Loai bo khoang trong trong duong dan + path=$(echo "$path" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//') + # Kiem tra duong dan ton tai + if [ ! -e "$path" ]; then + echo "" + echo -e "\e[41mError: $path not exists.\e[0m" + echo "" + exit 1 # Dung chuong trinh + else + echo -e "\e[32mProcessing in\e[0m: $path" + run_commands "$variable" + fi + fi + done <"$input_file" && +echo "|------------|" +echo "|*** DONE ***|" +echo "|------------|" diff --git a/service_temp/service/giteaService.conf b/service_temp/service/giteaService.conf new file mode 100755 index 0000000..dac44aa --- /dev/null +++ b/service_temp/service/giteaService.conf @@ -0,0 +1,42 @@ +# Luu y: (*) La nhung bien moi truong can khai bao + +# Duong dan thu muc, index.js file de chay dich vu giteaHook (cac bien do he thong tu dong cap nhat) +HOOK_PATH=/home/gitea_CICD + +# Tai khoan git (*) +GIT_USERNAME=joseph.le@apactech.io +GIT_PASSWORD=Maypjtkh0ng +GIT_BRANCH=main + +# Email (*) +EMAIL_ADDRESS=joseph@apactech.io +# Send mail: True or False (*) +SEND_EMAIL=True + +# Duong dan goc cua project (*) +PROJECT_PATH=/home/Log_service + +# Duong dan folder FE (*) +FE_PROJECT_PATH=/home/Log_service/manage-view + +# Duong dan folder public FE (*) +FE_ROOT_FOLDER_PATH=/var/www/html + +# +# Cac duong dan khac can thuc thi khi pull. Co the them nhieu duong dan voi cu phap khai bao: +# +# Duong dan: _PROCESS_PATH= +# Cau lenh thuc thi: _PROCESS_PATH_COMMAND= && ... +# + +# Duong dan folder BE (*) +BE_PROCESS_PATH=/home/Log_service +BE_PROCESS_PATH_COMMAND=npm install && echo "\n" && echo "BACK_END" + +# Duong dan folder Socket (*) +SOCKET_PROCESS_PATH=/home/Log_service +SOCKET_PROCESS_PATH_COMMAND=npm install && echo "\n" && echo "SOCKET" + +# Duong dan folder khac (*) +KHAC_PROCESS_PATH=/home/Log_service +KHAC_PROCESS_PATH_COMMAND=npm install && echo "\n" && echo "KHAC" diff --git a/service_temp/service/giteaService.sh b/service_temp/service/giteaService.sh new file mode 100755 index 0000000..4e4cedd --- /dev/null +++ b/service_temp/service/giteaService.sh @@ -0,0 +1,36 @@ +#!/bin/bash + +# Đường dẫn tới tệp index.js +indexjs_path=$HOOK_PATH/index.js +project_path=$HOOK_PATH + +# 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 && + node -v +else + echo -e "\e[32mNodejs is installed!\e[0m" && + node -v +fi + +# Kiểm tra xem npm đã cài đặt chưa +if ! command -v npm &> /dev/null; then + echo -e "\e[31mnpm is not installed. Install npm...\e[0m" + sudo apt-get install npm -y +else + echo -e "\e[32mnpm is installed!\e[0m" && + npm -v +fi + +# Kiểm tra xem tệp index.js có tồn tại không +if [ -f "$indexjs_path" ]; then + echo "Run file $indexjs_path..." + cd "$project_path" && + npm install && + node "$indexjs_path" +else + echo -e "\e[31mFile $indexjs_path not exists\e[0m" +fi