diff --git a/Dockerfile b/Dockerfile deleted file mode 100644 index f858fe4..0000000 --- a/Dockerfile +++ /dev/null @@ -1,9 +0,0 @@ -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 -#RUN chown -R admin:admin /data -#RUN chmod 755 /data -#USER admin -CMD npm install; npm run start diff --git a/README.md b/README.md index 1120f0d..587a2bf 100644 --- a/README.md +++ b/README.md @@ -1,117 +1,183 @@ -# gitea_CICD +# Gitea CICD Service -
NodeJS
+
+ CI/CD +

Automated CI/CD System for Gitea

+
-### 1) Clone project to your server +## 📝 Description -```sh - git clone https://gitea.nswteam.net/joseph/gitea_CICD.git +Gitea CICD Service is an automated CI/CD (Continuous Integration/Continuous Deployment) solution for projects hosted on Gitea. The system automatically monitors repository events (such as push, pull request) and executes corresponding commands to build, test, and deploy applications. + +### 🌟 Key Features + +- Automatic triggering on Gitea events (push, pull request) +- Support for multiple environments and technologies (Node.js, Frontend, Backend) +- Email notifications for events +- Flexible configuration through config file +- Nginx integration for webhook handling +- Runs as a systemd service for reliability + +## 🚀 Installation + +### System Requirements + +- Node.js (version 16.3.0 or higher) +- NPM +- Nginx +- Git +- Systemd (for Linux) + +### Installation Steps + +1. **Clone repository** +```bash +git clone https://gitea.nswteam.net/joseph/gitea_CICD.git +cd gitea_CICD ``` -### 2) Access the folder you just cloned +2. **Create service_run directory and configure** +```bash +mkdir service_run +cp service/giteaService.conf service_run/giteaService.conf +``` -root@root# - ```sh - cd gitea_CICD - ``` +3. **Configure environment variables** +```bash +nano service_run/giteaService.conf +``` -### 3) Check the directory path +Required environment variables: +- `GIT_USERNAME`: Gitea username +- `GIT_PASSWORD`: Gitea password +- `GIT_REPOSITORY`: Repository URL +- `GIT_BRANCH`: Branch to monitor +- `PORT_SERVICE`: Service port +- `EMAIL_ADDRESS`: Notification email +- `SEND_EMAIL`: Enable/disable email notifications (True/False) +- `RECEIVE_EMAIL_ADDRESS`: Email address to receive notifications +- `EMAIL_FROM`: Sender email address +- `EMAIL_HOST`: Email server host +- `EMAIL_PORT`: Email server port +- `EMAIL_SECURE`: Secure connection (True/False) +- `EMAIL_USER`: Email username +- `EMAIL_PASSWORD`: Email password +- `PROJECT_PATH`: Project directory path -root@root# - ```sh - pwd - ``` -- output: /gitea_CICD +4. **Configure Nginx** -### 4) Create folder service_run +Add the following configuration to your Nginx config file: +```nginx +location /git/ { + proxy_pass http://:; + proxy_http_version 1.1; + proxy_set_header X-Forwarded-Host $host; + proxy_set_header X-Forwarded-Server $host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Proto $scheme; + proxy_set_header Host $http_host; + proxy_set_header Upgrade $http_upgrade; + proxy_set_header Connection "Upgrade"; + proxy_pass_request_headers on; + proxy_max_temp_file_size 0; + proxy_connect_timeout 900; + proxy_send_timeout 900; + proxy_read_timeout 900; + proxy_buffer_size 128k; + proxy_buffers 4 256k; + proxy_busy_buffers_size 256k; + proxy_temp_file_write_size 256k; +} +``` -root@root# +Restart Nginx: +```bash +systemctl restart nginx +``` - ```sh - mkdir service_run - ``` -Copy environment file - ```sh - cp service/giteaService.conf service_run/giteaService.conf - ``` +5. **Install service** +```bash +./install.sh +``` -### 5) Fill in environment variables (\*) +## ⚙️ Configure Gitea Webhook -root@root# - ```sh - nano service_run/giteaService.conf - ``` - -### 6) Add Nginx configuration -- Open the Nginx configuration file. +1. Access your Gitea repository +2. Go to Settings > Webhooks +3. Click "Add Webhook" +4. Fill in the information: + - Target URL: `http://your-domain/git/gitea-webhook` + - Trigger on: Select events to monitor + - Branch filter: Enter branch to monitor (or '*' for all) -- Add thr following configuration content. +## 📁 Directory Structure - ``` - location /git/ { - #index index.html; - proxy_pass http://:; - proxy_http_version 1.1; - proxy_set_header X-Forwarded-Host $host; - proxy_set_header X-Forwarded-Server $host; - proxy_set_header X-Real-IP $remote_addr; - proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; - proxy_set_header X-Forwarded-Proto $scheme; - proxy_set_header Host $http_host; - proxy_set_header Upgrade $http_upgrade; - proxy_set_header Connection "Upgrade"; - proxy_pass_request_headers on; - proxy_max_temp_file_size 0; - proxy_connect_timeout 900; - proxy_send_timeout 900; - proxy_read_timeout 900; - proxy_buffer_size 128k; - proxy_buffers 4 256k; - proxy_busy_buffers_size 256k; - proxy_temp_file_write_size 256k; - } - ``` +``` +gitea_CICD/ +├── service/ # Service files directory +│ ├── giteaService.conf # Configuration template +│ ├── giteaService.sh # Main script +│ └── giteaHook.service # Systemd configuration +├── service_run/ # Runtime directory (created during installation) +├── index.js # Main webhook handler +├── install.sh # Installation script +└── README.md # Documentation +``` -- Change "IP-address" and "PORT". The "PORT" corresponds to the "PORT_SERVICE" of the "service_run/giteaService.conf" file. +## 🔧 Customize Build Process -- Restart Nginx service. - ```sh - systemctl restart nginx - ``` -### 7) Run file "install.sh" to install service +You can customize the build process by adding environment variables in `giteaService.conf`: -root@root# - ```sh - ./install.sh - ``` +```bash +# Syntax: +_PROCESS_PATH=/path/to/directory +_PROCESS_PATH_COMMAND=command1 && command2 ... -
install server
+# Example: +FE_PROCESS_PATH=/home/project/frontend +FE_PROCESS_PATH_COMMAND=npm install && npm run build -### 8) Add webhook +BE_PROCESS_PATH=/home/project/backend +BE_PROCESS_PATH_COMMAND=npm install && npm run start +``` -- Open your project on Gitea: -- Choose "Setting" -
webhook1
+## 📧 Email Notifications -- Choose "Webhooks" and "Add Webhooks" -
webhook2
+The system sends email notifications when: +- New repository events occur +- Build process succeeds/fails +- Errors occur during processing -- Fill in the information -
webhook3
+## 🔍 Monitoring and Troubleshooting -- Target URL: URL to the API that handles requests when a event is triggered (Default: your_domain/git/gitea-webhook). -- Trigger on: Event to trigger the webhook. -- Branch filter: Events will be listened on the branch you choose. ( '*' :all branch) +- View service logs: +```bash +journalctl -u giteaHook -f +``` -#### ** Note: ** +- Check service status: +```bash +systemctl status giteaHook +``` -- Project gitea_CICD should be placed at the same folder level as your project +## 📝 Important Notes - ``` - ├── your_project - └── gitea_CICD project - ``` +1. The `gitea_CICD` project should be placed at the same level as the project to be monitored +2. Ensure appropriate file and directory permissions +3. Verify environment variables before running the service +4. Use HTTPS for webhooks to ensure security -- Depending on the technology your project uses, the "gitea_CICD/service_run/giteaHook.sh" file will be custom configured to match the technology. +## 👨‍💻 Author -

**____ Joseph Le____ **

+**Joseph Le** - [GitHub](https://github.com/joseph) + +## 📄 License + +This project is licensed under the ISC License. + +--- + +
+ Built with ❤️ by Joseph Le +
diff --git a/abc.txt b/abc.txt deleted file mode 100644 index e69de29..0000000 diff --git a/docker-compose.yaml b/docker-compose.yaml deleted file mode 100644 index 9f74ed7..0000000 --- a/docker-compose.yaml +++ /dev/null @@ -1,10 +0,0 @@ -version: '3' -services: - giteahook: - build: . - container_name: giteahook - volumes: - - ".:/data" - tty: true - ports: - - '8000:8001' diff --git a/index.js b/index.js index 5a8a9f1..cc8dada 100644 --- a/index.js +++ b/index.js @@ -25,12 +25,12 @@ const getConfigValue = (key) => contentFile // Configure email transporter const createTransporter = () => nodeMailer.createTransport({ pool: true, - host: "mail.apactech.io", - port: 465, - secure: true, + host: getConfigValue("EMAIL_HOST") || "smtp.gmail.com", + port: parseInt(getConfigValue("EMAIL_PORT") || "587"), + secure: getConfigValue("EMAIL_SECURE") === "True", auth: { - user: "admin@apactech.io", - pass: "BGK!dyt6upd2eax1bhz", + user: getConfigValue("EMAIL_USER") || "git@notification.com", + pass: getConfigValue("EMAIL_PASSWORD") || "123456789", }, }); @@ -39,7 +39,8 @@ app.use(express.json()); app.post("/git/gitea-webhook", async (req, res) => { let title = ""; const checkSendMail = getConfigValue("SEND_EMAIL"); - const emailAddress = getConfigValue("EMAIL_ADDRESS"); + const emailAddress = getConfigValue("RECEIVE_EMAIL_ADDRESS"); + const emailFrom = getConfigValue("EMAIL_FROM") || "git@notification.com"; const giteaEvent = req.headers["x-gitea-event"]; // Return response immediately @@ -51,7 +52,7 @@ app.post("/git/gitea-webhook", async (req, res) => { if (checkSendMail === "True") { const transporter = createTransporter(); const options = { - from: "admin@apactech.io", + from: emailFrom, to: emailAddress, subject: "Git notifications (Fail)", html: "

There is an active build process. Please check and push again in a few minutes.

", @@ -90,7 +91,7 @@ app.post("/git/gitea-webhook", async (req, res) => { if (checkSendMail === "True") { const transporter = createTransporter(); const options = { - from: "admin@apactech.io", + from: emailFrom, to: emailAddress, subject: title, html: ` diff --git a/service/giteaService.conf b/service/giteaService.conf index e7b9500..d965124 100755 --- a/service/giteaService.conf +++ b/service/giteaService.conf @@ -15,10 +15,15 @@ GIT_BRANCH= PORT_SERVICE=5000 # Email: Used to send notifications when the repository has updates (*) -EMAIL_ADDRESS= # Enable/Disable send mail: True or False (*) -SEND_EMAIL=True - +SEND_EMAIL=False +RECEIVE_EMAIL_ADDRESS= +EMAIL_FROM= +EMAIL_HOST= +EMAIL_PORT= +EMAIL_SECURE=True +EMAIL_USER= +EMAIL_PASSWORD= # URL of project directory (*) # Example: /home/My_project/ PROJECT_PATH= diff --git a/test.js b/test.js deleted file mode 100644 index b68f82a..0000000 --- a/test.js +++ /dev/null @@ -1,33 +0,0 @@ -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