次の構成を使用して、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にアプリケーションを公開すると、ランチャーが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
。以下からコンテナを実行できます。分割モード--detach
docker-compose up --detach
これは彼らが出るのを待たないことを意味します。
この試み:
docker-compose up --detach
役に立ったことを願っています:)