Files
jenkins-docker-deploy-example/README.md
2026-03-07 03:43:50 +00:00

102 lines
2.9 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# Jenkins Docker Deploy Example
A Statping-like status monitoring app that demonstrates a Jenkins pipeline for Docker build, push, and deploy. The app performs HTTP/HTTPS and TCP checks, stores history in SQLite, and provides a dashboard with reports.
## App Features
- **HTTP/HTTPS checks** Ping URLs, measure response time and status code (success = 2xx)
- **TCP checks** Verify connectivity to host:port
- **History storage** SQLite database persists check results
- **Reports** Uptime %, avg/min/max latency, recent check history
## Repository Structure
```
.
├── Jenkinsfile # Pipeline definition
├── Dockerfile # Application image
├── docker-compose.yml # Deployment compose (pulled by deploy host)
├── app.py # Entry point
├── app/
│ ├── main.py # Flask routes
│ ├── models.py # SQLite schema
│ ├── checker.py # HTTP/HTTPS and TCP check logic
│ └── scheduler.py # APScheduler for periodic checks
├── templates/ # HTML templates
├── static/ # CSS
├── requirements.txt
└── README.md
```
## Manual Test
```bash
# Build and run locally
docker build -t myapp:test .
docker run -p 8080:8080 -v $(pwd)/data:/app/data myapp:test
# Visit http://localhost:8080
```
Add services from the dashboard (e.g. `https://example.com`, `google.com:443` for TCP) and view reports.
## Jenkins Pipeline
The pipeline:
1. **Builds** a Docker image
2. **Pushes** the image to a container registry (Docker Hub, etc.)
3. **SSHs** to a deployment machine
4. **Clones** (or pulls) this repo to get `docker-compose.yml`
5. **Deploys** with `docker compose up -d`
### Prerequisites
**Jenkins**
- Docker installed and Jenkins user in `docker` group
- Pipeline and SSH Agent plugins
- Git for cloning
**Jenkins Credentials**
| ID | Type | Purpose |
|----|------|---------|
| `docker-registry-credentials` | Username/Password | Docker Hub or registry login |
| `deploy-ssh-key` | SSH Username with private key | SSH to deploy host |
**Deploy Host**
- Docker and Docker Compose installed
- SSH access for the deploy user
- If using a private registry: run `docker login` on the deploy host
### Configuration
Edit the `environment` block in `Jenkinsfile`:
```groovy
environment {
DOCKER_REGISTRY = 'docker.io'
DOCKER_IMAGE = 'myorg/myapp'
DEPLOY_HOST = 'deploy-server.example.com'
DEPLOY_USER = 'deploy'
DEPLOY_PATH = '/opt/myapp'
GIT_REPO_URL = 'https://github.com/myorg/jenkins-docker-deploy-example.git'
}
```
### First-Time Deploy Host Setup
```bash
sudo mkdir -p /opt/myapp
sudo chown deploy:deploy /opt/myapp
sudo usermod -aG docker deploy
```
The `docker-compose.yml` mounts `./data:/app/data` for SQLite persistence. Ensure the deploy directory is writable.
### Branch Behavior
- **main** → image tag `latest`
- **other branches** → image tag `{BUILD_NUMBER}-{GIT_SHA}`