gitlab-ci.yml 可以将其变量共享到 ssh 吗?
我正在尝试通过Gitlab CI将Docker Image部署到服务器。我已经在YML文件中设置了变量,并且通过SSH连接到服务器。是否可以自动使用YML文件中的变量,或者我必须一个一个将它们传递给SSH?
另外,我想知道除了我的方式以外,还有更好的部署方法。
deploy:
image: alpine
variables:
#this may be overridden by parent pipeline, or it will be latest
my_nginx: registry.gitlab.com/myprofile/nginx:latest
before_script:
- apk add openssh-client
- eval $(ssh-agent -s)
- echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add -
- mkdir -p ~/.ssh
- chmod 700 ~/.ssh
script:
- scp -o StrictHostKeyChecking=no docker-compose.yml $HOST:~
- >
ssh -o StrictHostKeyChecking=no $HOST "
echo "$CI_JOB_TOKEN" | docker login -u "$CI_REGISTRY_USER" $CI_REGISTRY --password-stdin;
cd ~;
my_nginx=$my_nginx
docker-compose pull;
docker-compose up -d --remove-orphans;
docker image prune;"
services:
nginx:
container_name: my-nginx
image: $my_nginx
restart: unless-stopped
ports:
- 80:80
I'm trying to deploy my Docker Image to the server via GitLab CI. I have set variables inside yml file and I am connecting to my server via SSH. Is it possible to automatically use variables inside the yml file or do I have to pass them one by one to SSH?
also, I would like to know if there is a better way to deploy this other than my way.
deploy:
image: alpine
variables:
#this may be overridden by parent pipeline, or it will be latest
my_nginx: registry.gitlab.com/myprofile/nginx:latest
before_script:
- apk add openssh-client
- eval $(ssh-agent -s)
- echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add -
- mkdir -p ~/.ssh
- chmod 700 ~/.ssh
script:
- scp -o StrictHostKeyChecking=no docker-compose.yml $HOST:~
- >
ssh -o StrictHostKeyChecking=no $HOST "
echo "$CI_JOB_TOKEN" | docker login -u "$CI_REGISTRY_USER" $CI_REGISTRY --password-stdin;
cd ~;
my_nginx=$my_nginx
docker-compose pull;
docker-compose up -d --remove-orphans;
docker image prune;"
services:
nginx:
container_name: my-nginx
image: $my_nginx
restart: unless-stopped
ports:
- 80:80
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
开箱即用的 Docker 可以使用 SSH 连接到远程 Docker 守护进程。因此无需复制 docker-compose.yaml 或转发环境变量。
您所需要做的就是设置 DOCKER_HOST 变量以指向远程 docker 守护程序的 SSH uri。
Docker out of the box can connect to a remote Docker Daemon using SSH. So no need to copy
docker-compose.yaml
or forward environment variables.All you need is to set the
DOCKER_HOST
variable to point to the SSH uri of your remote docker daemon.