- 从开发到上线流程:
- 部署到Prod
variables:
user: eric
pwd: Admin@1234
harbor: http://172.16.128.215:8080
image_hellocat: 172.16.128.215:8080/hive/hellocat
stages:
- testing
- build
- deploy_qa
- deploy_to_staging
- test
- deploy_to_prod
deploy_to_qa:
stage: deploy_qa
tags:
- shell
before_script:
- eval $(ssh-agent -s) #执行ssh
- ssh-add <(echo "$SERVER_PRIVATE_KEY") #读取密钥并加入列表
script:
- echo "start deploying.."
- scp -o StrictHostKeyChecking=no ./docker-compose.yml root@172.16.128.153:/root #拷贝docker-compose.yml到目标机器
# 启动docker服务到目标机器,配置APP_PORT 和 COMPOSE_PROJECT_NAME
- ssh -o StrictHostKeyChecking=no root@172.16.128.153 "
export APP_PORT=3000 && export COMPOSE_PROJECT_NAME=qa &&
docker-compose down && docker-compose up -d
"
# 在Gitlab中->Operations -> environment便于管理
environment:
name: qa
url: 172.16.128.153:3000
deploy_to_staging:
stage: deploy_to_staging
tags:
- shell
before_script:
- eval $(ssh-agent -s) #执行ssh
- ssh-add <(echo "$SERVER_PRIVATE_KEY") #读取密钥并加入列表
script:
- echo "start deploying"
- scp -o StrictHostKeyChecking=no ./docker-compose.yml root@172.16.128.153:/root #拷贝docker-compose.yml到目标机器
# 启动docker服务到目标机器,配置APP_PORT 和 COMPOSE_PROJECT_NAME
- ssh -o StrictHostKeyChecking=no root@172.16.128.153 "
export APP_PORT=8000 && export COMPOSE_PROJECT_NAME=stagging &&
docker-compose down && docker-compose up -d
"
environment:
name: staging
url: 172.16.128.153:8000
run_test_prod:
stage: test
tags:
- shell
script:
- echo "running test"
deploy_to_prod: #部署到prod
stage: deploy_to_prod
needs: # run_test_prod完成后,才进行此job运行
- run_test_prod
tags:
- shell
before_script:
- eval $(ssh-agent -s) #执行ssh
- ssh-add <(echo "$SERVER_PRIVATE_KEY") #读取密钥并加入列表
script:
- echo "start deploying"
- scp -o StrictHostKeyChecking=no ./docker-compose.yml root@172.16.128.153:/root #拷贝docker-compose.yml到目标机器
# 启动docker服务到目标机器,配置APP_PORT 和 COMPOSE_PROJECT_NAME
- ssh -o StrictHostKeyChecking=no root@172.16.128.153 "
export APP_PORT=9000 && export COMPOSE_PROJECT_NAME=stagging &&
docker-compose down && docker-compose up -d
"
environment:
name: prod
url: 172.16.128.153:9000
- 部署暂停,等待领导审批后,再部署到Prod生产 (when: manual)
variables:
user: eric
pwd: Admin@1234
harbor: http://172.16.128.215:8080
image_hellocat: 172.16.128.215:8080/hive/hellocat
stages:
- testing
- build
- deploy_qa
- deploy_to_staging
- test
- deploy_to_prod
deploy_to_qa:
stage: deploy_qa
tags:
- shell
before_script:
- eval $(ssh-agent -s) #执行ssh
- ssh-add <(echo "$SERVER_PRIVATE_KEY") #读取密钥并加入列表
script:
- echo "start deploying.."
- scp -o StrictHostKeyChecking=no ./docker-compose.yml root@172.16.128.153:/root #拷贝docker-compose.yml到目标机器
# 启动docker服务到目标机器,配置APP_PORT 和 COMPOSE_PROJECT_NAME
- ssh -o StrictHostKeyChecking=no root@172.16.128.153 "
export APP_PORT=3000 && export COMPOSE_PROJECT_NAME=qa &&
docker-compose down && docker-compose up -d
"
# 在Gitlab中->Operations -> environment便于管理
environment:
name: qa
url: 172.16.128.153:3000
deploy_to_staging:
stage: deploy_to_staging
tags:
- shell
before_script:
- eval $(ssh-agent -s) #执行ssh
- ssh-add <(echo "$SERVER_PRIVATE_KEY") #读取密钥并加入列表
script:
- echo "start deploying"
- scp -o StrictHostKeyChecking=no ./docker-compose.yml root@172.16.128.153:/root #拷贝docker-compose.yml到目标机器
# 启动docker服务到目标机器,配置APP_PORT 和 COMPOSE_PROJECT_NAME
- ssh -o StrictHostKeyChecking=no root@172.16.128.153 "
export APP_PORT=8000 && export COMPOSE_PROJECT_NAME=stagging &&
docker-compose down && docker-compose up -d
"
environment:
name: staging
url: 172.16.128.153:8000
run_test_prod:
stage: test
tags:
- shell
script:
- echo "running test"
deploy_to_prod: #部署到prod
stage: deploy_to_prod
when: manual #等到领导审批后,再部署到生产
needs: # run_test_prod完成后,才进行此job运行
- run_test_prod
tags:
- shell
before_script:
- eval $(ssh-agent -s) #执行ssh
- ssh-add <(echo "$SERVER_PRIVATE_KEY") #读取密钥并加入列表
script:
- echo "start deploying"
- scp -o StrictHostKeyChecking=no ./docker-compose.yml root@172.16.128.153:/root #拷贝docker-compose.yml到目标机器
# 启动docker服务到目标机器,配置APP_PORT 和 COMPOSE_PROJECT_NAME
- ssh -o StrictHostKeyChecking=no root@172.16.128.153 "
export APP_PORT=9000 && export COMPOSE_PROJECT_NAME=stagging &&
docker-compose down && docker-compose up -d
"
environment:
name: prod
url: 172.16.128.153:9000
效果: