|
| 1 | +name: CI/CD Pipeline |
| 2 | + |
| 3 | +# Trigger the workflow on push or pull request to the main branch |
| 4 | +on: |
| 5 | + push: |
| 6 | + branches: |
| 7 | + - main |
| 8 | + pull_request: |
| 9 | + branches: |
| 10 | + - main |
| 11 | + |
| 12 | +jobs: |
| 13 | + build: |
| 14 | + runs-on: ubuntu-latest # The OS environment the workflow will run on |
| 15 | + |
| 16 | + services: |
| 17 | + docker: |
| 18 | + image: docker:19.03.12 # Docker image to use for running Docker commands |
| 19 | + options: --privileged # Run in privileged mode to allow Docker-in-Docker |
| 20 | + ports: |
| 21 | + - 2375:2375 # Expose Docker port |
| 22 | + env: |
| 23 | + DOCKER_TLS_CERTDIR: "" # Disable Docker's TLS |
| 24 | + |
| 25 | + steps: |
| 26 | + # Step 1: Checkout code from the repository |
| 27 | + - name: Checkout code |
| 28 | + uses: actions/checkout@v3 |
| 29 | + |
| 30 | + # Step 2: Set up Node.js for building the React app |
| 31 | + - name: Set up Node.js |
| 32 | + uses: actions/setup-node@v3 |
| 33 | + with: |
| 34 | + node-version: '18' # Use Node.js version 18 |
| 35 | + |
| 36 | + # Step 3: Cache Node.js modules to speed up subsequent runs |
| 37 | + - name: Cache Node.js modules |
| 38 | + uses: actions/cache@v3 |
| 39 | + with: |
| 40 | + path: frontend-react/node_modules # Path to cache |
| 41 | + key: ${{ runner.os }}-node-${{ hashFiles('**/frontend-react/package-lock.json') }} |
| 42 | + restore-keys: | |
| 43 | + ${{ runner.os }}-node/ # Restore cache if available |
| 44 | +
|
| 45 | + # Step 4: Install Node.js dependencies and build the React app |
| 46 | + - name: Install dependencies and build React app |
| 47 | + working-directory: ./frontend-react |
| 48 | + run: | |
| 49 | + npm ci # Install dependencies using package-lock.json |
| 50 | + npm run build # Build the React app |
| 51 | +
|
| 52 | + # Step 5: Set up Python for the FastAPI backend |
| 53 | + - name: Set up Python |
| 54 | + uses: actions/setup-python@v3 |
| 55 | + with: |
| 56 | + python-version: '3.11' # Use Python version 3.11 |
| 57 | + |
| 58 | + # Step 6: Install Python dependencies for the FastAPI backend |
| 59 | + - name: Install dependencies for FastAPI |
| 60 | + run: | |
| 61 | + python -m pip install --upgrade pip # Upgrade pip |
| 62 | + pip install -r requirements.txt # Install dependencies from requirements.txt |
| 63 | +
|
| 64 | + # Step 7: Run pytest tests for the FastAPI backend |
| 65 | + - name: Run pytest tests |
| 66 | + run: | |
| 67 | + pytest --maxfail=1 --disable-warnings # Run pytest, stop at first failure |
| 68 | +
|
| 69 | + # Step 8: Log in to Docker Hub |
| 70 | + # Use Docker credentials stored in GitHub Secrets to log in |
| 71 | + - name: Log in to Docker Hub |
| 72 | + uses: docker/login-action@v2 |
| 73 | + with: |
| 74 | + username: ${{ secrets.DOCKER_USERNAME }} # Docker Hub username stored in secrets |
| 75 | + password: ${{ secrets.DOCKER_PASSWORD }} # Docker Hub password stored in secrets |
| 76 | + |
| 77 | + # Step 9: Build and push Docker image |
| 78 | + # This step builds the Docker image from the Dockerfile and pushes it to Docker Hub |
| 79 | + - name: Build and push Docker image |
| 80 | + run: | |
| 81 | + docker build -t ${{ secrets.DOCKER_USERNAME }}/fullstack-app:latest . # Build the Docker image |
| 82 | + docker push ${{ secrets.DOCKER_USERNAME }}/fullstack-app:latest # Push the image to Docker Hub |
| 83 | +
|
| 84 | + # Step 10: Notify of a successful build |
| 85 | + # This step outputs a success message if the Docker build and push steps succeed |
| 86 | + - name: Build Success Notification |
| 87 | + run: echo "Docker image has been built and pushed successfully." |
| 88 | + |
| 89 | + deploy: |
| 90 | + runs-on: ubuntu-latest # The OS environment for deployment |
| 91 | + needs: build # Ensure the deployment job runs only after the build job |
| 92 | + |
| 93 | + steps: |
| 94 | + # Step 11: Checkout code from the repository |
| 95 | + - name: Checkout code |
| 96 | + uses: actions/checkout@v3 |
| 97 | + |
| 98 | + # Step 12: Pull and run the Docker container for the application |
| 99 | + # This step pulls the built Docker image and runs it on the server |
| 100 | + - name: Deploy Application |
| 101 | + run: | |
| 102 | + docker pull ${{ secrets.DOCKER_USERNAME }}/fullstack-app:latest # Pull the latest image from Docker Hub |
| 103 | + docker run -d -p 8000:8000 --name fullstack-app ${{ secrets.DOCKER_USERNAME }}/fullstack-app:latest # Run the app in a container |
0 commit comments