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.sh通过 SSH 将应用程序部署到 AWS。现在的问题是,在 git 上发布应用程序会导致运行器运行 CI 任务,该任务将通过 SSH 连接到 AWS 服务器并开始执行应用程序安装(克隆、yarn 等)过程。但是我如何告诉 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。您可以在以下位置运行容器分离模式--detach这意味着不会docker-compose up --detach等待他们退出。

尝试这个:

docker-compose up --detach

希望有帮助:)

相关内容