add dockerfile and github actions workflow for deploy

This commit is contained in:
2024-04-13 13:01:49 +02:00
parent 616626b4ce
commit 82484be19c
4 changed files with 109 additions and 0 deletions

4
.dockerignore Normal file
View File

@@ -0,0 +1,4 @@
node_modules
npm-debug.log
build
tmp

38
.github/workflows/deploy.yml vendored Normal file
View File

@@ -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

31
Dockerfile Normal file
View File

@@ -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"]

36
docker-compose.yml Normal file
View File

@@ -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