update source variables

This commit is contained in:
root 2025-05-14 15:53:23 +07:00
parent 8d732a72e9
commit 979105c08c
7 changed files with 171 additions and 151 deletions

View File

@ -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

242
README.md
View File

@ -1,117 +1,183 @@
# gitea_CICD
# Gitea CICD Service
<div align="center"><img src="https://upload.wikimedia.org/wikipedia/commons/d/d9/Node.js_logo.svg" alt="NodeJS" width="300"></div>
<div align="center">
<img src="https://static-00.iconduck.com/assets.00/ci-cd-line-icon-512x511-3m4br5fx.png" alt="CI/CD" width="200">
<h3>Automated CI/CD System for Gitea</h3>
</div>
### 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: <path>/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://<IP-address>:<PORT>;
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://<IP-address>:<PORT>;
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:
<name>_PROCESS_PATH=/path/to/directory
<name>_PROCESS_PATH_COMMAND=command1 && command2 ...
<div align="center"><img src="https://i.ibb.co/VJHhb3y/install-Service.png" alt="install server" width="600"></div>
# 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"
<div align="center"><img src="https://i.ibb.co/QHsST4D/image.png" alt="webhook1" width="800"></div>
## 📧 Email Notifications
- Choose "Webhooks" and "Add Webhooks"
<div align="center"><img src="https://i.ibb.co/bKFpvbV/image.png" alt="webhook2" width="800"></div>
The system sends email notifications when:
- New repository events occur
- Build process succeeds/fails
- Errors occur during processing
- Fill in the information
<div align="center"><img src="https://i.ibb.co/GJ55RDr/image.png" alt="webhook3" width="800"></div>
## 🔍 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
<div align="center"><h4><i>**____ Joseph Le____ **</i></h4></div>
**Joseph Le** - [GitHub](https://github.com/joseph)
## 📄 License
This project is licensed under the ISC License.
---
<div align="center">
<sub>Built with ❤️ by Joseph Le</sub>
</div>

View File

View File

@ -1,10 +0,0 @@
version: '3'
services:
giteahook:
build: .
container_name: giteahook
volumes:
- ".:/data"
tty: true
ports:
- '8000:8001'

View File

@ -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: "<h3>There is an active build process. Please check and push again in a few minutes.</h3>",
@ -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: `

View File

@ -15,10 +15,15 @@ GIT_BRANCH=<your_branch>
PORT_SERVICE=5000
# Email: Used to send notifications when the repository has updates (*)
EMAIL_ADDRESS=<youremail>
# Enable/Disable send mail: True or False (*)
SEND_EMAIL=True
SEND_EMAIL=False
RECEIVE_EMAIL_ADDRESS=<youremail>
EMAIL_FROM=
EMAIL_HOST=
EMAIL_PORT=
EMAIL_SECURE=True
EMAIL_USER=
EMAIL_PASSWORD=
# URL of project directory (*)
# Example: /home/My_project/
PROJECT_PATH=</project/directory/path>

33
test.js
View File

@ -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: `
<h1>*** event ***</h1>
<h4>Committer: N/A</h4>
<h4>Message: N/A</h4>
<h4>Branch: N/A</h4>
<h4>Process output:</h4>
<textarea style='wordWrap:break-word; display: block; width:100%; height:70vh;border:solid 2px orange'>N/A</textarea>
`,
};
transporter.sendMail(options);
console.log("first")