diff --git a/.github/workflows/deploy-dev-vote-backend.yml b/.github/workflows/deploy-dev-vote-backend.yml index e77a0ef..d74e0e3 100644 --- a/.github/workflows/deploy-dev-vote-backend.yml +++ b/.github/workflows/deploy-dev-vote-backend.yml @@ -58,7 +58,7 @@ jobs: id: deploy-to-webapp uses: azure/webapps-deploy@v2 with: - app-name: "ntnui-vote-backend-dev" - slot-name: "Production" + app-name: "vote-backend/dev" + slot-name: "development" publish-profile: ${{ secrets.AZURE_BACKEND_DEV_PUBLISH_PROFILE }} package: "./backend" diff --git a/.github/workflows/deploy-prod-vote-backend.yml b/.github/workflows/deploy-prod-vote-backend.yml new file mode 100644 index 0000000..8d1f014 --- /dev/null +++ b/.github/workflows/deploy-prod-vote-backend.yml @@ -0,0 +1,64 @@ +# Docs for the Azure Web Apps Deploy action: https://github.com/Azure/webapps-deploy +# More GitHub Actions for Azure: https://github.com/Azure/actions + +name: Build and deploy prod backend + +on: + push: + branches: + - main + workflow_dispatch: + +jobs: + build: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v3 + + - name: Set up Node.js version + uses: actions/setup-node@v3 + with: + node-version: "18.x" + + - name: npm install, build, and test + run: | + cd backend + npm install + npm run build + + - name: Zip artifact for deployment job + run: zip -r -q release.zip ./* + + - name: Upload artifact for deployment job + uses: actions/upload-artifact@v3 + with: + name: node-app + path: release.zip + + deploy: + runs-on: ubuntu-latest + needs: build + environment: + name: "Development" + url: ${{ steps.deploy-to-webapp.outputs.webapp-url }} + + steps: + - name: Download artifact from build job + uses: actions/download-artifact@v3 + with: + name: node-app + + - name: Unzip artifact for deployment job + run: | + unzip -q release.zip + rm release.zip + + - name: "Deploy to Azure Web App" + id: deploy-to-webapp + uses: azure/webapps-deploy@v2 + with: + app-name: "vote-backend" + slot-name: "production" + publish-profile: ${{ secrets.AZURE_BACKEND_PROD_PUBLISH_PROFILE }} + package: "./backend" diff --git a/.github/workflows/deploy-prod-vote-frontend.yml b/.github/workflows/deploy-prod-vote-frontend.yml new file mode 100644 index 0000000..def911b --- /dev/null +++ b/.github/workflows/deploy-prod-vote-frontend.yml @@ -0,0 +1,52 @@ +name: Build and deploy dev frontend + +on: + push: + branches: + - main + pull_request: + types: [opened, synchronize, reopened, closed] + branches: + - main + paths-ignore: + - "**" + - "!frontend/**" + workflow_dispatch: + +jobs: + build_and_deploy_job: + if: github.event_name == 'push' || (github.event_name == 'pull_request' && github.event.action != 'closed') + runs-on: ubuntu-latest + name: Build and Deploy Job + steps: + - uses: actions/checkout@v3 + with: + submodules: true + - name: Build And Deploy + id: builddeploy + uses: Azure/static-web-apps-deploy@v1 + with: + azure_static_web_apps_api_token: ${{ secrets.AZURE_FRONTEND_PROD_TOKEN }} + repo_token: ${{ secrets.GITHUB_TOKEN }} # Used for Github integrations (i.e. PR comments) + action: "upload" + ###### Repository/Build Configurations - These values can be configured to match your app requirements. ###### + # For more information regarding Static Web App workflow configurations, please visit: https://aka.ms/swaworkflowconfig + app_location: "/frontend" # App source code path + api_location: "" # Api source code path - optional + output_location: "/dist" # Built app content directory - optional + ###### End of Repository/Build Configurations ###### + env: + VITE_BACKEND_URL: https://api.vote.ntnui.no + VITE_SOCKET_URL: wss://api.vote.ntnui.no/lobby + + close_pull_request_job: + if: github.event_name == 'pull_request' && github.event.action == 'closed' + runs-on: ubuntu-latest + name: Close Pull Request Job + steps: + - name: Close Pull Request + id: closepullrequest + uses: Azure/static-web-apps-deploy@v1 + with: + azure_static_web_apps_api_token: ${{ secrets.AZURE_FRONTEND_PROD_TOKEN }} + action: "close" diff --git a/backend/utils/db.ts b/backend/utils/db.ts index 01c07d3..9078947 100644 --- a/backend/utils/db.ts +++ b/backend/utils/db.ts @@ -16,7 +16,14 @@ const mongoConnect = async () => { console.error("ERROR: ", e); } - database = mongoose.connection; + if (process.env.NODE_ENV === "development") { + database = mongoose.connection.useDb("development"); + } else if (process.env.NODE_ENV === "production") { + database = mongoose.connection.useDb("production"); + } else { + database = mongoose.connection; + } + database.once("open", async () => { console.log("Connected to MongoDB 🌱"); });