From 2ff791e179014a46e6b736d3466ad38c9f65a173 Mon Sep 17 00:00:00 2001 From: apozdniakov Date: Tue, 2 Jul 2024 22:44:41 +0200 Subject: [PATCH] extract backend url to variable --- .github/workflows/build.yml | 14 ++++++++++- .github/workflows/release.yml | 14 ++++++++++- .jenkins/deploy-prod.Jenkinsfile | 2 +- .jenkins/deploy-uat.Jenkinsfile | 2 +- deploy/deploy_prod.yml | 4 ++++ deploy/deploy_uat.yml | 4 ++++ deploy/templates/docker-compose.yml.j2 | 2 +- docker-compose.yml | 1 + frontend/Dockerfile | 3 +++ frontend/src/app/api/api-domain-service.ts | 27 ++++++++++++---------- 10 files changed, 56 insertions(+), 17 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index d889770..a5feab5 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -30,6 +30,18 @@ jobs: context: ./frontend file: ./frontend/Dockerfile push: false - tags: retypeme/retypeme-frontend:latest + tags: retypeme/retypeme-frontend-uat:latest build-args: | NEXT_PUBLIC_ENV_LOCAL_INFURA_AMOY_API_KEY=${{ secrets.NEXT_PUBLIC_ENV_LOCAL_INFURA_AMOY_API_KEY }} + NEXT_PUBLIC_API_DOMAIN: ${{NEXT_PUBLIC_API_DOMAIN_UAT}} + + - name: Build Docker frontend image + uses: docker/build-push-action@v5 + with: + context: ./frontend + file: ./frontend/Dockerfile + push: false + tags: retypeme/retypeme-frontend-prod:latest + build-args: | + NEXT_PUBLIC_ENV_LOCAL_INFURA_AMOY_API_KEY=${{ secrets.NEXT_PUBLIC_ENV_LOCAL_INFURA_AMOY_API_KEY }} + NEXT_PUBLIC_API_DOMAIN: ${{NEXT_PUBLIC_API_DOMAIN_PROD}} diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index ec12123..2aeebc1 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -66,6 +66,18 @@ jobs: context: ./frontend file: ./frontend/Dockerfile push: true - tags: retypeme/retypeme-frontend:${{ env.newTag }}, retypeme/retypeme-frontend:latest + tags: retypeme/retypeme-frontend-uat:${{ env.newTag }}, retypeme/retypeme-frontend-uat:latest build-args: | NEXT_PUBLIC_ENV_LOCAL_INFURA_AMOY_API_KEY=${{ secrets.NEXT_PUBLIC_ENV_LOCAL_INFURA_AMOY_API_KEY }} + NEXT_PUBLIC_API_DOMAIN: ${{NEXT_PUBLIC_API_DOMAIN_UAT}} + + - name: Build and push Docker frontend image + uses: docker/build-push-action@v5 + with: + context: ./frontend + file: ./frontend/Dockerfile + push: true + tags: retypeme/retypeme-frontend-prod:${{ env.newTag }}, retypeme/retypeme-frontend-prod:latest + build-args: | + NEXT_PUBLIC_ENV_LOCAL_INFURA_AMOY_API_KEY=${{ secrets.NEXT_PUBLIC_ENV_LOCAL_INFURA_AMOY_API_KEY }} + NEXT_PUBLIC_API_DOMAIN: ${{NEXT_PUBLIC_API_DOMAIN_PROD}} diff --git a/.jenkins/deploy-prod.Jenkinsfile b/.jenkins/deploy-prod.Jenkinsfile index e7293d3..6d44e4c 100644 --- a/.jenkins/deploy-prod.Jenkinsfile +++ b/.jenkins/deploy-prod.Jenkinsfile @@ -11,7 +11,7 @@ pipeline { steps { withCredentials([string(credentialsId: 'JASYPT_ENCRYPTOR_PASSWORD_PROD', variable: 'JASYPT_ENCRYPTOR_PASSWORD')]) { sh ''' - /var/lib/jenkins/.local/bin/ansible-playbook -i /var/lib/jenkins/inventory.yml --extra-vars "version=${VERSION}" deploy/deploy_prod.yml + /var/lib/jenkins/.local/bin/ansible-playbook -i /var/lib/jenkins/inventory.yml --extra-vars "env=prod" --extra-vars "version=${VERSION}" deploy/deploy_prod.yml ''' } } diff --git a/.jenkins/deploy-uat.Jenkinsfile b/.jenkins/deploy-uat.Jenkinsfile index 9d6e5b7..2b9e6ef 100644 --- a/.jenkins/deploy-uat.Jenkinsfile +++ b/.jenkins/deploy-uat.Jenkinsfile @@ -11,7 +11,7 @@ pipeline { steps { withCredentials([string(credentialsId: 'JASYPT_ENCRYPTOR_PASSWORD_UAT', variable: 'JASYPT_ENCRYPTOR_PASSWORD')]) { sh ''' - /var/lib/jenkins/.local/bin/ansible-playbook -i /var/lib/jenkins/inventory.yml --extra-vars "version=${VERSION}" deploy/deploy_uat.yml + /var/lib/jenkins/.local/bin/ansible-playbook -i /var/lib/jenkins/inventory.yml --extra-vars "env=uat" --extra-vars "version=${VERSION}" deploy/deploy_uat.yml ''' } } diff --git a/deploy/deploy_prod.yml b/deploy/deploy_prod.yml index f322103..6ad8935 100755 --- a/deploy/deploy_prod.yml +++ b/deploy/deploy_prod.yml @@ -12,6 +12,10 @@ debug: var: version + - name: Debug env variable + debug: + var: env + - name: ensure directory retypeme exists file: path: /home/wheel/retypeme diff --git a/deploy/deploy_uat.yml b/deploy/deploy_uat.yml index 5393d17..ca0aa66 100755 --- a/deploy/deploy_uat.yml +++ b/deploy/deploy_uat.yml @@ -12,6 +12,10 @@ debug: var: version + - name: Debug env variable + debug: + var: env + - name: ensure directory retypeme exists file: path: /home/wheel/retypeme diff --git a/deploy/templates/docker-compose.yml.j2 b/deploy/templates/docker-compose.yml.j2 index c11fc39..aab2327 100644 --- a/deploy/templates/docker-compose.yml.j2 +++ b/deploy/templates/docker-compose.yml.j2 @@ -15,7 +15,7 @@ services: - redis ui: - image: retypeme/retypeme-frontend:{{ version }} + image: retypeme/retypeme-frontend-{{ env }}:{{ version }} restart: always ports: - "3001:3000" diff --git a/docker-compose.yml b/docker-compose.yml index 96f3e85..586da86 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -27,6 +27,7 @@ services: # context: ./frontend # args: # NEXT_PUBLIC_ENV_LOCAL_INFURA_AMOY_API_KEY: ${NEXT_PUBLIC_ENV_LOCAL_INFURA_AMOY_API_KEY} +# NEXT_PUBLIC_API_DOMAIN: ${NEXT_PUBLIC_API_DOMAIN} depends_on: - api diff --git a/frontend/Dockerfile b/frontend/Dockerfile index a84accb..4723fd8 100644 --- a/frontend/Dockerfile +++ b/frontend/Dockerfile @@ -4,6 +4,9 @@ FROM node:21.4.0-alpine AS build ARG NEXT_PUBLIC_ENV_LOCAL_INFURA_AMOY_API_KEY ENV NEXT_PUBLIC_ENV_LOCAL_INFURA_AMOY_API_KEY ${NEXT_PUBLIC_ENV_LOCAL_INFURA_AMOY_API_KEY} +ARG NEXT_PUBLIC_API_DOMAIN +ENV NEXT_PUBLIC_API_DOMAIN ${NEXT_PUBLIC_API_DOMAIN} + WORKDIR /app # Copy package.json and package-lock.json to install dependencies diff --git a/frontend/src/app/api/api-domain-service.ts b/frontend/src/app/api/api-domain-service.ts index ebd3764..54eda38 100644 --- a/frontend/src/app/api/api-domain-service.ts +++ b/frontend/src/app/api/api-domain-service.ts @@ -1,20 +1,23 @@ +const domain = process.env.NEXT_PUBLIC_API_DOMAIN; + export default class ApiDomainService { - private readonly uiDomain: string; - private readonly domains: Map; + + private readonly wsUrl; + private readonly restUrl; constructor() { - this.uiDomain = window.location.hostname; - this.domains = new Map([ - ["retypeme.apozdniakov.com", "retypeme-api.apozdniakov.com"], - ["retypeme.vercel.app", "retypeme-api.apozdniakov.com"], - ["retypeme.xyz", "app.retypeme.xyz"], - ]); + console.log("API_DOMAIN: ", domain); + + this.wsUrl = this.getUrl(true); + this.restUrl = this.getUrl(false); + + console.log("WebSocket URL: ", this.wsUrl); + console.log("REST URL: ", this.restUrl); } private getUrl(isWebSocket: boolean): string { - let domain = this.domains.get(this.uiDomain); let api = "" - if (domain === undefined) { + if (domain === undefined || domain.includes("localhost")) { api = isWebSocket ? "ws://localhost:8080/api/ws" : "http://localhost:8080/api"; } else { api = isWebSocket ? `wss://${domain}/api/ws` : `https://${domain}/api`; @@ -23,10 +26,10 @@ export default class ApiDomainService { } getWebSocketUrl(): string { - return this.getUrl(true); + return this.wsUrl; } getRestUrl(): string { - return this.getUrl(false); + return this.restUrl; } }