Dockerfile.

Dockerfile.

다음 구성을 사용하여 GitLab, Docker 및 AWS 사이에 CI(지속적 통합)를 만들었습니다.

Dockerfile.

# install node.js
FROM node:latest

# create necessary directories and
# permissions
RUN mkdir -p /home/node/{project_name}/node_modules && chown -R node.node /home/node/{project_name}

# copy package.json files in directory
# check and switch to node user.
USER node

# copy project files.
COPY . /home/node/{project_name}

# switch to working directory
WORKDIR /home/node/{project_name}

docker-compose.yml

version: '3'
services:
  server:
    container_name: truckpeserver
    restart: always
    build: .
    command: 'npm run dev'
    links:
      - redis
      - prisma
    env_file:
      - ./.env
    volumes:
      - .:/home/node/truckpeserver/
    working_dir: /home/node/truckpeserver/
    ports:
      - '3000:3000'
  redis:
    container_name: "redisserver"
    image: redis:latest
    restart: always
    command: ["redis-server", "--bind", "redis", "--port", "6379"]
  prisma:
    image: prismagraphql/prisma:1.34
    restart: always
    ports:
      - '4466:4466'
    environment:
      PRISMA_CONFIG: |
        port: 4466
        databases:
          default:
            connector: mysql
            host: mysql
            port: 3306
            user: root
            password: prisma
  mysql:
    image: mysql:5.7
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: prisma
    volumes:
      - mysql:/var/lib/mysql
volumes:
  mysql: ~

gitlab-ci.yml

# Node docker image on which this would be run
image: node:10.15.3

#This command is run before actual stages start running
before_script:
  - 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )'
  - 'apt-get update && apt-get upgrade -y && apt-get install git -y'

stages:
  - test
  - deploy

# lint and test are two different jobs in the same stage.
# This allows us to run these two in parallel and making build faster

# Job 1:
deployToAWS:
  only:
    - master
  stage: deploy
  script:
    - bash ./deploy.sh

배포.sh

#!/bin/bash

# any future command that fails will exit the script
set -e

# Lets write the public key of our aws instance
eval $(ssh-agent -s)
echo "$PRIVATE_KEY" | tr -d '\r' | ssh-add - > /dev/null

# ** Alternative approach
# echo -e "$PRIVATE_KEY" > /root/.ssh/id_rsa
# chmod 600 /root/.ssh/id_rsa
# ** End of alternative approach

# disable the host key checking.
chmod 755 ./disableHostKeyChecking.sh
./disableHostKeyChecking.sh

# we have already setup the DEPLOY_SERVE R in our gitlab settings which is a
# comma seperated values of ip addresses.
DEPLOY_SERVERS=$DEPLOY_SERVERS

# lets split this string and convert this into array
# In UNIX, we can use this commond to do this
# ${string//substring/replacement}
# our substring is "," and we replace it with nothing.
ALL_SERVERS=(${DEPLOY_SERVERS//,/ })
echo "ALL_SERVERS ${ALL_SERVERS}"

# Lets iterate over this array and ssh into each EC2 instance
# Once inside the server, run updateAndRestart.sh
for server in "${ALL_SERVERS[@]}"
do
  echo "deploying to ${server}"
  ssh ubuntu@${server} 'bash' < ./production.sh
done

생산.sh

#!/bin/bash

# exist script any error occur
set -e

if [ -d "./truckpeserver" ]

# if directory exists than kill old
# containers
then
  # move to server directory.
  cd ./truckpeserver

  # down all services.
  docker-compose down

  # get out of directory and remove directory
  cd .. && rm -rf ./truckpeserver

fi

# pull truckpeserver
git clone [email protected]:rootandleaves/truckpeserver.git

# move to server directory.
cd ./truckpeserver

# install node_modules.
yarn

# build and up all containers.
docker-compose build && docker-compose up

production.shSSH를 통해 AWS에 애플리케이션을 배포합니다 . 이제 문제는 git에 애플리케이션을 게시하면 실행기가 SSH를 통해 AWS 서버에 연결하고 애플리케이션 설치(복제, 원사 등) 프로세스를 시작하는 CI 작업을 실행하게 된다는 것입니다. 하지만 애플리케이션이 성공적으로 실행되면 GitLab 실행기에 SSH를 종료하도록 어떻게 지시합니까?

답변1

내 추측은 - 백그라운드에서 앱을 실행하지 않고 앱이 실행되는 동안 스크립트가 기다리고 있다는 것입니다.

다음과 같은 것을 교체해야 합니다:

yarn run start
# this line only executes after yarn _exits_

그리고

nohup yarn run start &
# this line executes as soon as yarn _starts_

&명령을 백그라운드 프로세스로 실행하고 즉시 다음 줄에서 계속되는 줄 끝 부분에 유의하십시오 .


고쳐 쓰다:OP의 의견을 바탕으로 그는 docker-compose. 다음에서 컨테이너를 실행할 수 있습니다.분할 모드--detachdocker-compose up --detach이는 그들이 나갈 때까지 기다리지 않는다는 것을 의미합니다 .

이 시도:

docker-compose up --detach

도움이 되었기를 바랍니다 :)

관련 정보