Update README.md
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2024-11-28 17:26:40 +01:00
parent fd0b0f8840
commit d93aa1a088

156
README.md
View File

@@ -1,118 +1,104 @@
## Drone CI Pipeline Configuration
# Drone CI/CD Pipeline - Hello World Example with Cleanup
This repository includes a CI pipeline configuration for Drone CI to automate testing and syntax validation for a project with PHP, HTML, React, and Python components.
This project demonstrates a simple Drone CI/CD pipeline that outputs a "Hello, World!" message and then performs a cleanup of any created data. It is designed to show the basics of defining a pipeline in Drone, including running commands and performing cleanup at the end of the pipeline.
## Pipeline Overview
The `.drone.yml` file defines a multi-step pipeline that performs the following actions:
This pipeline contains two main steps:
### Steps
1. **Hello World**: A simple step that prints "Hello, World!" to verify that the pipeline is running correctly.
2. **Cleanup**: A cleanup step that removes any files or data created during the pipeline execution (for example, temporary files).
1. **Check PHP Syntax**:
Verifies the syntax of all `.php` files in the repository using PHP's built-in linter.
2. **Check HTML Syntax**:
Uses the `tidy` tool to validate the syntax of all `.html` files in the repository.
3. **Run Tests for React Client**:
Executes unit tests for the React client application if a `package.json` file exists in the `services/client` directory.
4. **Run Tests for Python Backend**:
Installs dependencies, recreates the database, and runs unit tests for the Python backend application using `pytest`.
The purpose of this pipeline is to:
### Services - PostgreSQL Database Service
* Ensure that the Drone setup is working.
* Demonstrate a simple pipeline structure.
* Show how to perform cleanup after pipeline steps.
A PostgreSQL container named `gamedb` is set up to provide a testing database for the Python backend.
## How to Use
## How It Works
To use this pipeline:
### Pipeline Configuration
1. Ensure that you have Drone set up and running in your environment.
2. Add the `.drone.yml` file to the root directory of your repository.
3. Push your changes to the repository. Drone will automatically detect the `.drone.yml` file and run the pipeline.
The pipeline is defined in the `.drone.yml` file. Each step uses a Docker container to provide the required environment and tools.
## Prerequisites
### Key Features
* A working Drone server and Drone runner.
* The repository must be connected to Drone.
* **PHP Syntax Check:** Scans all `.php` files and ensures there are no syntax errors.
* **HTML Syntax Check:** Installs and uses the `tidy` tool to validate `.html` files.
* **React Client Tests:** Runs tests using `yarn` if a `package.json` file is present in the `services/client` directory.
* **Python Backend Tests:** Runs `pytest` after installing dependencies and recreating the database.
## Running the Pipeline
### Environment Variables
After pushing the `.drone.yml` file to your repository:
The pipeline uses the following environment variables for the Python backend tests:
1. Drone will trigger a build.
2. The "Hello, World!" step will execute and print the message to the console.
3. After that, the cleanup step will execute, deleting any temporary data or files created.
* `FLASK_ENV`: Set to `production` to ensure tests run in the correct environment.
* `APP_SETTINGS`: Specifies the configuration class for the Flask app.
* `DATABASE_TEST_URL`: Connection string for the testing database.
## Structure of `.drone.yml`
## Requirements
```yaml
---
kind: pipeline
name: hello-world-pipeline
steps:
- name: hello-world
image: alpine:latest
commands:
- echo "Hello, World! Everything is running fine!"
* **Drone CI:** The pipeline requires a Drone CI server to execute the `.drone.yml` file.
* **Docker:** The pipeline steps and services run in Docker containers, so Docker must be available on the Drone runner.
- name: cleanup
image: alpine:latest
commands:
- echo "Cleaning up any created data..."
- rm -rf /tmp/* # Example cleanup, adjust based on what needs cleanup
- echo "Cleanup complete!"
```
## Usage
**Explanation of Key Sections:**
### Local Testing
* **`kind: pipeline`**: Defines the type of pipeline (in this case, it's a pipeline).
* **`name: hello-world-pipeline`**: The name of the pipeline (you can give it any name you prefer).
* **`steps`**: A list of steps that define the actions to be executed in the pipeline. Each step runs a Docker container (using a specified image) and can include commands to execute.
Before pushing changes to the repository, you can manually test each component:
**Steps Breakdown:**
1. **PHP Syntax:**
* **`hello-world` step:**
* `name: hello-world`: The name of the step.
* `image: alpine:latest`: The `alpine:latest` Docker image is used because it's small and has basic utilities.
* `commands`: The `echo` command prints "Hello, World!" to verify that the pipeline is running.
* **`cleanup` step:**
* `name: cleanup`: The name of the cleanup step.
* `image: alpine:latest`: Again, `alpine:latest` is used here for the cleanup process.
* `commands`: The `rm -rf /tmp/*` command is an example cleanup that deletes temporary files. You can replace this with other cleanup commands based on your project's requirements.
## Example Cleanup
In this example, the cleanup step is as simple as removing files from `/tmp`:
```bash
find . -type f -name '*.php' -exec php -l {} \;
- rm -rf /tmp/*
```
2. **HTML Syntax:**
This is just a placeholder for any cleanup logic. Depending on your project, you may need to clean up:
Install tidy and run:
* Temporary files or directories created during the build.
* Build artifacts (like compiled files, logs, etc.).
* Any other resources used during the pipeline execution.
**Customizing Cleanup:**
If your project creates files or directories that need to be cleaned up, replace the `rm -rf /tmp/*` command with commands suited for your environment, such as:
```bash
find . -type f -name '*.html' -exec tidy -q -e {} \;
- rm -rf /build/output/*
- rm -rf /logs/*
- docker volume prune -f # Remove unused Docker volumes, if relevant
```
3. **React Client Tests:**
## Conclusion
Navigate to services/client and run:
This `.drone.yml` provides a simple example of how to structure a Drone CI/CD pipeline that runs a "Hello, World!" message and cleans up any created data afterward. It's a good starting point for more complex pipelines.
```bash
yarn install
CI=true yarn test --coverage
```
4. **Python Backend Tests:**
Navigate to services/game and run:
```bash
pip install -r requirements.txt
python manage.py recreate_db
pytest --cov=project --cov-report=term-missing
```
### Running the Pipeline
Push your changes to the repository. The pipeline will execute automatically in Drone CI. Review the results in the Drone CI dashboard.
## Directory Structure
Ensure the following structure for the pipeline to work:
```
.
├── services/
│ ├── client/ # React client code
│ │ ├── package.json
│ │ ├── src/
│ │ └── ...
│ └── game/ # Python backend code
│ ├── manage.py
│ ├── requirements.txt
│ └── ...
├── .drone.yml # Drone CI pipeline configuration
└── README.md # Project documentation
```
## Troubleshooting
### Common Issues
* **Missing Directories:** If the pipeline cannot find services/client or services/game, ensure your repository structure matches the expected layout.
* **Missing Dependencies:** Make sure all required dependencies are installed in package.json and requirements.txt.
* **Database Connection Errors:** Ensure the gamedb service is running and accessible by the Python backend.
Feel free to extend and customize the pipeline by adding more steps for building, testing, or deploying your application!