Add main.yml in workflows #1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Main CI/CD Pipeline | |
on: | |
push: | |
branches: [ main, master ] | |
paths-ignore: | |
- '**.md' | |
- '.gitignore' | |
pull_request: | |
branches: [ main, master ] | |
paths-ignore: | |
- '**.md' | |
- '.gitignore' | |
workflow_dispatch: # Enable manual triggers | |
env: | |
DOCKER_USERNAME: tuandung12092002 | |
SEARCH_SERVER_IMAGE: tuandung12092002/semantic-search-server | |
DEMO_APP_IMAGE: tuandung12092002/semantic-search-demo | |
jobs: | |
test: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v3 | |
- name: Set up Python | |
uses: actions/setup-python@v4 | |
with: | |
python-version: '3.9' | |
- name: Create environment files | |
run: | | |
echo "${{ secrets.OPENAI_API_KEY }}" > openai_api_key.txt | |
echo "${{ secrets.WEAVIATE_API_KEY }}" > weaviate_api_key.txt | |
- name: Install dependencies | |
run: | | |
python -m pip install --upgrade pip | |
pip install -r requirements.txt | |
pip install pytest | |
- name: Install package in development mode | |
run: | | |
pip install -e . | |
- name: Run tests | |
env: | |
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }} | |
WEAVIATE_API_KEY: ${{ secrets.WEAVIATE_API_KEY }} | |
run: | | |
pytest src/test_connection.py -v | |
build-and-push: | |
needs: test | |
if: github.event_name != 'pull_request' | |
runs-on: ubuntu-latest | |
steps: | |
- name: Free disk space | |
run: | | |
sudo rm -rf /usr/share/dotnet | |
sudo rm -rf /usr/local/lib/android | |
sudo rm -rf /opt/ghc | |
sudo rm -rf /opt/hostedtoolcache/CodeQL | |
sudo docker image prune -af | |
- name: Checkout code | |
uses: actions/checkout@v3 | |
- name: Set up Docker Buildx | |
uses: docker/setup-buildx-action@v2 | |
- name: Login to DockerHub | |
uses: docker/login-action@v2 | |
with: | |
username: ${{ env.DOCKER_USERNAME }} | |
password: ${{ secrets.DOCKERHUB_TOKEN }} | |
- name: Extract metadata for Search Server | |
id: meta-search-server | |
uses: docker/metadata-action@v4 | |
with: | |
images: ${{ env.SEARCH_SERVER_IMAGE }} | |
tags: | | |
type=sha,format=short | |
type=ref,event=branch | |
latest | |
- name: Extract metadata for Demo App | |
id: meta-demo-app | |
uses: docker/metadata-action@v4 | |
with: | |
images: ${{ env.DEMO_APP_IMAGE }} | |
tags: | | |
type=sha,format=short | |
type=ref,event=branch | |
latest | |
- name: Build and Push Search Server Image | |
uses: docker/build-push-action@v4 | |
with: | |
context: . | |
file: ./docker/search_server.Dockerfile | |
push: true | |
tags: ${{ steps.meta-search-server.outputs.tags }} | |
labels: ${{ steps.meta-search-server.outputs.labels }} | |
cache-from: type=gha | |
cache-to: type=gha,mode=max | |
- name: Build and Push Demo App Image | |
uses: docker/build-push-action@v4 | |
with: | |
context: . | |
file: ./docker/demo_app.Dockerfile | |
push: true | |
tags: ${{ steps.meta-demo-app.outputs.tags }} | |
labels: ${{ steps.meta-demo-app.outputs.labels }} | |
cache-from: type=gha | |
cache-to: type=gha,mode=max | |
test-deployment: | |
needs: build-and-push | |
if: github.event_name != 'pull_request' | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v3 | |
- name: Create environment files | |
run: | | |
mkdir -p secrets | |
echo "${{ secrets.OPENAI_API_KEY }}" > secrets/openai_api_key.txt | |
echo "${{ secrets.WEAVIATE_API_KEY }}" > secrets/weaviate_api_key.txt | |
- name: Login to DockerHub | |
uses: docker/login-action@v2 | |
with: | |
username: ${{ env.DOCKER_USERNAME }} | |
password: ${{ secrets.DOCKERHUB_TOKEN }} | |
- name: Deploy application | |
env: | |
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }} | |
WEAVIATE_API_KEY: ${{ secrets.WEAVIATE_API_KEY }} | |
run: | | |
docker-compose -f docker/docker-compose.full.yml up -d | |
- name: Wait for services to start | |
run: | | |
timeout=120 | |
counter=0 | |
echo "Waiting for services to be ready..." | |
while ! curl --silent --fail http://localhost:8000/health; do | |
if [ $counter -eq $timeout ]; then | |
echo "Timed out waiting for health check" | |
exit 1 | |
fi | |
echo "Services not ready yet. Waiting..." | |
sleep 5 | |
counter=$((counter + 5)) | |
done | |
echo "Services are ready!" | |
- name: Test Search API | |
run: | | |
# Process a test document | |
curl -X POST http://localhost:8000/process-text \ | |
-H "Content-Type: application/json" \ | |
-d '{"text":"This is a test document for CI/CD pipeline verification."}' | |
# Query the API | |
response=$(curl -s -X POST http://localhost:8000/ask-question \ | |
-H "Content-Type: application/json" \ | |
-d '{"question":"What is this document about?", "num_search_results":1, "num_generations":1}') | |
echo "API Response: $response" | |
# Check if the response contains expected keywords | |
if echo "$response" | grep -q "CI/CD"; then | |
echo "Test passed: Response contains expected content" | |
else | |
echo "Test failed: Response does not contain expected content" | |
exit 1 | |
fi | |
- name: Cleanup | |
if: always() | |
run: | | |
docker-compose -f docker/docker-compose.full.yml down | |
docker logout |