diff --git a/index.js b/index.js
index aa18a5a..5a8a9f1 100644
--- a/index.js
+++ b/index.js
@@ -3,138 +3,111 @@ const express = require("express");
const app = express();
const { exec } = require("child_process");
const nodeMailer = require("nodemailer");
+
+// Read configuration file
const contentFile = fs.readFileSync("./service_run/giteaService.conf", "utf8");
-let checkStatus = "ready"
-// const checkFile = fs.readFileSync("./service_run/checkFile", "utf8");
+let checkStatus = "ready";
+
+// Get port from configuration file
const PORT = contentFile
.split("\n")
- .filter((i) => i.includes("PORT_SERVICE"))[0]
+ .find(line => line.includes("PORT_SERVICE"))
+ ?.split("=")[1]
+ .trim() || 5000;
+
+// Get email configuration from config file
+const getConfigValue = (key) => contentFile
+ .split("\n")
+ .find(line => line.includes(key))
?.split("=")[1]
.trim();
+
+// Configure email transporter
+const createTransporter = () => nodeMailer.createTransport({
+ pool: true,
+ host: "mail.apactech.io",
+ port: 465,
+ secure: true,
+ auth: {
+ user: "admin@apactech.io",
+ pass: "BGK!dyt6upd2eax1bhz",
+ },
+});
+
app.use(express.json());
app.post("/git/gitea-webhook", async (req, res) => {
let title = "";
-
- 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 FE_Path = contentFile
- .split("\n")
- .filter((i) => i.includes("FE_PROJECT_PATH"))[0]
- ?.split("=")[1]
- .trim();
-
+ const checkSendMail = getConfigValue("SEND_EMAIL");
+ const emailAddress = getConfigValue("EMAIL_ADDRESS");
const giteaEvent = req.headers["x-gitea-event"];
- const body = req.body;
- //console.log(body)
- res
- .status(200)
- .send({ mess: "The event has been received!", data: req.body });
- console.log("CHECK FILE", checkStatus);
+ // Return response immediately
+ res.status(200).send({ mess: "The event has been received!", data: req.body });
+ console.log("STATUS:", checkStatus);
+
+ // Check if there is a running process
if (checkStatus === "busy") {
- 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: "Git notifications (Fail)",
- html:
- "
There is an active build process in the " +
- FE_Path +
- " directory. Please check and push again in a few minutes.
",
- };
-
- return transporter.sendMail(options);
- } else {
- console.log("New event: " + giteaEvent);
- // fs.writeFileSync("./service_run/checkFile", "true");
- checkStatus="busy"
- exec("./service_run/giteaHook.sh", (error, stdout, stderr) => {
- let content = stdout !== "" ? stdout : stderr;
- if (error) {
- console.log("Pull failed. Please check");
- console.log(`Error executing command: ${error}`);
- title += "New Git envent: " + giteaEvent;
- } else {
- console.log(stdout);
- if (
- stderr.includes("Error") ||
- stderr.includes("failed") ||
- stdout.includes("Error") ||
- stdout.includes("failed")
- ) {
- title += "New Git envent: " + giteaEvent;
- } else {
- title += "New Git envent: " + giteaEvent + "(success)";
- }
- setTimeout(() => {
-
- checkStatus="ready"
- // fs.writeFileSync("./service_run/checkFile", "false");
-
- }, 10000);
- }
-
- 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: title,
- 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);
- }
- });
+ if (checkSendMail === "True") {
+ const transporter = createTransporter();
+ const options = {
+ from: "admin@apactech.io",
+ to: emailAddress,
+ subject: "Git notifications (Fail)",
+ html: "There is an active build process. Please check and push again in a few minutes.
",
+ };
+ return transporter.sendMail(options);
+ }
+ return;
}
+
+ // Process new event
+ console.log("New event: " + giteaEvent);
+ checkStatus = "busy";
+
+ exec("./service_run/giteaHook.sh", (error, stdout, stderr) => {
+ let content = stdout || stderr;
+
+ if (error) {
+ console.log("Pull failed. Please check");
+ console.log(`Error executing command: ${error}`);
+ title = `New Git event: ${giteaEvent}`;
+ } else {
+ console.log(stdout);
+ const hasError = [stderr, stdout].some(output =>
+ output && (output.includes("Error") || output.includes("failed"))
+ );
+
+ title = `New Git event: ${giteaEvent}${hasError ? "" : " (success)"}`;
+
+ // Reset status after 10 seconds
+ setTimeout(() => {
+ checkStatus = "ready";
+ }, 10000);
+ }
+
+ // Send notification email if configured
+ if (checkSendMail === "True") {
+ const transporter = createTransporter();
+ const options = {
+ from: "admin@apactech.io",
+ to: emailAddress,
+ subject: title,
+ html: `
+ *** ${giteaEvent} event ***
+ Committer: ${req.body.commits[0]?.committer.name || "N/A"}
+ Message: ${req.body.commits[0]?.message || "N/A"}
+ Branch: ${req.body.ref || "N/A"}
+ ${req.body.commits[0]?.url ? `Link: ${req.body.commits[0].url}` : ""}
+ Process output:
+
+ `,
+ };
+ return transporter.sendMail(options);
+ }
+ });
});
app.listen(PORT, () => {
- console.log("Service is running on port ", PORT);
+ console.log("Service is running on port", PORT);
});
diff --git a/service/giteaHook.sh b/service/giteaHook.sh
index 4a3525f..ab6d752 100755
--- a/service/giteaHook.sh
+++ b/service/giteaHook.sh
@@ -49,14 +49,14 @@ else
echo "" &&
echo "$result" &&
output=$(echo "$result" | grep "|" | awk -F "|" '{gsub(/ /, "", $1); print $1}') &&
- while IFS= read -r line; do
- if [[ $line != "" && $(find "$project" | grep "$line" | grep -q "$FE_PROJECT_PATH"; echo $?) -eq 0 ]]; then
- echo "$line ton tai" &&
- check="true"
- else
- echo "$line ko ton tai"
- fi
- done <<< "$output"
+ # while IFS= read -r line; do
+ # if [[ $line != "" && $(find "$project" | grep "$line" | grep -q "$FE_PROJECT_PATH"; echo $?) -eq 0 ]]; then
+ # echo "$line ton tai" &&
+ # check="true"
+ # else
+ # echo "$line ko ton tai"
+ # fi
+ # done <<< "$output"
# process_ids=$(ps aux | grep "$FE_PROJECT_PATH" | grep "build" | grep -v grep | awk '{print $2}') &&
#
@@ -82,25 +82,25 @@ else
#
# fi
- if [[ $check == "true" ]]; then
- echo "|--------------------------------------------------------|"
- echo "|***** THERE ARE CHANGES INSIDE FOLDER $FE_PROJECT_PATH *****|"
- echo "|--------------------------------------------------------|"
+ # if [[ $check == "true" ]]; then
+ # echo "|--------------------------------------------------------|"
+ # echo "|***** THERE ARE CHANGES INSIDE FOLDER $FE_PROJECT_PATH *****|"
+ # echo "|--------------------------------------------------------|"
- echo "|---------------|"
- echo "|*** INSTALL ***|"
- echo "|---------------|"
+ # echo "|---------------|"
+ # echo "|*** INSTALL ***|"
+ # echo "|---------------|"
- cd $FE_PROJECT_PATH &&
- npm install -f
+ # cd $FE_PROJECT_PATH &&
+ # npm install -f
- echo "|-------------|"
- echo "|*** BUILD ***|"
- echo "|-------------|"
+ # echo "|-------------|"
+ # echo "|*** BUILD ***|"
+ # echo "|-------------|"
- npm run build &&
- cp -rf $FE_PROJECT_PATH/build/* $FE_ROOT_FOLDER_PATH
-fi
+ # npm run build &&
+ # cp -rf $FE_PROJECT_PATH/build/* $FE_ROOT_FOLDER_PATH
+# fi
echo "|----------------------|"
echo "|*** UPDATE LIBRARY ***|"
diff --git a/service/giteaService.conf b/service/giteaService.conf
index 2102188..c874008 100755
--- a/service/giteaService.conf
+++ b/service/giteaService.conf
@@ -22,14 +22,6 @@ SEND_EMAIL=True
# Example: /home/My_project/
PROJECT_PATH=
-# URL of front-end directory(*)
-# Example: /home/My_project/FE_directory
-FE_PROJECT_PATH=
-
-# URL of front-end public directory (*)
-# Example: /var/www/html
-FE_ROOT_FOLDER_PATH=
-
#
# It is possible to add many other directory besides the front-end directory
#
@@ -39,6 +31,10 @@ FE_ROOT_FOLDER_PATH=
#
# Example:
+# URL of FE directory(*)
+#FE_PROCESS_PATH=/home/My_project/FE_directory
+#FE_PROCESS_PATH_COMMAND=npm install -f && npm run build && cp -rf /home/My_project/FE_directory/build/* /var/www/html
+
# URL of back-end directory(*)
#BE_PROCESS_PATH=/home/My_project/BE_directory
#BE_PROCESS_PATH_COMMAND=npm install && echo "\n" && echo "BACK_END"
diff --git a/service/giteaService.sh b/service/giteaService.sh
index ca48464..e4e1aae 100755
--- a/service/giteaService.sh
+++ b/service/giteaService.sh
@@ -18,11 +18,12 @@ 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 &&
- node -v
+ # 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
@@ -30,8 +31,8 @@ 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
+ echo -e "\e[41mNPM is not install\e[0m"
+ exit 1
else
echo -e "\e[32mnpm is installed!\e[0m" &&
npm -v
diff --git a/test.js b/test.js
new file mode 100644
index 0000000..b68f82a
--- /dev/null
+++ b/test.js
@@ -0,0 +1,33 @@
+const nodeMailer = require("nodemailer");
+
+// Configure email transporter
+const transporter = nodeMailer.createTransport({
+ pool: true,
+ host: "mail.apactech.io",
+ port: 465,
+ secure: true,
+ auth: {
+ user: "admin@apactech.io",
+ pass: "BGK!dyt6upd2eax1bhz",
+ },
+ });
+
+// const transporter = createTransporter();
+
+ const options = {
+ from: "admin@apactech.io",
+ to: "joseph@apactech.io",
+ subject: "Test",
+ html: `
+ *** event ***
+ Committer: N/A
+ Message: N/A
+ Branch: N/A
+ Process output:
+
+ `,
+ };
+
+ transporter.sendMail(options);
+
+ console.log("first")
\ No newline at end of file