Skip to content

Commit

Permalink
Merge pull request #203 from DSC-McMaster-U/raymondtaoo/jenkins
Browse files Browse the repository at this point in the history
Create Jenkins Pipeline
  • Loading branch information
rawanmahdi authored Feb 21, 2024
2 parents e1a2377 + da63590 commit 14e0d38
Show file tree
Hide file tree
Showing 5 changed files with 226 additions and 14 deletions.
49 changes: 49 additions & 0 deletions backend/Jenkinsfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
pipeline {
agent any
environment {
// Path for homebrew installed Poetry
PATH = "/opt/homebrew/bin:$PATH"
}

stages {
stage('Checkout') {
steps {
// Make sure it is the correct branch
git branch: 'main', url: 'https://github.com/DSC-McMaster-U/Auto-ML.git'
}
}

stage('Set Up Backend Environment') {
steps {
dir('backend') {
sh 'poetry install'
}
}
}

stage('Start FastAPI Server') {
steps {
dir('backend') {
// Start FastAPI server
sh 'poetry run uvicorn main:app --reload --host 0.0.0.0 --port 8000 &'
// Add a sleep command to give the server time to start
sh 'sleep 10'
}
}
}

stage('Run Tests') {
steps {
dir('backend') {
sh 'poetry run pytest tests/api_test.py'
}
}
}
}
post {
always {
// Commands to clean up, stop server, etc.
sh 'kill $(lsof -t -i:8000)'
}
}
}
99 changes: 89 additions & 10 deletions backend/README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
# Poetry + FastAPI Backend Setup

- Poetry is a tool for dependency management and packaging in Python. It allows you to declare the libraries your project depends on and it will manage (install/update) them for you.
- Poetry is a tool for dependency management and packaging in Python. It allows you to declare the libraries your project depends on and it will manage (install/update) them for you.
- It's similar to npm in the JavaScript world
- FastAPI is a modern, fast (high-performance), web framework for building APIs with Python 3.6+ based on standard Python type hints.

References:
References:

- https://python-poetry.org/docs/
- [Medium Article](https://medium.com/@caetanoog/start-your-first-fastapi-server-with-poetry-in-10-minutes-fef90e9604d9)

Expand All @@ -16,7 +17,7 @@ Poetry installation:
- To start with Poetry on mac, run: `brew install poetry`

- For Linux, macOS, Windows (WSL), run:
`curl -sSL https://install.python-poetry.org | python3 -`
`curl -sSL https://install.python-poetry.org | python3 -`

This will set up poetry. check installation using `poetry --version`

Expand All @@ -25,18 +26,18 @@ This will set up poetry. check installation using `poetry --version`
<br>

(Read **only** if you have added a new import to `main.py` that requires a package not currently listed in `pyproject.toml` file)

- `pyproject.toml` file will not be automatically updated when you modify your `.py` files.

- `pyproject.toml` file will not be automatically updated when you modify your `.py` files.
- The `pyproject.toml` file is a configuration file that specifies the dependencies and other settings for your Python project. It doesn't automatically track the imports or other changes in your Python files.
- If you add a new import to your Python files that requires a package not currently listed in your `pyproject.toml` file, you need to manually add that package to the `[tool.poetry.dependencies]` section of your `pyproject.toml` file.
- This can be done by running `poetry add package-name` which will add the package to the `[tool.poetry.dependencies]` section and update the `poetry.lock` file to include the new package and its dependencies.
- This can be done by running `poetry add package-name` which will add the package to the `[tool.poetry.dependencies]` section and update the `poetry.lock` file to include the new package and its dependencies.
- Now, run `poetry lock` and then `poetry install` to install the new package and its dependencies.
</details>

Next, to install the dependencies, run: `poetry install`

- This command reads the `pyproject.toml` file from the current project, resolves the dependencies, and installs them.
- If there is a `poetry.lock` file in the current directory, it will use the exact versions from there instead of resolving them.
- If there is a `poetry.lock` file in the current directory, it will use the exact versions from there instead of resolving them.
- This ensures that everyone using the library will get the same versions of the dependencies.

Lastly, to start the FastAPI server locally (using virtual env), run:
Expand All @@ -50,11 +51,12 @@ and then run:`uvicorn main:app --reload`
(Only required for local setup)
Make sure both the frontend and backend setups are up and running

- Next.js frontend running on http://localhost:3000/
- FastAPI server runs on http://127.0.0.1:8000/
- Next.js frontend running on http://localhost:3000/
- FastAPI server runs on http://127.0.0.1:8000/
- See if http://localhost:3000/api/python prints hello message from backend (which confirms that the frontend is able to make a request to the backend)

References:

- Guide : https://vercel.com/templates/next.js/nextjs-fastapi-starter
- Corresponding Repo : https://github.com/digitros/nextjs-fastapi

Expand All @@ -66,4 +68,81 @@ If you don't want to run locally, you can run the backend using docker.
- Run: `docker build . -t automate-be`
- This builds a local docker image of the automate backend tagged "automate-be"
- run: `docker run -d -p 8000:8000 automate-be`
- This runs the container in detached mode, on port 8000 (default port)
- This runs the container in detached mode, on port 8000 (default port)

### Running Tests with Pytest

Using Pytest for testing the backend, follow these steps to run the tests:

- Ensure you have Poetry installed and the backend dependencies are installed using `poetry install`.
- To run the tests, execute:
`poetry run pytest tests/api_test.py`
This command will run the tests located in `tests/api_test.py` using the Pytest framework.

### Jenkins Pipeline Integration

Jenkins is for continuous integration and automation. The `Jenkinsfile` in the repository outlines the necessary steps for the pipeline.

#### Jenkins Setup

Before you can run the Jenkins pipeline, you need to set up Jenkins on your system. Follow these steps based on your operating system:

**For macOS:**

1. Install Jenkins using Homebrew:
```
brew install jenkins-lts
```
2. Start Jenkins:
```
brew services start jenkins-lts
```
3. Access Jenkins by navigating to `http://localhost:8080` in your browser.
**For Windows:**
1. Download the Jenkins installer from the [Jenkins website](https://www.jenkins.io/download/).
2. Run the installer and follow the on-screen instructions.
3. Once installed, start Jenkins and access it via `http://localhost:8080`.
**For Linux (using curl):**
1. Add the Jenkins repository and key:
```
curl -fsSL https://pkg.jenkins.io/debian-stable/jenkins.io.key | sudo tee \
/usr/share/keyrings/jenkins-keyring.asc > /dev/null
```
2. Add the Jenkins source list:
```
echo deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc] \
https://pkg.jenkins.io/debian-stable binary/ | sudo tee \
/etc/apt/sources.list.d/jenkins.list > /dev/null
```
3. Update your local package index, then install Jenkins:
```
sudo apt-get update
sudo apt-get install jenkins
```
4. Start Jenkins:
```
sudo systemctl start jenkins
```
5. Access Jenkins at `http://localhost:8080`.
#### Running the Jenkins Pipeline
Once Jenkins is set up:
1. Open Jenkins in your web browser.
2. Create a new job and select "Pipeline" as the project type.
3. In the pipeline configuration, specify the path to your `Jenkinsfile`.
4. Run the pipeline. It will execute the steps defined in the `Jenkinsfile`:
- **Checkout**: Clones the repository.
- **Set Up Backend Environment**: Installs dependencies using Poetry.
- **Start FastAPI Server**: Launches the FastAPI application.
- **Run Tests**: Executes tests using Pytest.
- **Post-Execution**: Cleans up resources and stops the server.
#### Additional Resources
For more detailed information on Jenkins and its setup, refer to the [official Jenkins documentation](https://www.jenkins.io/doc/).
49 changes: 47 additions & 2 deletions backend/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions backend/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@ readme = "README.md"
[tool.poetry.dependencies]
python = ">=3.11,<3.12"
fastapi = "^0.104.0"
uvicorn = {extras = ["standard"], version = "^0.23.2"}
uvicorn = { extras = ["standard"], version = "^0.23.2" }
google-cloud-storage = "*"
google-cloud-bigquery = "*"
python-multipart = "*"
seaborn = "^0.13.2"
pytest = "^8.0.0"
joblib = "^1.3.2"
kaleido = "0.2.1"
pycaret = "^3.2.0"

[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
build-backend = "poetry.core.masonry.api"
38 changes: 38 additions & 0 deletions backend/tests/api_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import requests
import pytest

BASE_URL = "http://127.0.0.1:8000"

def test_root():
response = requests.get(f"{BASE_URL}/")
assert response.status_code == 200

def test_python():
response = requests.get(f"{BASE_URL}/api/python")
assert response.status_code == 200

def test_datasets():
response = requests.get(f"{BASE_URL}/api/datasets")
assert response.status_code == 200

def test_bigquery():
response = requests.get(f"{BASE_URL}/api/bq?filename=sample_contacts")
assert response.status_code == 200

def test_get_data():
response = requests.get(f"{BASE_URL}/api/data?filename=data")
assert response.status_code == 200

def test_automl():
response = requests.get(f"{BASE_URL}/api/automl")
assert response.status_code == 200

def test_upload_data():
url = f"{BASE_URL}/api/upload?filename=test-data"
files = {'file': ('data.csv', open('data.csv', 'rb'), 'text/csv')}
response = requests.put(url, files=files)
assert response.status_code == 200

def test_eda():
response = requests.get(f"{BASE_URL}/api/eda?filename=data")
assert response.status_code == 200

0 comments on commit 14e0d38

Please sign in to comment.