From bc9d14cbe8f001b291436bde5f76ed2d7bb98172 Mon Sep 17 00:00:00 2001 From: Joseph Le Date: Mon, 25 Sep 2023 19:58:46 +1000 Subject: [PATCH] giteaHook service --- Dockerfile | 7 +++++- docker-compose.yaml | 2 +- index.js | 22 +++++++++---------- package.json | 2 +- service/giteaHook.service | 11 ++++++++++ giteaHook.sh => service/giteaHook.sh | 7 ++++-- service/giteaService | 12 ++++++++++ service/giteaService.sh | 33 ++++++++++++++++++++++++++++ 8 files changed, 80 insertions(+), 16 deletions(-) create mode 100644 service/giteaHook.service rename giteaHook.sh => service/giteaHook.sh (90%) mode change 100644 => 100755 create mode 100755 service/giteaService create mode 100755 service/giteaService.sh diff --git a/Dockerfile b/Dockerfile index bf1293f..f858fe4 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,9 @@ FROM node:16.3.0-alpine RUN apk update +RUN apk add --no-cache --upgrade bash +#RUN addgroup -S admin && adduser -S admin -G admin WORKDIR /data -CMD npm install; npm run start \ No newline at end of file +#RUN chown -R admin:admin /data +#RUN chmod 755 /data +#USER admin +CMD npm install; npm run start diff --git a/docker-compose.yaml b/docker-compose.yaml index cbc2713..9f74ed7 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -7,4 +7,4 @@ services: - ".:/data" tty: true ports: - - '7777:8000' \ No newline at end of file + - '8000:8001' diff --git a/index.js b/index.js index 9957d23..228850d 100644 --- a/index.js +++ b/index.js @@ -3,17 +3,17 @@ const app = express(); const { exec } = require("child_process"); app.post("/git/gitea-webhook", (req, res) => { - // exec( - // "/home/giteaHook/giteaHook.sh", - // (error, stdout, stderr) => { - // if (error) { - // console.log(`Error executing command: ${error}`); - // res.status(500).send("PULL ERROR") - // } - // res.status(200).send({mess: "PULL SUCCESS!", data: stdout}) - // console.log(`Command output:\n${stdout}`); - // } - // ); + exec( + "./service/giteaHook.sh", + (error, stdout, stderr) => { + if (error) { + console.log(`Error executing command: ${error}`); + res.status(500).send("PULL ERROR") + } + res.status(200).send({mess: "PULL SUCCESS!", data: stdout}) + console.log(`Command output:\n${stdout}`); + } + ); console.log("PULL2 EE333!!"); }); diff --git a/package.json b/package.json index a252719..fc136f0 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1", - "start": "nodemon index.js" + "start": "node index.js" }, "repository": { "type": "git", diff --git a/service/giteaHook.service b/service/giteaHook.service new file mode 100644 index 0000000..b3b9a01 --- /dev/null +++ b/service/giteaHook.service @@ -0,0 +1,11 @@ +[Unit] +Description=Gitea Hook Service +After=network.target + +[Service] +ExecStart=/home/gitea_CICD/service/giteaService.sh +Restart=always +WorkingDirectory=/home/gitea_CICD/service +User=root +Group=root +EnvironmentFile=/home/gitea_CICD/service/giteaService diff --git a/giteaHook.sh b/service/giteaHook.sh old mode 100644 new mode 100755 similarity index 90% rename from giteaHook.sh rename to service/giteaHook.sh index d8ca319..f65f4cb --- a/giteaHook.sh +++ b/service/giteaHook.sh @@ -1,10 +1,13 @@ #!/bin/bash +#File chua bien moi truong +source /home/gitea_CICD/service/giteaService + #URL project -project="/home/Log_service" +project=$PROJECT_PATH #URL folder FE -fe_path="manage-view/" +fe_path=$FE_PROJECT_PATH #URL folder BE #be_path="netmiko_Api/" diff --git a/service/giteaService b/service/giteaService new file mode 100755 index 0000000..d7882b0 --- /dev/null +++ b/service/giteaService @@ -0,0 +1,12 @@ +#Duong dan thu muc, index.js file de chay dich vu giteaHook + +INDEXJS_HOOK_PATH=/home/gitea_CICD/index.js +HOOK_PATH=/home/gitea_CICD + +#Duong dan cac thu muc trong project chinh +#Co the khai bao nhieu bien hon de cau hinh giteaHook.sh + +PROJECT_PATH=/home/Log_service +FE_PROJECT_PATH=manage-view/ +BE_PROJECT_PATH=/home/Log_service + diff --git a/service/giteaService.sh b/service/giteaService.sh new file mode 100755 index 0000000..52ca235 --- /dev/null +++ b/service/giteaService.sh @@ -0,0 +1,33 @@ +#!/bin/bash + +# Đường dẫn tới tệp index.js +indexjs_path=$INDEXJS_HOOK_PATH +project_path=$HOOK_PATH + +# Kiểm tra xem Node.js đã cài đặt chưa +if ! command -v node &> /dev/null; then + echo "Node.js chưa được cài đặt. Đang cài đặt Node.js..." + curl -sL https://deb.nodesource.com/setup_16.x | sudo -E bash - + sudo apt-get install -y nodejs +else + echo "Nodejs da duoc cai dat!" && + node -v +fi + +# Kiểm tra xem npm đã cài đặt chưa +if ! command -v npm &> /dev/null; then + echo "npm chưa được cài đặt. Đang cài đặt npm..." + sudo apt-get install -y npm +else + echo "npm da duoc cai dat!" && + npm -v +fi + +# Kiểm tra xem tệp index.js có tồn tại không +if [ -f "$indexjs_path" ]; then + echo "Chạy tệp $indexjs_path..." + cd "$project_path" && + node "$indexjs_path" +else + echo "Tệp $indexjs_path không tồn tại." +fi \ No newline at end of file