gitlab-ci.yml 可以将其变量共享到 ssh 吗?

发布于 2025-01-18 01:11:48 字数 1024 浏览 3 评论 0原文

我正在尝试通过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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

总以为 2025-01-25 01:11:48

开箱即用的 Docker 可以使用 SSH 连接到远程 Docker 守护进程。因此无需复制 docker-compose.yaml 或转发环境变量。

您所需要做的就是设置 DOCKER_HOST 变量以指向远程 docker 守护程序的 SSH uri。

deploy:
  image: alpine
  variables:
    #this may be overridden by parent pipeline, or it will be latest
    my_nginx: registry.gitlab.com/myprofile/nginx:latest 
    DOCKER_HOST: ssh://$HOST # format is ssh://user@host
  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:
    - echo "$CI_JOB_TOKEN"  | docker login -u "$CI_REGISTRY_USER" $CI_REGISTRY --password-stdin;
      docker-compose pull; 
      docker-compose up -d --remove-orphans; 
      docker image prune;"

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.

deploy:
  image: alpine
  variables:
    #this may be overridden by parent pipeline, or it will be latest
    my_nginx: registry.gitlab.com/myprofile/nginx:latest 
    DOCKER_HOST: ssh://$HOST # format is ssh://user@host
  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:
    - echo "$CI_JOB_TOKEN"  | docker login -u "$CI_REGISTRY_USER" $CI_REGISTRY --password-stdin;
      docker-compose pull; 
      docker-compose up -d --remove-orphans; 
      docker image prune;"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文