From 7a390d7c98ce3ebaea8b33bb75df5e0994a36a76 Mon Sep 17 00:00:00 2001 From: ManasC478 Date: Wed, 9 Oct 2024 12:47:13 -0700 Subject: [PATCH 1/6] feat: update actions with db --- .github/workflows/ci.yml | 95 ++++++++++++++++------------------------ server/Dockerfile | 21 +++++++++ 2 files changed, 59 insertions(+), 57 deletions(-) create mode 100644 server/Dockerfile diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index d0cb6e1..d10f709 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -2,76 +2,57 @@ name: CI on: push: - branches: - - "**" # currently run on all branches pull_request: types: [opened, synchronize, reopened] jobs: - test: - runs-on: ubuntu-latest - + typecheck: + runs-on: ubunut-latest steps: - - uses: actions/checkout@v4 - - - uses: ./.github/actions/setup - with: - os: ${{ runner.os }} - - - name: Run tests - run: | - cd server - bun test - + - uses: actions/checkout@v4 + + - name: Typecheck + run: | + docker build --target=typecheck --no-cache . lint: - runs-on: ubuntu-latest + runs-on: ubunut-latest steps: - - uses: actions/checkout@v4 - with: - ref: ${{ github.event.pull_request.head.ref }} - - - uses: ./.github/actions/setup - with: - os: ${{ runner.os }} + - uses: actions/checkout@v4 + + - name: Lint check + run: | + docker build --target=lint --no-cache . - - name: Lint fix - run: | - cd server/ - bun lint --fix - - - name: Commit lint fix - run: | - git config --global user.name "github-actions[bot]" - git config --global user.email "github-actions[bot]@users.noreply.github.com" - if ! git diff --exit-code > /dev/null - then - git commit -a -m "lint fix" - git push - fi + test: + runs-on: ubuntu-latest + services: + db: + image: postgres:latest + ports: + - 5432:5432 + volumes: + - acm_website_db_data:/var/lib/postgresql/data + - ./db-scripts:/docker-entrypoint-initdb.d/ + options: >- + --health-cmd "pg_isready -U postgres" + --health-interval 5s + --health-timeout 5s + --health-retries 5 + --health-start-period 15s + --env-file .env + steps: + - uses: actions/checkout@v4 - - name: Run linter + - name: Test run: | - cd server - bun run lint + docker build --target=test --no-cache . - typecheck: + build: + needs: [typecheck, lint, test] runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - - uses: ./.github/actions/setup - with: - os: ${{ runner.os }} - - - name: Run typecheck + - name: Build run: | - cd server - bun run typecheck - - pr-check: - if: github.event_name == 'pull_request' - needs: [test, lint, typecheck] - runs-on: ubuntu-latest - steps: - - name: PR is ready to be merged - run: echo "All checks passed. PR is ready to be merged." + docker build --target=run --no-cache . \ No newline at end of file diff --git a/server/Dockerfile b/server/Dockerfile new file mode 100644 index 0000000..cbfb763 --- /dev/null +++ b/server/Dockerfile @@ -0,0 +1,21 @@ +FROM oven/bun:alpine as base +RUN apk --no-cache add curl +WORKDIR /app + +FROM base as install +COPY package.json bun.lockb ./ +RUN bun install +COPY . /app + +FROM install as typecheck +RUN bun run typecheck + +FROM install as lint +RUN bun run lint + +FROM install as test +RUN bun test + +FROM install as run +EXPOSE 5001 +CMD ["bun", "run", "dev"] From e9d1dab674b6f3c027d1d0b4d694ca8703170362 Mon Sep 17 00:00:00 2001 From: ManasC478 Date: Wed, 9 Oct 2024 13:10:58 -0700 Subject: [PATCH 2/6] fix: ci action db env --- .github/workflows/ci.yml | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index d10f709..02e80e8 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -7,7 +7,7 @@ on: jobs: typecheck: - runs-on: ubunut-latest + runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 @@ -15,7 +15,7 @@ jobs: run: | docker build --target=typecheck --no-cache . lint: - runs-on: ubunut-latest + runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 @@ -31,15 +31,17 @@ jobs: ports: - 5432:5432 volumes: - - acm_website_db_data:/var/lib/postgresql/data - ./db-scripts:/docker-entrypoint-initdb.d/ + env: + POSTGRES_PASSWORD: postgres + POSTGRES_USER: postgres + POSTGRES_DB: acm_website options: >- --health-cmd "pg_isready -U postgres" --health-interval 5s --health-timeout 5s --health-retries 5 --health-start-period 15s - --env-file .env steps: - uses: actions/checkout@v4 From 8b767de2ed42e52771a140ab7af225748ddbead0 Mon Sep 17 00:00:00 2001 From: ManasC478 Date: Wed, 9 Oct 2024 13:12:54 -0700 Subject: [PATCH 3/6] fix: Dockerfile path --- .github/workflows/ci.yml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 02e80e8..50cb146 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -13,6 +13,7 @@ jobs: - name: Typecheck run: | + cd ./server docker build --target=typecheck --no-cache . lint: runs-on: ubuntu-latest @@ -21,6 +22,7 @@ jobs: - name: Lint check run: | + cd ./server docker build --target=lint --no-cache . test: @@ -47,6 +49,7 @@ jobs: - name: Test run: | + cd ./server docker build --target=test --no-cache . build: @@ -57,4 +60,5 @@ jobs: - name: Build run: | + cd ./server docker build --target=run --no-cache . \ No newline at end of file From 6df3bb22702b872ced41177feb6cc327a29867c9 Mon Sep 17 00:00:00 2001 From: ManasC478 Date: Thu, 10 Oct 2024 18:01:27 -0700 Subject: [PATCH 4/6] add white space --- .github/workflows/ci.yml | 3 ++- server/Dockerfile | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 50cb146..a6640dd 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -61,4 +61,5 @@ jobs: - name: Build run: | cd ./server - docker build --target=run --no-cache . \ No newline at end of file + docker build --target=run --no-cache . + diff --git a/server/Dockerfile b/server/Dockerfile index cbfb763..cec8ca1 100644 --- a/server/Dockerfile +++ b/server/Dockerfile @@ -19,3 +19,4 @@ RUN bun test FROM install as run EXPOSE 5001 CMD ["bun", "run", "dev"] + From 2ec786576a6a61047d54fb1eed195737d30ef77e Mon Sep 17 00:00:00 2001 From: ManasC478 Date: Fri, 11 Oct 2024 00:34:43 -0700 Subject: [PATCH 5/6] fix: revert ci testing --- .github/workflows/ci.yml | 51 +++++++++++++++++++++++++++++++--------- server/Dockerfile | 11 +-------- 2 files changed, 41 insertions(+), 21 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index a6640dd..42d83dd 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -10,20 +10,46 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - - - name: Typecheck + + - uses: ./.github/actions/setup + with: + os: ${{ runner.os }} + + - name: Run typecheck run: | - cd ./server - docker build --target=typecheck --no-cache . + cd server + bun run typecheck + lint: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - - - name: Lint check + with: + ref: ${{ github.event.pull_request.head.ref }} + + - uses: ./.github/actions/setup + with: + os: ${{ runner.os }} + + - name: Lint fix run: | - cd ./server - docker build --target=lint --no-cache . + cd server/ + bun lint --fix + + - name: Commit lint fix + run: | + git config --global user.name "github-actions[bot]" + git config --global user.email "github-actions[bot]@users.noreply.github.com" + if ! git diff --exit-code > /dev/null + then + git commit -a -m "lint fix" + git push + fi + + - name: Run linter + run: | + cd server + bun run lint test: runs-on: ubuntu-latest @@ -46,11 +72,14 @@ jobs: --health-start-period 15s steps: - uses: actions/checkout@v4 + - uses: ./.github/actions/setup + with: + os: ${{ runner.os }} - - name: Test + - name: Run tests run: | - cd ./server - docker build --target=test --no-cache . + cd server + bun test build: needs: [typecheck, lint, test] diff --git a/server/Dockerfile b/server/Dockerfile index cec8ca1..81bacc6 100644 --- a/server/Dockerfile +++ b/server/Dockerfile @@ -5,18 +5,9 @@ WORKDIR /app FROM base as install COPY package.json bun.lockb ./ RUN bun install -COPY . /app - -FROM install as typecheck -RUN bun run typecheck - -FROM install as lint -RUN bun run lint - -FROM install as test -RUN bun test FROM install as run +COPY . /app EXPOSE 5001 CMD ["bun", "run", "dev"] From b3ddeeb0aafd139d52ac3364487b0d8600aafb01 Mon Sep 17 00:00:00 2001 From: ManasC478 Date: Fri, 11 Oct 2024 18:20:41 -0700 Subject: [PATCH 6/6] feat: added dev cd --- .github/workflows/cd-dev.yml | 47 ++++++++++++++++++++++++++++++++++++ docker-compose.yml | 23 +++--------------- nginx.conf | 9 +++++-- server/Dockerfile | 2 +- 4 files changed, 59 insertions(+), 22 deletions(-) create mode 100644 .github/workflows/cd-dev.yml diff --git a/.github/workflows/cd-dev.yml b/.github/workflows/cd-dev.yml new file mode 100644 index 0000000..50fcfd3 --- /dev/null +++ b/.github/workflows/cd-dev.yml @@ -0,0 +1,47 @@ +name: CD-DEV + +on: + workflow_run: + workflows: ["CI"] + types: + - completed + +jobs: + deploy: + name: Deploy + if: github.event.workflow_run.head_branch == 'develop' + runs-on: ubuntu-latest + environment: Development + steps: + - uses: actions/checkout@v4 + + - name: Docker login + uses: docker/login-action@v3 + with: + username: ${{ secrets.DOCKER_USERNAME }} + password: ${{ secrets.DOCKERHUB_PASSWORD }} + + - name: Build image + run: | + cd ./server + docker build -t ${{ secrets.DOCKER_USERNAME }}/${{ vars.IMAGE_NAME }}:latest . + + - name: Push image + run: | + docker push ${{ secrets.DOCKER_USERNAME }}/${{ vars.IMAGE_NAME }}:latest + + - name: Setup ssh + run: | + mkdir -p ~/.ssh + echo "${{ secrets.SSH_PRIVATE_KEY }}" > ~/.ssh/id_rsa + chmod 600 ~/.ssh/id_rsa + + - name: SCP core files + run: | + scp -r docker-compose.yml nginx.conf db-scripts/ .env ${{ vars.SERVER_USER }}@${{ vars.SERVER_IP }} + + - name: SSH into server + run: | + ssh -o StrictHostKeyChecking=no ${{ vars.SERVER_USER }}@${{ vars.SERVER_IP }} + docker compose down + docker compose up --build -d diff --git a/docker-compose.yml b/docker-compose.yml index 9c7e3e3..0fbe802 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -2,7 +2,7 @@ services: nginx: image: nginx:latest ports: - - 8080:8080 + - 80:80 configs: - source: nginx_config target: /etc/nginx/nginx.conf @@ -14,12 +14,7 @@ services: restart: true server: - build: - context: ./server - dockerfile: Dockerfile.dev - volumes: - - ./server:/app - - /app/node_modules + image: manasc478/acm-website-server:latest environment: - NODE_ENV=development env_file: @@ -40,17 +35,11 @@ services: db: image: postgres:latest - # ports: - # - "5432:5432" - environment: - POSTGRES_USER: postgres - POSTGRES_DB: acm_website - POSTGRES_PASSWORD_FILE: /run/secrets/pg_password volumes: - acm_website_db_data:/var/lib/postgresql/data - ./db-scripts:/docker-entrypoint-initdb.d/ - secrets: - - pg_password + env_file: + - .env healthcheck: test: ["CMD", "pg_isready", "-U", "postgres"] interval: 5s @@ -74,7 +63,3 @@ networks: configs: nginx_config: file: ./nginx.conf - -secrets: - pg_password: - file: ./pg_password.txt diff --git a/nginx.conf b/nginx.conf index e9e720e..438fd22 100644 --- a/nginx.conf +++ b/nginx.conf @@ -14,10 +14,15 @@ http { } server { - listen 8080; - + listen 80; + server_name acm-website-server.crabdance.com; + location / { proxy_pass http://app; + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Proto $scheme; } } } \ No newline at end of file diff --git a/server/Dockerfile b/server/Dockerfile index 81bacc6..27173b5 100644 --- a/server/Dockerfile +++ b/server/Dockerfile @@ -7,7 +7,7 @@ COPY package.json bun.lockb ./ RUN bun install FROM install as run -COPY . /app +COPY ./server /app EXPOSE 5001 CMD ["bun", "run", "dev"]