git commit -m 'updated documentation' #37
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: CI/CD Pipeline | |
| on: | |
| push: | |
| branches: ms/Scrun-130-Project-Deployment-dockerfiles | |
| jobs: | |
| CI-PIPELINE: | |
| runs-on: ubuntu-latest | |
| # Installs needed dependencies | |
| steps: | |
| - name: Checkout Code | |
| uses: actions/checkout@v3 | |
| - name: Install Node.js | |
| uses: actions/setup-node@v3 | |
| with: | |
| node-version: 18 | |
| # Tests code too ensure it passes all tests | |
| - name: Run frontend tests | |
| run: cd course-matrix/frontend && npm install && npm run test | |
| - name: Run backend tests | |
| run: cd course-matrix/backend && npm install && npm run test | |
| # Set up Docker Buildx | |
| - name: Set up Docker Buildx | |
| uses: docker/setup-buildx-action@v2 | |
| # Sets our our application's environment | |
| - name: setup application env | |
| run: | | |
| cd course-matrix | |
| # Update frontend .env | |
| cd frontend | |
| echo "VITE_SERVER_URL=\"http://34.130.253.243:8081\"" > .env && \ | |
| echo "VITE_PUBLIC_ASSISTANT_BASE_URL=\"${{ secrets.VITE_PUBLIC_ASSISTANT_BASE_URL }}\"" >> .env && \ | |
| echo "VITE_ASSISTANT_UI_KEY=\"${{ secrets.VITE_ASSISTANT_UI_KEY }}\"" >> .env | |
| # Update backend .env | |
| cd ../backend | |
| echo "NODE_ENV=\"development\"" > .env && \ | |
| echo "PORT=8081" >> .env && \ | |
| echo "CLIENT_APP_URL=\"http://34.130.253.243:5173\"" >> .env && \ | |
| echo "DATABASE_URL=\"${{ secrets.DATABASE_URL }}\"" >> .env && \ | |
| echo "DATABASE_KEY=\"${{ secrets.DATABASE_KEY }}\"" >> .env && \ | |
| echo "OPENAI_API_KEY=\"${{ secrets.OPENAI_API_KEY }}\"" >> .env && \ | |
| echo "PINECONE_API_KEY=\"${{ secrets.PINECONE_API_KEY }}\"" >> .env && \ | |
| echo "PINECONE_INDEX_NAME=\"course-matrix\"" >> .env && \ | |
| echo "BREVO_API_KEY=\"${{ secrets.BREVO_API_KEY }}\"" >> .env && \ | |
| echo "SENDER_EMAIL=\"${{ secrets.SENDER_EMAIL }}\"" >> .env && \ | |
| echo "SENDER_NAME=\"Course Matrix Notifications\"" >> .env | |
| cd ../ | |
| # Logging in to dockerhub | |
| - name: Log in to Docker Hub | |
| uses: docker/login-action@v3 | |
| with: | |
| username: ${{ secrets.DOCKERHUB_USERNAME }} | |
| password: ${{ secrets.DOCKERHUB_TOKEN }} | |
| # Build all required doccker images | |
| - name: Build Docker Image | |
| run: | | |
| cd course-matrix | |
| docker compose build | |
| # Check if images exist before tagging | |
| - name: List Docker Images (Debugging) | |
| run: docker images | |
| # Tags created images with version using github commit tags | |
| - name: Tag Images With Version | |
| run: | | |
| docker tag course-matrix/frontend:latest ${{ secrets.DOCKERHUB_USERNAME }}/course-matrix-frontend:${{ github.sha }} | |
| docker tag course-matrix/backend:latest ${{ secrets.DOCKERHUB_USERNAME }}/course-matrix-backend:${{ github.sha }} | |
| # Push Docker images version to Docker Hub | |
| - name: Push Docker images version to Docker Hub | |
| run: | | |
| docker push ${{ secrets.DOCKERHUB_USERNAME }}/course-matrix-frontend:${{ github.sha }} | |
| docker push ${{ secrets.DOCKERHUB_USERNAME }}/course-matrix-backend:${{ github.sha }} | |
| # Tags created images for the master branch | |
| - name: Tag Images for Master Branch | |
| run: | | |
| docker tag course-matrix/frontend:latest ${{ secrets.DOCKERHUB_USERNAME }}/course-matrix-frontend:master | |
| docker tag course-matrix/backend:latest ${{ secrets.DOCKERHUB_USERNAME }}/course-matrix-backend:master | |
| # Push Docker images to Docker Hub master branch | |
| - name: Push images to Master Branch | |
| run: | | |
| docker push ${{ secrets.DOCKERHUB_USERNAME }}/course-matrix-frontend:master | |
| docker push ${{ secrets.DOCKERHUB_USERNAME }}/course-matrix-backend:master | |
| CD-PIPELINE: | |
| needs: CI-PIPELINE | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout Code | |
| uses: actions/checkout@v3 | |
| - name: Setup SSH Connection | |
| run: | | |
| echo "${{ secrets.GCP_SSH_PRIVATE_KEY }}" > private_key | |
| chmod 600 private_key | |
| - name: Deploy to Google Cloud VM | |
| run: | | |
| ssh -i private_key -o StrictHostKeyChecking=no ${{ secrets.GCP_USERNAME }}@${{ secrets.GCP_VM_IP }} << 'EOF' | |
| cd /home/masahisasekita/term-group-project-c01w25-project-course-matrix || { echo "Error: Directory /root/myapp does not exist!"; exit 1; } | |
| # Pull the latest images | |
| docker pull ${{ secrets.DOCKERHUB_USERNAME }}/course-matrix-frontend:master | |
| docker pull ${{ secrets.DOCKERHUB_USERNAME }}/course-matrix-backend:master | |
| # Stop and remove existing containers | |
| docker compose down | |
| # Start new containers with the latest images | |
| docker compose up -d --pull always | |
| # Confirm running containers | |
| docker ps | |
| EOF |