使用 gitlab cicd 通过 nodemon 部署 express

Deploy express with nodemon using gitlab cicd

我正在尝试使用 gitlab 管道在 AWS 上部署我的 express API。由于是开发服务器,服务器通过nodemon.

进行管理

这是我的 Dockerfile:

FROM node:15 AS base

WORKDIR /usr/src/app

COPY . .

RUN npm i

FROM base AS production

RUN yarn build

这是 docker-compose.yml:

version: '3.8'
services: 
  api:
    container_name: api
    build:
      context: .
      dockerfile: Dockerfile
      target: base
      args:
        PORT: ${PORT}
    restart: always
    ports:
      - 80:${PORT}
    command: yarn start:dev

这是 .gitlab-ci.yml:

image: node:latest

stages:
  - deploy-dev
  - start-dev

deploy_dev:
  stage: deploy-dev
  image: node
  before_script:
    - echo " Starting deployment [development mode]..."
    - 'which ssh-agent || (apt-get update -y && apt-get install openssh-client -y)'
    - eval $(ssh-agent -s)
    - mkdir -p ~/.ssh
    - chmod 700 ~/.ssh
    - echo "$KEY_DEV" > "$(pwd)/key.pem"
    - chmod 400 $(pwd)/key.pem
    - echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config
    - apt-get update -y
    - apt-get -y install rsync
  script:
    - ssh -T -i $(pwd)/key.pem user@$DEV
    - rsync -zvhr -auv --exclude 'node_modules' -e "ssh -T -i $(pwd)/key.pem" ./auth user@$DEV:/home/user/services
    - echo " Deployment completed..."
    - echo " Starting auth services..."
    - ssh -T -i $(pwd)/key.pem user@$DEV "cd ./services/auth/; docker-compose --env-file ./config/.development.env down; docker-compose --env-file ./config/.development.env up --build;"
  only: ['dev']

总的来说它可以工作,但问题是一旦容器启动,管道总是保持 运行。我该如何解决这个问题?

尝试 运行在分离模式下 docker 撰写 (docker-compose up --detach)。 这将导致容器在后台 运行 (非阻塞调用)。 可以找到更多信息 here