首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >与Gitlab CI一起构建和部署?

与Gitlab CI一起构建和部署?
EN

Stack Overflow用户
提问于 2020-04-17 21:11:16
回答 2查看 398关注 0票数 0

我有一个DockerizedRange/Node.js应用程序,我正试图通过GitLab CI来部署它。

使用GitLab CI,我使用带有Runner的专用构建VM/服务器构建映像并将其推送到GitLab容器注册中心,然后将映像作为容器在另一个服务器(即生产服务器)中被提取和启动。

这就是我的gitlab-ci.yml文件现在看起来的样子:

代码语言:javascript
复制
image: docker:latest

#services:
#    - docker:dind

stages:
    - build
    - deploy

build-1:
    stage: build
    only:
        - deploy
    script:
        - docker login -u $GITLAB_USERNAME -p $CI_ACCESS_TOKEN $CI_REGISTRY
        - docker build -t $FRONTEND_IMG .
        - echo Pushing Docker image to GitLab
        - docker push $FRONTEND_IMG
    when: manual
    tags:
        - my-runner

build-2:
  stage: build
  only:
    - deploy
  script:
    - docker login -u $GITLAB_USERNAME -p $CI_ACCESS_TOKEN $CI_REGISTRY
    - docker build -t $BACKEND_IMG .
    - docker push $BACKEND_IMG
  when: manual
  tags:
    - my-runner

deploy-live:
    stage: deploy
    only:
        - deploy
    before_script:
        ## Install ssh-agent if not already installed, it is required by Docker.
        ## (change apt-get to yum if you use an RPM-based image)
        ##
        - 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )'

        ##
        ## Run ssh-agent (inside the build environment)
        ##
        - eval $(ssh-agent -s)

        ##
        ## Add the SSH key stored in SSH_PRIVATE_KEY variable to the agent store
        ## We're using tr to fix line endings which makes ed25519 keys work
        ## without extra base64 encoding.
        ## https://gitlab.com/gitlab-examples/ssh-private-key/issues/1#note_48526556
        ##
        - echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add -

        ##
        ## Create the SSH directory and give it the right permissions
        ##
        - mkdir -p ~/.ssh
        - chmod 700 ~/.ssh

        # - mkdir -p ~/.ssh && touch ~/.ssh/known_hosts
        # - echo "$SSH_KNOWN_HOSTS" >> ~/.ssh/known_hosts
        ##
        ## Use ssh-keyscan to scan the keys of your private server. Replace gitlab.com
        ## with your own domain name. You can copy and repeat that command if you have
        ## more than one server to connect to.
        ##
        - ssh-keyscan $SERVER_IP_ADDRESS >> ~/.ssh/known_hosts
        - chmod 644 ~/.ssh/known_hosts
    script:
        - echo SSH to prod server
        - ssh $SERVER_USERNAME@$SERVER_IP_ADDRESS && ip addr show && docker login -u $GITLAB_USERNAME -p $CI_ACCESS_TOKEN $CI_REGISTRY && docker pull $FRONTEND_IMG && docker pull $BACKEND_IMG && docker-compose -f docker-compose.yml up -d
    when: manual
    tags:
        - my-runner

  • 问题是,对接命令似乎是在构建服务器上执行的(而不是我们ssh的生产服务器),应用程序可以从那里访问,但不能从生产服务器访问。
  • 在部署后在生产服务器上运行docker images时,列表将为空。但是,当我在构建服务器中这样做时,已经生成的映像就在那里了。
  • 该作业似乎在没有错误消息的情况下成功完成,但我确实得到了以下消息:

Pseudo-terminal will not be allocated because stdin is not a terminal.

代码语言:javascript
复制
Welcome to Ubuntu 18.04.3 LTS (GNU/Linux 4.15.0-66-generic x86_64)
  * Documentation:  https://help.ubuntu.com
  * Management:     https://landscape.canonical.com
  * Support:        https://ubuntu.com/advantage
   System information as of Wed Apr 15 00:58:45 UTC 2020
   System load:  0.0               Processes:              110
   Usage of /:   6.0% of 24.06GB   Users logged in:        2
   Memory usage: 26%               IP address for eth0:    x.x.x.x
   Swap usage:   0%                IP address for docker0: x.x.x.x
 121 packages can be updated.
 73 updates are security updates.
 mesg: ttyname failed: Inappropriate ioctl for device

我错过了什么或者做错了什么?

EN

回答 2

Stack Overflow用户

发布于 2021-06-17 00:19:03

在查看了ci代码之后,当您想在生产服务器上运行容器时,应该使用ansible

Ansible比

代码语言:javascript
复制
ssh myserver "command1 && command2 &&....."
票数 2
EN

Stack Overflow用户

发布于 2021-06-17 07:59:49

我可以和你分享我的deploy.yml文件

代码语言:javascript
复制
# https://stackoverflow.com/questions/59384708/ansible-returns-with-failed-to-import-the-required-python-library-docker-sdk-f/65495769#65495769
---
- name: Build
  hosts: local
  connection: local
  tags:
    - build
  tasks:
    - name: Build Image
      community.general.docker_image:
        build:
          path: .
          pull: no
        name: registry.digitalocean.com/main/example-com
        push: true
        source: build
        force_source: yes
      environment:
        DOCKER_BUILDKIT: 1
- name: Deploy
  hosts: remote
  tags:
    - deploy
  vars:
    path: /root/example.com
  tasks:
    - name: Creates directory
      file:
        path: "{{ path }}"
        state: directory
    - name: Copy Docker Compose
      copy:
        src: docker-compose.yml
        dest: "{{ path }}/docker-compose.yml"
    - name: Reload Compose
      community.general.docker_compose:
        pull: yes
        project_src: "{{ path }}"

和gitlab文件.gitlab-ci.yml

代码语言:javascript
复制
variables:
  DOCKER_REGISTRY_DOMAIN: "registry.digitalocean.com"
  DOCKER_HOST: tcp://docker:2375
  DOCKER_TLS_CERTDIR: ""
  DOCKER_DRIVER: overlay2

image: docker:latest

services:
  - docker:dind

.deploy:
  image: archlinux:latest
  stage: deploy
  before_script:
    - pacman -Sy make ansible python python-pip openssh docker --noconfirm
    - docker login -u ${DOCKER_TOKEN} -p ${DOCKER_TOKEN} ${DOCKER_REGISTRY_DOMAIN}
    - pip3 install docker docker-compose
    - eval $(ssh-agent -s)
    - ssh-add <(echo $SSH_PRIVATE_KEY_BASE64 | base64 -d)
    - mkdir -p ~/.ssh
    - '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config'
  script:
    - ansible-playbook -i hosts deploy.yml

deploy:
  extends: .deploy
  environment:
    name: production
    url: https://example.com
  only:
    refs:
      - prod

最后hosts

代码语言:javascript
复制
[local]
127.0.0.1 env=prod

[remote]
xxx.xxx.xxx ansible_user=root env=prod

fei yang一样,我也建议用ansible替换ssh命令。

我认为这个问题是存在的:

代码语言:javascript
复制
ssh $SERVER_USERNAME@$SERVER_IP_ADDRESS && ip addr show

在我的电脑里我可以运行:

代码语言:javascript
复制
curl ipinfo.io

并得到:

代码语言:javascript
复制
{
  "ip": "193.118.225.242"
...

然后我打字:

代码语言:javascript
复制
ssh root@104.248.40.145 && curl ipinfo.io

我看到:

代码语言:javascript
复制
Last login: Thu Jun 17 07:53:38 2021 from 193.118.225.242

我登录到服务器,看不到ipinfo的结果

当我打字时

代码语言:javascript
复制
exit

要从远程服务器注销,我可以看到:

代码语言:javascript
复制
logout
Connection to 104.248.40.145 closed.
{
  "ip": "193.118.225.242",

要通过ssh远程执行命令,不应该使用&&,而应该使用""

代码语言:javascript
复制
ssh root@104.248.40.145 "curl ipinfo.io"
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61280754

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档