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

240
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 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.
git clone https://gitea.nswteam.net/joseph/gitea_CICD.git
### 🌟 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# 3. **Configure environment variables**
```sh ```bash
cd gitea_CICD 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# 4. **Configure Nginx**
```sh
pwd
```
- output: <path>/gitea_CICD
### 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 5. **Install service**
mkdir service_run ```bash
``` ./install.sh
Copy environment file ```
```sh
cp service/giteaService.conf service_run/giteaService.conf
```
### 5) Fill in environment variables (\*) ## ⚙️ Configure Gitea Webhook
root@root# 1. Access your Gitea repository
```sh 2. Go to Settings > Webhooks
nano service_run/giteaService.conf 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)
### 6) Add Nginx configuration ## 📁 Directory Structure
- Open the Nginx configuration file.
- Add thr following configuration content. ```
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
```
``` ## 🔧 Customize Build Process
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;
}
```
- Change "IP-address" and "PORT". The "PORT" corresponds to the "PORT_SERVICE" of the "service_run/giteaService.conf" file. You can customize the build process by adding environment variables in `giteaService.conf`:
- Restart Nginx service. ```bash
```sh # Syntax:
systemctl restart nginx <name>_PROCESS_PATH=/path/to/directory
``` <name>_PROCESS_PATH_COMMAND=command1 && command2 ...
### 7) Run file "install.sh" to install service
root@root# # Example:
```sh FE_PROCESS_PATH=/home/project/frontend
./install.sh FE_PROCESS_PATH_COMMAND=npm install && npm run build
```
<div align="center"><img src="https://i.ibb.co/VJHhb3y/install-Service.png" alt="install server" width="600"></div> BE_PROCESS_PATH=/home/project/backend
BE_PROCESS_PATH_COMMAND=npm install && npm run start
```
### 8) Add webhook ## 📧 Email Notifications
- Open your project on Gitea: The system sends email notifications when:
- Choose "Setting" - New repository events occur
<div align="center"><img src="https://i.ibb.co/QHsST4D/image.png" alt="webhook1" width="800"></div> - Build process succeeds/fails
- Errors occur during processing
- Choose "Webhooks" and "Add Webhooks" ## 🔍 Monitoring and Troubleshooting
<div align="center"><img src="https://i.ibb.co/bKFpvbV/image.png" alt="webhook2" width="800"></div>
- Fill in the information - View service logs:
<div align="center"><img src="https://i.ibb.co/GJ55RDr/image.png" alt="webhook3" width="800"></div> ```bash
journalctl -u giteaHook -f
```
- Target URL: URL to the API that handles requests when a event is triggered (Default: your_domain/git/gitea-webhook). - Check service status:
- Trigger on: Event to trigger the webhook. ```bash
- Branch filter: Events will be listened on the branch you choose. ( '*' :all branch) systemctl status giteaHook
```
#### ** Note: ** ## 📝 Important Notes
- Project gitea_CICD should be placed at the same folder level as your 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
``` ## 👨‍💻 Author
├── your_project
└── gitea_CICD project
```
- Depending on the technology your project uses, the "gitea_CICD/service_run/giteaHook.sh" file will be custom configured to match the technology. **Joseph Le** - [GitHub](https://github.com/joseph)
<div align="center"><h4><i>**____ Joseph Le____ **</i></h4></div> ## 📄 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 // Configure email transporter
const createTransporter = () => nodeMailer.createTransport({ const createTransporter = () => nodeMailer.createTransport({
pool: true, pool: true,
host: "mail.apactech.io", host: getConfigValue("EMAIL_HOST") || "smtp.gmail.com",
port: 465, port: parseInt(getConfigValue("EMAIL_PORT") || "587"),
secure: true, secure: getConfigValue("EMAIL_SECURE") === "True",
auth: { auth: {
user: "admin@apactech.io", user: getConfigValue("EMAIL_USER") || "git@notification.com",
pass: "BGK!dyt6upd2eax1bhz", pass: getConfigValue("EMAIL_PASSWORD") || "123456789",
}, },
}); });
@ -39,7 +39,8 @@ app.use(express.json());
app.post("/git/gitea-webhook", async (req, res) => { app.post("/git/gitea-webhook", async (req, res) => {
let title = ""; let title = "";
const checkSendMail = getConfigValue("SEND_EMAIL"); 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"]; const giteaEvent = req.headers["x-gitea-event"];
// Return response immediately // Return response immediately
@ -51,7 +52,7 @@ app.post("/git/gitea-webhook", async (req, res) => {
if (checkSendMail === "True") { if (checkSendMail === "True") {
const transporter = createTransporter(); const transporter = createTransporter();
const options = { const options = {
from: "admin@apactech.io", from: emailFrom,
to: emailAddress, to: emailAddress,
subject: "Git notifications (Fail)", subject: "Git notifications (Fail)",
html: "<h3>There is an active build process. Please check and push again in a few minutes.</h3>", 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") { if (checkSendMail === "True") {
const transporter = createTransporter(); const transporter = createTransporter();
const options = { const options = {
from: "admin@apactech.io", from: emailFrom,
to: emailAddress, to: emailAddress,
subject: title, subject: title,
html: ` html: `

View File

@ -15,10 +15,15 @@ GIT_BRANCH=<your_branch>
PORT_SERVICE=5000 PORT_SERVICE=5000
# Email: Used to send notifications when the repository has updates (*) # Email: Used to send notifications when the repository has updates (*)
EMAIL_ADDRESS=<youremail>
# Enable/Disable send mail: True or False (*) # 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 (*) # URL of project directory (*)
# Example: /home/My_project/ # Example: /home/My_project/
PROJECT_PATH=</project/directory/path> 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")