Add: Springboot tests in CICD #2
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: Deploy to AWS | |
| on: | |
| # Allows to manually run this workflow from the Actions tab | |
| # You can select which branch to run it on from the UI. | |
| workflow_dispatch: | |
| # Triggers the workflow on pushes to main branch and feature branches | |
| # Main branch for production deployments, feature branches for testing | |
| push: | |
| branches: | |
| - 'main' | |
| - 'feature/**' | |
| jobs: | |
| test-springboot: | |
| uses: ./.github/workflows/test-springboot-services.yml | |
| test-genai: | |
| uses: ./.github/workflows/test-genai-service.yml | |
| secrets: inherit | |
| deploy: | |
| needs: [test-springboot, test-genai] | |
| if: always() | |
| runs-on: ubuntu-latest | |
| environment: | |
| name: oopoops | |
| url: 'https://client.${{ vars.EC2_PUBLIC_IP }}.nip.io' | |
| steps: | |
| - name: Checkout Code | |
| uses: actions/checkout@v4 | |
| - name: Debug Environment Variables | |
| run: | | |
| echo "EC2_PUBLIC_IP: ${{ vars.EC2_PUBLIC_IP }}" | |
| echo "AWS_EC2_USER: ${{ vars.AWS_EC2_USER }}" | |
| echo "SSH Key length: ${#AWS_EC2_PRIVATE_KEY}" | |
| env: | |
| AWS_EC2_PRIVATE_KEY: ${{ secrets.AWS_EC2_PRIVATE_KEY }} | |
| - name: Copy Docker Compose File to EC2 | |
| uses: appleboy/scp-action@v0.1.7 | |
| with: | |
| host: ${{ vars.EC2_PUBLIC_IP }} | |
| username: ${{ vars.AWS_EC2_USER }} | |
| key: ${{ secrets.AWS_EC2_PRIVATE_KEY }} | |
| source: "./compose.aws.yml,./create-multiple-dbs.sh,./keycloak" | |
| target: /home/${{ vars.AWS_EC2_USER }} | |
| - name: Create .env file on EC2 | |
| uses: appleboy/ssh-action@v1.0.3 | |
| with: | |
| host: ${{ vars.EC2_PUBLIC_IP }} | |
| username: ${{ vars.AWS_EC2_USER }} | |
| key: ${{ secrets.AWS_EC2_PRIVATE_KEY }} | |
| script: | | |
| # Exit immediately if a command exits with a non-zero status. | |
| set -e | |
| echo "--- Creating .env file ---" | |
| # Overwrite existing file to ensure it's clean on each deployment | |
| > .env | |
| echo "CLIENT_HOST=client.${{ vars.EC2_PUBLIC_IP }}.nip.io" >> .env | |
| echo "API_HOST=api.${{ vars.EC2_PUBLIC_IP }}.nip.io" >> .env | |
| echo "PUBLIC_API_URL=https://api.${{ vars.EC2_PUBLIC_IP }}.nip.io/docs" >> .env | |
| echo "POSTGRES_USER=${{ secrets.POSTGRES_USER }}" >> .env | |
| echo "POSTGRES_PASSWORD=${{ secrets.POSTGRES_PASSWORD }}" >> .env | |
| echo "OPENAI_API_KEY=${{ secrets.OPENAI_API_KEY }}" >> .env | |
| echo "KEYCLOAK_ADMIN_USER=${{ secrets.KEYCLOAK_ADMIN_USER }}" >> .env | |
| echo "KEYCLOAK_ADMIN_PASSWORD=${{ secrets.KEYCLOAK_ADMIN_PASSWORD }}" >> .env | |
| echo ".env file created successfully." | |
| echo "--- Updating Keycloak realm configuration ---" | |
| CLIENT_HOST_VAR="client.${{ vars.EC2_PUBLIC_IP }}.nip.io" | |
| # Use a different delimiter for sed to avoid issues with slashes in the URL | |
| sed -i "s|http://localhost:3000|https://${CLIENT_HOST_VAR}|g" ./keycloak/myrealm.json | |
| echo "Keycloak realm configuration updated." | |
| - name: Deploy with Docker Compose | |
| uses: appleboy/ssh-action@v1.0.3 | |
| with: | |
| host: ${{ vars.EC2_PUBLIC_IP }} | |
| username: ${{ vars.AWS_EC2_USER }} | |
| key: ${{ secrets.AWS_EC2_PRIVATE_KEY }} | |
| script: | | |
| # Exit on error, print commands | |
| set -ex | |
| echo "--- Verifying Docker installation ---" | |
| docker --version | |
| docker compose version | |
| echo "--- Checking disk space before cleanup ---" | |
| df -h | |
| echo "--- Logging into Docker registry (GHCR) ---" | |
| echo ${{ secrets.GITHUB_TOKEN }} | docker login ghcr.io -u ${{ github.actor }} --password-stdin | |
| echo "--- Stopping any running services to avoid conflicts ---" | |
| docker compose -f compose.aws.yml down || echo "No running containers to stop. Continuing." | |
| echo "--- Cleaning up Docker resources to free space ---" | |
| # Remove all stopped containers | |
| docker container prune -f || echo "No containers to prune" | |
| # Remove all unused images (not just dangling ones) | |
| docker image prune -a -f || echo "No images to prune" | |
| # Remove all unused volumes (be careful with data!) | |
| docker volume prune -f || echo "No volumes to prune" | |
| # Remove all unused networks | |
| docker network prune -f || echo "No networks to prune" | |
| # Remove build cache | |
| docker builder prune -f || echo "No build cache to prune" | |
| echo "--- Checking disk space after cleanup ---" | |
| df -h | |
| echo "--- Starting Docker Compose services ---" | |
| docker compose -f compose.aws.yml --env-file .env up --pull always -d | |
| echo "--- Deployment initiated. Waiting 15 seconds for services to stabilize... ---" | |
| sleep 15 | |
| echo "--- Checking status of running containers ---" | |
| docker compose -f compose.aws.yml ps | |
| echo "--- Displaying recent logs for troubleshooting ---" | |
| docker compose -f compose.aws.yml logs --tail=50 | |
| echo "--- Deployment completed successfully ---" | |
| post-deployment-validation: | |
| needs: deploy | |
| if: always() && needs.deploy.result == 'success' | |
| uses: ./.github/workflows/post-deployment-validation.yml | |
| with: | |
| environment: ${{ github.ref == 'refs/heads/main' && 'production' || 'staging' }} | |
| base_url: 'https://client.${{ vars.EC2_PUBLIC_IP }}.nip.io' |