An open API service indexing awesome lists of open source software.

https://github.com/graphtylove/jenkins-demo


https://github.com/graphtylove/jenkins-demo

Last synced: 11 months ago
JSON representation

Awesome Lists containing this project

README

          

# Jekins deployment FastAPI with Docker demo

This is a dead simple FastAPI application with [Jenkins](https://www.jenkins.io/) [CI/CD](https://github.com/resources/articles/devops/ci-cd) integration.

It demonstrates how to build, deploy, and serve a Python [FastAPI](https://fastapi.tiangolo.com/) app on [Docker](https://www.docker.com/) with automated deployment on every push to the `main` branch.

![Jenkins logo](https://upload.wikimedia.org/wikipedia/commons/e/e3/Jenkins_logo_with_title.svg)

---

## 🚀 Features
- **FastAPI** app with two endpoints:
- `/` — Returns `Hello, world!`
- `/user/{name}` — Returns `Hello {name}`
- `/docs` — Show the documentation of your API *(auto-generated by FastAPI)*
- **Jenkins Pipeline** for automated deployment on every push to `main`

---

## 📂 Project Structure
```
jenkins-demo/
├── Jenkinsfile # Jenkins pipeline definition
├── main.py # FastAPI application
├── requirements.txt # Python dependencies
└── README.md # Project documentation
```

---

## 🖥️ Endpoints

### `GET /`
Returns a simple hello world message.

**Response:**
```
Hello, world!
```

### `GET /user/{name}`
Returns a personalized hello message.

**Example:**
```
GET /user/Alice
```
**Response:**
```
Hello Alice
```

---
## 🤖 Jenkins CI/CD Pipeline

This project includes a `Jenkinsfile` for automated deployment:

- **Trigger:** On every push to the `main` branch (to configure in Jenkins UI)
- **Steps:**
1. Build the Docker image
2. Run the container

### Jenkinsfile Overview
```groovy
pipeline {
agent any

stages {

// 1. Build the docker image on the server
stage('Build Docker Image') {
steps {
sh 'docker build -t fastapi-image.'
}
}

// 2. Delete the old container and deploy a new one
stage('Run Docker Container') {
steps {
sh 'docker rm -f fastapi-app-container || true'
sh 'docker run -d --name fastapi-app-container -p 8081:8081 fastapi-image'
}
}

}
}
```

### Jenkins Setup Instructions
1. **Install Jenkins** and required plugins (e.g., Git, Pipeline).
2. **Create a new Pipeline job** and point it to your repository.
3. **Ensure Docker are available** on the Jenkins agent. (already done on our Becode server)
4. **Configure credentials** if your repository is private.
5. **Start the pipeline** — it will auto-deploy on every push to `main`. (when configured in the UI)

---

## ⚙️ Local Development (Advanced)

1. **Clone the repository:**
```bash
git clone
cd jenkins-demo
```
2. **Create a virtual environment and install dependencies:**
```bash
python3 -m venv venv
source venv/bin/activate
pip install -r requirements.txt
```
3. **Run the FastAPI app:**
```bash
uvicorn main:app --reload
```
4. **Access the app:**
- [http://localhost:8000/](http://localhost:8000/) — Hello World
- [http://localhost:8000/user/YourName](http://localhost:8000/user/YourName) — Personalized Hello
- [http://localhost:8000/docs](http://localhost:8000/docs) — The documentation of your API

---

## 📄 License
MIT