diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..b09285f --- /dev/null +++ b/.dockerignore @@ -0,0 +1,4 @@ +node_modules +npm-debug.log +build +tmp diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml new file mode 100644 index 0000000..db1b248 --- /dev/null +++ b/.github/workflows/deploy.yml @@ -0,0 +1,38 @@ +name: Deploy to DigitalOcean + +on: + push: + branches: + - master + +jobs: + build-and-deploy: + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v2 + + - name: Log in to Docker Hub + uses: docker/login-action@v1 + with: + username: ${{ secrets.DOCKER_USERNAME }} + password: ${{ secrets.DOCKER_PASSWORD }} + + - name: Build and push Docker image + uses: docker/build-push-action@v2 + with: + push: true + tags: ershisan99/flashcards:latest + + - name: SSH and Deploy to Droplet + uses: appleboy/ssh-action@master + with: + host: ${{ secrets.DROPLET_IP }} + username: root + password: ${{ secrets.SSH_PASSWORD }} # Using password instead of key + script: | + docker pull ershisan99/flashcards:latest + docker stop flashcards || true + docker rm flashcards || true + docker run -d --name flashcards -p 80:3333 ershisan99/flashcards:latest diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..265d9a5 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,31 @@ +# Use the official Node.js LTS as a parent image +FROM node:lts-alpine + +# Set the working directory in the Docker container +WORKDIR /usr/src/app + +# Install pnpm +RUN npm install -g pnpm + +# Copy package.json, pnpm-lock.yaml (or package-lock.json if you use it) to the Docker container +COPY package.json pnpm-lock.yaml ./ + +# Install dependencies +RUN pnpm install --frozen-lockfile + +# Copy the rest of your application's code into the Docker container +COPY . . + +# Generate Prisma client +RUN npx prisma generate + +# Build your NestJS application +RUN pnpm run build + +ENV PORT 3333 + +# Expose the port the app runs on +EXPOSE 3333 + +# Command to run when starting the container +CMD ["node", "dist/main"] diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..b620bf5 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,36 @@ +stages: + - build + - deploy + +variables: + IMAGE_NAME: registry.gitlab.com/cargo-solutions/api + IMAGE_TAG: ${CI_COMMIT_SHA} + +build: + stage: build + image: docker:stable + services: + - docker:dind + script: + - sudo apt-get update && sudo apt-get install -y python3-pip python3-dev libffi-dev libssl-dev gcc libc6-dev make && sudo rm -rf /var/lib/apt/lists/* + - docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY_IMAGE + - docker build -t api:latest . + - docker tag api registry.gitlab.com/r-et-d/todo-api:latest + - docker tag api:latest registry.gitlab.com/r-et-d/todo-api:${CI_COMMIT_SHA} + - docker push registry.gitlab.com/r-et-d/todo-api:latest + - docker push registry.gitlab.com/r-et-d/todo-api:${CI_COMMIT_SHA} + only: + - master + +deploy: + stage: deploy + image: ubuntu:20.04 + script: + - export DEBIAN_FRONTEND=noninteractive + - sudo apt-get update -y + - sudo apt-get install -y apt-transport-https ca-certificates curl software-properties-common openssh-client + - echo "$SSH_PRIVATE_KEY" | tr -d '\r' > id_rsa + - sudo chmod 600 id_rsa + - ssh -i id_rsa -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null $SSH_USER@$SSH_HOST "\cd /home/ci && \git pull && \cd && \docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $IMAGE_NAME && \bash /home/ci/docker-compose restart" + only: + - master \ No newline at end of file