This commit is contained in:
2026-03-07 03:43:50 +00:00
parent 365cb6692e
commit 17d9033152
17 changed files with 1349 additions and 88 deletions

142
README.md
View File

@@ -1,12 +1,13 @@
# Jenkins Docker Deploy Example
Example repository demonstrating a Jenkins pipeline that:
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.
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`
## 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
@@ -15,77 +16,86 @@ Example repository demonstrating a Jenkins pipeline that:
├── Jenkinsfile # Pipeline definition
├── Dockerfile # Application image
├── docker-compose.yml # Deployment compose (pulled by deploy host)
├── app.py # Minimal Python web app
├── 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
```
## Prerequisites
### Jenkins
- **Docker** installed and Jenkins user in `docker` group
- **Pipeline** and **SSH Agent** plugins
- **Git** for cloning
### Jenkins Credentials
Create these in **Manage 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 (or add to the pipeline)
## Configuration
Edit the `environment` block in `Jenkinsfile`:
```groovy
environment {
DOCKER_REGISTRY = 'docker.io' // or your registry (ghcr.io, etc.)
DOCKER_IMAGE = 'myorg/myapp' // e.g., username/repo
DEPLOY_HOST = 'deploy-server.example.com'
DEPLOY_USER = 'deploy'
DEPLOY_PATH = '/opt/myapp' // Where to clone & run compose
GIT_REPO_URL = 'https://github.com/myorg/jenkins-docker-deploy-example.git'
}
```
## Pipeline Stages
1. **Build** `docker build` with tag from branch (e.g. `latest` for main, `123-abc1234` for others)
2. **Push** `docker push` to registry using stored credentials
3. **Deploy** SSH to host, clone/pull repo, run `docker compose up -d`
## First-Time Deploy Host Setup
On the deploy host:
```bash
# Create deploy directory
sudo mkdir -p /opt/myapp
sudo chown deploy:deploy /opt/myapp
# Ensure deploy user can run docker (add to docker group)
sudo usermod -aG docker deploy
```
## Manual Test
```bash
# Build and run locally
docker build -t myapp:test .
docker run -p 8080:8080 myapp:test
docker run -p 8080:8080 -v $(pwd)/data:/app/data myapp:test
# Visit http://localhost:8080
```
## Branch Behavior
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}`