From 1b18f84ef55e752cc60e027ba426ebfd45759b29 Mon Sep 17 00:00:00 2001 From: quannhg Date: Sat, 18 Nov 2023 13:17:59 +0700 Subject: [PATCH] fix(cd): separate ports and docker-compose file for test deployment service and developer service Refactor the project to use distinct ports and Docker-compose files for the test deployment service and the developer service. Introduce separate naming conventions and utilize new environment variables for improved clarity and maintainability. --- .github/workflows/remove-test-deploy.yml | 5 ++- .github/workflows/test-deploy.yml | 33 ++++++++++++------ docker-compose.test-deploy.yml | 43 ++++++++++++++++++++++++ 3 files changed, 70 insertions(+), 11 deletions(-) create mode 100644 docker-compose.test-deploy.yml diff --git a/.github/workflows/remove-test-deploy.yml b/.github/workflows/remove-test-deploy.yml index 4b54f5b..e356cbd 100644 --- a/.github/workflows/remove-test-deploy.yml +++ b/.github/workflows/remove-test-deploy.yml @@ -10,6 +10,9 @@ permissions: name: Remove test deploy +env: + - REGISTRY: ${{vars.REGISTRY_NAME}} + jobs: remove: name: Remove test deploying @@ -18,7 +21,7 @@ jobs: env: TEST_DEPLOY_IMAGE_NAME: ticklabvn/ssps-be-test-deploy - if: github.event_name == 'pull_request' && github.event.action == 'closed' && github.event.pull_request.base.ref == 'staging' + if: github.event_name == 'pull_request' && github.event.action == 'closed' && github.event.pull_request.base.ref == 'test/staging' steps: - name: remove diff --git a/.github/workflows/test-deploy.yml b/.github/workflows/test-deploy.yml index 0ad9c16..5e6e07a 100644 --- a/.github/workflows/test-deploy.yml +++ b/.github/workflows/test-deploy.yml @@ -1,7 +1,8 @@ on: pull_request: types: - - opened + - opened + - synchronize permissions: contents: write @@ -19,6 +20,7 @@ jobs: build: name: Build runs-on: ubuntu-latest + if: github.event.pull_request.base.ref == 'test/staging' steps: - uses: actions/checkout@v3 name: Checkout repository @@ -50,8 +52,8 @@ jobs: runs-on: ubuntu-latest needs: [build] env: - POSTGRES_URL: postgresql://${{ secrets.POSTGRES_USER }}:${{ secrets.POSTGRES_PASSWORD }}@postgres:${{secrets.POSTGRES_PORT_TEST_DEPLOY}}/${{ secrets.POSTGRES_DB }}?schema=public - if: github.event_name == 'pull_request' && github.event.action == 'opened' && github.event.pull_request.base.ref == 'staging' + POSTGRES_URL: postgresql://${{ secrets.POSTGRES_USER }}:${{ secrets.POSTGRES_PASSWORD }}@postgres:${{secrets.POSTGRES_PORT}}/${{ secrets.POSTGRES_DB }}?schema=public + if: github.event.pull_request.base.ref == 'test/staging' steps: - name: Deploy uses: appleboy/ssh-action@master @@ -69,36 +71,47 @@ jobs: mkdir -p $HOME/${{env.IMAGE_NAME}} && cd $HOME/${{env.IMAGE_NAME}} rm -f .env - echo FASTIFY_PORT=${{ secrets.FASTIFY_PORT_TEST_DEPLOY }} >> .env + echo FASTIFY_PORT=${{ secrets.FASTIFY_PORT }} >> .env + echo FASTIFY_TEST_PORT=${{ secrets.FASTIFY_PORT_TEST_DEPLOY }} >> .env + echo >> .env + echo POSTGRES_USER=${{ secrets.POSTGRES_USER }} >> .env echo POSTGRES_PASSWORD=${{ secrets.POSTGRES_PASSWORD }} >> .env echo POSTGRES_DB=${{ secrets.POSTGRES_DB }} >> .env - echo POSTGRES_PORT=${{ secrets.POSTGRES_PORT_TEST_DEPLOY }} >> .env + echo POSTGRES_PORT=${{ secrets.POSTGRES_PORT }} >> .env + echo POSTGRES_TEST_PORT=${{ secrets.POSTGRES_PORT_TEST_DEPLOY }} >> .env echo POSTGRES_URL=${{ env.POSTGRES_URL }} >> .env + echo >> .env + echo CORS_WHITE_LIST=${{ secrets.CORS_WHITE_LIST }} >> .env echo COOKIE_SECRET=${{ secrets.COOKIE_SECRET }} >> .env echo JWT_SECRET=${{ secrets.JWT_SECRET }} >> .env + echo >> .env + echo MINIO_URL=${{ secrets.MINIO_URL }} >> .env echo MINIO_SERVER_ENDPOINT=${{ secrets.MINIO_SERVER_ENDPOINT }} >> .env echo MINIO_PORT=${{ secrets.MINIO_PORT }} >> .env echo MINIO_ACCESS_KEY=${{ secrets.MINIO_ACCESS_KEY }} >> .env echo MINIO_SECRET_KEY=${{ secrets.MINIO_SECRET_KEY }} >> .env echo MINIO_BUCKET_NAME=${{ secrets.MINIO_BUCKET_NAME }} >> .env + echo >> .env + echo CHECKOUT_ENVIRONMENT=${{ vars.CHECKOUT_ENVIRONMENT }} >> .env echo PAYPAL_LIVE_ENDPOINT=${{ vars.PAYPAL_LIVE_ENDPOINT }} >> .env echo PAYPAL_SANDBOX_ENDPOINT=${{ vars.PAYPAL_SANDBOX_ENDPOINT }} >> .env echo PAYPAL_CLIENT_ID=${{ secrets.PAYPAL_CLIENT_ID }} >> .env echo PAYPAL_CLIENT_SECRET=${{ secrets.PAYPAL_CLIENT_SECRET }} >> .env + echo >> .env + echo GOOGLE_CLIENT_ID=${{ secrets.GOOGLE_CLIENT_ID }} >> .env echo GOOGLE_CLIENT_SECRET=${{ secrets.GOOGLE_CLIENT_SECRET }} >> .env echo GOOGLE_REDIRECT_URL=${{ secrets.GOOGLE_REDIRECT_URL }} >> .env echo UI_HOME_URL=${{ secrets.UI_HOME_URL }} >> .env + echo >> .env - # curl -s ${{secrets.DOCKER_COMPOSE_RAW_FILE_URL}} -O -f - # docker compose stop ${{vars.DOCKER_COMPOSE_DEPLOY_SERVICE_NAME}} - # docker compose down --volumes --remove-orphans - # docker compose rm -f ${{vars.DOCKER_COMPOSE_DEPLOY_SERVICE_NAME}} - # docker compose up -d ${{vars.DOCKER_COMPOSE_DEPLOY_SERVICE_NAME}} + curl -s https://${{secrets.READ_FILE_ACCESS_TOKEN}}@raw.githubusercontent.com/${{github.repository}}/${{github.head_ref}}/docker-compose.test-deploy.yml -O -f + docker compose down --volumes --remove-orphans + docker compose -f docker-compose.test-deploy.yml up -d ${{vars.DOCKER_COMPOSE_DEPLOY_SERVICE_NAME}} docker exec -it $(docker ps --filter "ancestor=${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest" -q) /bin/sh -c "npx prisma db seed" diff --git a/docker-compose.test-deploy.yml b/docker-compose.test-deploy.yml new file mode 100644 index 0000000..7a42ec8 --- /dev/null +++ b/docker-compose.test-deploy.yml @@ -0,0 +1,43 @@ +version: "3.5" +services: + postgres: + image: postgres:15 + container_name: ssps-db-test-deploy + env_file: + - .env + ports: + - "${POSTGRES_TEST_PORT:-6432}:5432" + volumes: + - postgres_db:/var/lib/postgresql/data + restart: always + networks: + - ssps + + #docker build -t ssps-be:test-deploy . + fastify: + image: ghcr.io/ticklabvn/ssps-be-test-deploy:latest + container_name: ssps-be-test-deploy + env_file: + - .env + ports: + - "${FASTIFY_TEST_PORT:-9080}:8080" + volumes: + - .env:/app/.env + depends_on: + - postgres + restart: always + networks: + - ssps + +volumes: + postgres_db: + +networks: + ssps: + name: ssps-test-deploy + external: true + +# run the following command after docker compose up to create seed data: +# docker-compose exec fastify npx prisma db seed +# if the ssps network haven't created yet, run the following command: +# docker network create --driver bridge ssps-test-deploy