概念梳理
配置文件
以下是前端工程部署的.gitlab-ci.yml文件和Dockerfile文件示例。
.gitlab-ci.yml
stages: # 分段
- build
- deploy
cache: # 缓存
paths:
- node_modules
- build
# ----------------------------------------------【测试环境】
npm:
tags:
- guarantee-test
only:
- test
stage: build
image: node:14.15.4
script:
- npm i --save --registry=https://registry.npm.taobao.org
- npm run build
docker:
tags:
- guarantee-test
only:
- test
stage: build
script:
- docker build -t guarantee-pc:latest .
- docker tag guarantee-pc:latest docker-registry.xjchenhao.com/xxx/guarantee-pc:$CI_COMMIT_REF_NAME
- docker login --username=$harborUserName --password=$harborPassword http://docker-registry.rd.com
- docker push docker-registry.xjchenhao.com/xxx/guarantee-pc:$CI_COMMIT_REF_NAME
deploy:
tags:
- guarantee-test
only:
- test
stage: deploy
before_script:
- 'command -v ssh-agent >/dev/null || ( apt-get update -y && apt-get install openssh-client -y )'
- eval $(ssh-agent -s)
- echo "$TEST_PRIVATE_KEY" | tr -d '\r' | ssh-add -
- mkdir -p ~/.ssh
- chmod 700 ~/.ssh
script:
- ssh -o StrictHostKeyChecking=no root@"$TEST_SERVER_IP" ./docker-deploy-web.sh guarantee-pc "$CI_COMMIT_REF_NAME" 8001
Dockerfile
FROM nginx
COPY build /usr/share/nginx/html
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo "Asia/Shanghai" > /etc/timezone
EXPOSE 80
Shell
#/bin/bash
set -e;
service_name="$1"
tag="$2"
port="$3"
docker pull docker-registry.xjchenhao.com/xxx/$service_name:$tag;
docker run --rm curlimages/curl:latest 'https://oapi.dingtalk.com/robot/send?access_token=123456789' \
-H 'Content-Type: application/json' \
-d '{"msgtype": "text","text": {"content": "【xxx测试环境服务器】\n⚠️ '$service_name:$tag'开始部署"}}'
docker stop $service_name||true;
docker rm $service_name||true;
docker run -d -p $port:80 --name $service_name docker-registry.rd.com/ztb/$service_name:$tag;
docker run --rm curlimages/curl:latest 'https://oapi.dingtalk.com/robot/send?access_token=8b4fdd45b8e851dcb1191ab081f8eb61f664dbbbbefc78fb17112d0d0d731556' \
-H 'Content-Type: application/json' \
-d '{"msgtype": "text","text": {"content": "【xxx测试环境服务器】\n✅ '$service_name:$tag'部署成功"}}'
docker image prune -a -f;