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/.github/workflows/ci.yml b/.github/workflows/ci.yml index d0cb6e1..42d83dd 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -2,76 +2,93 @@ name: CI on: push: - branches: - - "**" # currently run on all branches pull_request: types: [opened, synchronize, reopened] jobs: - test: + typecheck: runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v4 + - uses: actions/checkout@v4 + + - uses: ./.github/actions/setup + with: + os: ${{ runner.os }} - - uses: ./.github/actions/setup - with: - os: ${{ runner.os }} - - - name: Run tests - run: | - cd server - bun test + - name: Run typecheck + run: | + cd server + bun run typecheck lint: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v4 - with: - ref: ${{ github.event.pull_request.head.ref }} + - uses: actions/checkout@v4 + with: + ref: ${{ github.event.pull_request.head.ref }} - - uses: ./.github/actions/setup - with: - os: ${{ runner.os }} + - uses: ./.github/actions/setup + with: + os: ${{ runner.os }} - - name: Lint fix - run: | - cd server/ - bun lint --fix + - 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 + - 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 + - name: Run linter + run: | + cd server + bun run lint - typecheck: + test: runs-on: ubuntu-latest + services: + db: + image: postgres:latest + ports: + - 5432:5432 + volumes: + - ./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 steps: - - uses: actions/checkout@v4 - + - uses: actions/checkout@v4 - uses: ./.github/actions/setup with: os: ${{ runner.os }} - - name: Run typecheck + - name: Run tests run: | cd server - bun run typecheck + bun test - pr-check: - if: github.event_name == 'pull_request' - needs: [test, lint, typecheck] + build: + needs: [typecheck, lint, test] runs-on: ubuntu-latest steps: - - name: PR is ready to be merged - run: echo "All checks passed. PR is ready to be merged." + - uses: actions/checkout@v4 + + - name: Build + run: | + cd ./server + docker build --target=run --no-cache . + 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 new file mode 100644 index 0000000..27173b5 --- /dev/null +++ b/server/Dockerfile @@ -0,0 +1,13 @@ +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 + +FROM install as run +COPY ./server /app +EXPOSE 5001 +CMD ["bun", "run", "dev"] +