最近因为需要使用gitlab上的CI/CD进行自动部署,涉及到了使用docker-compose搭建的远程服务器环境,踩了很多坑,特此记录一下。
.git-ci.yml 样例
stages: # 分段
- build
- deploy
- restart
- stop
restart-server-dev-job:
stage: restart
script:
- echo "=============== 停止服务器 ==============="
- chmod a+x ./stop_server_dev.sh
- ./stop_server_dev.sh
- echo "=============== 执行结束 ==================="
- echo "=============== 启动服务器 ==============="
- chmod a+x ./deploy_dev.sh
- ./deploy_dev.sh
- echo "=============== 执行结束 ==================="
when: manual # 手动启动
only:
- dev # 使用git上哪个branch
使用sh执行命令
#!/bin/bash
ssh -tt user@server_ip<< remotessh
cd ./path_to_file/
docker-compose exec sts bash # 进入docker
python --version
python script.py
exit
remotessh
- 问题一:Pseudo-terminal will not be allocated because stdin is not a terminal
解决方法:使用 -tt 参数
ssh 参数:
- -t:强制配置 pseudo-tty
- -T:禁止分配伪终端
- 问题二:直接在yml中使用 ssh -tt 后只执行第一条命令
解决方法:使用sh文件存储所有命令及使用remotessh