diff --git a/.gitea/workflows/deploy.yml b/.gitea/workflows/main-deploy.yml similarity index 100% rename from .gitea/workflows/deploy.yml rename to .gitea/workflows/main-deploy.yml diff --git a/.gitea/workflows/new-deploy.yml b/.gitea/workflows/new-deploy.yml new file mode 100644 index 0000000..6c93c66 --- /dev/null +++ b/.gitea/workflows/new-deploy.yml @@ -0,0 +1,129 @@ +name: Build & Deploy Costco Grocery List + +on: + push: + branches: [ "main-new" ] + +env: + REGISTRY: git.nicosaya.com/nalalangan/costco-grocery-list + IMAGE_TAG: main-new + +jobs: + build: + runs-on: ubuntu-latest + + steps: + - name: Checkout repo + uses: actions/checkout@v3 + + - name: Set up Node.js + uses: actions/setup-node@v3 + with: + node-version: 20 + + # ------------------------- + # 🔹 BACKEND TESTS + # ------------------------- + - name: Install backend dependencies + working-directory: backend + run: npm ci + + - name: Run backend tests + working-directory: backend + run: npm test --if-present + + # ------------------------- + # 🔹 Docker Login + # ------------------------- + - name: Docker login + run: | + echo "${{ secrets.REGISTRY_PASS }}" | docker login $REGISTRY \ + -u "${{ secrets.REGISTRY_USER }}" --password-stdin + + # ------------------------- + # 🔹 Build Backend Image + # ------------------------- + - name: Build Backend Image + run: | + docker build \ + -t $REGISTRY/backend:${{ github.sha }} \ + -t $REGISTRY/backend:${{ env.IMAGE_TAG }} \ + -f backend/Dockerfile backend/ + + - name: Push Backend Image + run: | + docker push $REGISTRY/backend:${{ github.sha }} + docker push $REGISTRY/backend:${{ env.IMAGE_TAG }} + + # ------------------------- + # 🔹 Build Frontend Image + # ------------------------- + - name: Build Frontend Image + run: | + docker build \ + -t $REGISTRY/frontend:${{ github.sha }} \ + -t $REGISTRY/frontend:${{ env.IMAGE_TAG }} \ + -f frontend/Dockerfile.dev frontend/ + + - name: Push Frontend Image + run: | + docker push $REGISTRY/frontend:${{ github.sha }} + docker push $REGISTRY/frontend:${{ env.IMAGE_TAG }} + + deploy: + needs: build + runs-on: ubuntu-latest + + steps: + - name: Checkout repo + uses: actions/checkout@v3 + + - name: Install SSH key + run: | + mkdir -p ~/.ssh + echo "${{ secrets.DEPLOY_KEY }}" > ~/.ssh/id_ed25519 + chmod 600 ~/.ssh/id_ed25519 + ssh-keyscan -H "${{ secrets.DEPLOY_HOST }}" >> ~/.ssh/known_hosts + + # --------------------------------------------------------- + # 1. Upload docker-compose.yml to the production directory + # --------------------------------------------------------- + - name: Upload docker-compose.yml + run: | + ssh ${{ secrets.DEPLOY_USER }}@${{ secrets.DEPLOY_HOST }} "mkdir -p /opt/costco-app-new" + scp docker-compose.new.yml \ + ${{ secrets.DEPLOY_USER }}@${{ secrets.DEPLOY_HOST }}:/opt/costco-app-new/docker-compose.yml + + # --------------------------------------------------------- + # 2. Deploy using the uploaded compose file + # --------------------------------------------------------- + - name: Deploy via SSH + run: | + ssh ${{ secrets.DEPLOY_USER }}@${{ secrets.DEPLOY_HOST }} << 'EOF' + cd /opt/costco-app-new + docker compose pull + docker compose up -d --remove-orphans + docker image prune -f + EOF + + notify: + needs: deploy + runs-on: ubuntu-latest + if: always() + + steps: + - name: Notify ntfy + run: | + STATUS="${{ needs.deploy.result }}" + echo "Deployment job finished with status: $STATUS" + + if [ "$STATUS" = "success" ]; then + MSG="🚀 Costco App Deployment succeeded: $IMAGE_NAME:${{ github.sha }}" + else + MSG="❌ Costco App Deployment FAILED: $IMAGE_NAME:${{ github.sha }}" + fi + + curl -d "$MSG" \ + https://ntfy.nicosaya.com/gitea + + \ No newline at end of file diff --git a/docker-compose.new.yml b/docker-compose.new.yml new file mode 100644 index 0000000..5343dc3 --- /dev/null +++ b/docker-compose.new.yml @@ -0,0 +1,18 @@ +services: + backend: + image: git.nicosaya.com/nalalangan/costco-grocery-list/backend:main-new + restart: always + env_file: + - ./backend.env + ports: + - "5001:5000" + + frontend: + image: git.nicosaya.com/nalalangan/costco-grocery-list/frontend:main-new + restart: always + env_file: + - ./frontend.env + ports: + - "3001:5173" + depends_on: + - backend