Commit 370f1e7c authored by Anthony Jacob's avatar Anthony Jacob
Browse files

first CI/CD

parent ed1d542f
Loading
Loading
Loading
Loading
Loading

.dockerignore

0 → 100644
+11 −0
Original line number Diff line number Diff line
.env
.env.*
node_modules
dist
logs
coverage
.git
.gitignore
Dockerfile
README.md
*.log

.gitlab-ci.yml

0 → 100644
+57 −0
Original line number Diff line number Diff line
# For more information, see: https://docs.gitlab.com/ee/ci/yaml/index.html#stages
# predefined variables https://docs.gitlab.com/ee/ci/variables/predefined_variables.html

stages:
  - publish
  - deploy

publish:
  stage: publish
  image: docker:cli
  services:
    - docker:dind
  variables:
    DOCKER_IMAGE_NAME: $CI_REGISTRY_IMAGE:latest
  before_script:
    - echo "CI_REGISTRY_IMAGE $CI_REGISTRY_IMAGE"
    - echo "DOCKER_IMAGE_NAME $DOCKER_IMAGE_NAME"
    - echo "CI_REGISTRY $CI_REGISTRY"
    - echo "DOCKER_HOST $DOCKER_HOST"
    - echo "CI_COMMIT_BRANCH $CI_COMMIT_BRANCH"
    - echo "CI_DEFAULT_BRANCH $CI_DEFAULT_BRANCH"
    - docker info
    - docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY

  rules:
    - if: $CI_COMMIT_BRANCH == "main"
      exists:
        - Dockerfile

  environment: Development
  script:
    - echo "Build Docker image..."
    - docker build -t $DOCKER_IMAGE_NAME . -f Dockerfile
    - echo "Publish Docker image..."
    - docker push $DOCKER_IMAGE_NAME
    - echo "image successfully published."

deploy-demo:
  stage: deploy
  image: ubuntu:24.04
  variables:
    DOCKER_IMAGE_NAME: $CI_REGISTRY_IMAGE:latest
  before_script:
    - apt-get -yq update
    - apt-get -yqq install ssh
    - install -m 600 -D /dev/null ~/.ssh/id_rsa
    - echo "$SSH_KEY" | base64 -d > ~/.ssh/id_rsa
    - ssh-keyscan -p $SSH_PORT -H $SSH_HOST  > ~/.ssh/known_hosts
    - cat ~/.ssh/known_hosts
  script:
    - ssh $SSH_USER@$SSH_HOST -p $SSH_PORT "docker service update --force --image $DOCKER_IMAGE_NAME stack_trading_bot"
  after_script:
    - rm -rf ~/.ssh
  rules:
    - if: $CI_COMMIT_BRANCH == "main"

  environment: Development

Dockerfile

0 → 100644
+21 −0
Original line number Diff line number Diff line
# syntax=docker/dockerfile:1

FROM node:20-alpine AS deps
WORKDIR /app
COPY package*.json ./
RUN npm ci

FROM node:20-alpine AS build
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY . .
RUN npm run build

FROM node:20-alpine AS runner
WORKDIR /app
ENV NODE_ENV=production
COPY package*.json ./
RUN npm ci --omit=dev
COPY --from=build /app/dist ./dist
EXPOSE 3000
CMD ["node", "dist/main"]