【问题标题】:Syntax error when adding SSH key in GitLab CI在 GitLab CI 中添加 SSH 密钥时出现语法错误
【发布时间】:2020-10-21 04:13:44
【问题描述】:

参考:https://gitlab.ida.liu.se/help/ci/ssh_keys/README.md

以下脚本有问题。即使我将最后一个符号从 ` 更改为 ' ,问题是如何修复如下所示的错误

$ ssh-add

before_script:
  # Install ssh-agent if not already installed, it is required by Docker.
  # (change apt-get to yum if you use a CentOS-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
  - ssh-add <(echo "$SSH_PRIVATE_KEY")

  # For Docker builds disable host key checking. Be aware that by adding that
  # you are suspectible to man-in-the-middle attacks.
  # WARNING: Use this only with the Docker executor, if you use it with shell
  # you will overwrite your user's SSH config.
  - mkdir -p ~/.ssh
  - '[[ -f /.dockerinit ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config`

【问题讨论】:

    标签: git docker gitlab gitlab-ci build-script


    【解决方案1】:

    我试了很多次,下面的.gitlab-ci.yml应该可以正常工作了。

        image: gitlab/dind:latest
    
    variables:
      COMPOSE: docker-compose
    
    before_script:
      # Install ssh-agent if not already installed, it is required by Docker.
      # (change apt-get to yum if you use a CentOS-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
      - ssh-add <(echo "$SSH_PRIVATE_KEY")
    
      # For Docker builds disable host key checking. Be aware that by adding that
      # you are suspectible to man-in-the-middle attacks.
      # WARNING: Use this only with the Docker executor, if you use it with shell
      # you will overwrite your user's SSH config.
      - mkdir -p ~/.ssh
    
      - ssh-keyscan -H 'gitlab.com' >> ~/.ssh/known_hosts
      - ssh-keyscan gitlab.com | sort -u - ~/.ssh/known_hosts -o ~/.ssh/known_hosts
    
      - '[[ -f /.dockerinit ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config'
    
    stages:
      - build
      - test
      - deploy
    
    # Add a job called 'build' -> to run your builds
    # TODO: Build the image in remote docker registry 
    build-dev:
      stage: build 
      script:
        - sh scripts/install-dev.sh
        - $COMPOSE build
      only:
        - dev 
    
    build-master:
      stage: build 
      script:
        - sh scripts/install.sh
        - $COMPOSE build
      only:
        - master 
    

    我还打开了一个问题here

    【讨论】:

      【解决方案2】:

      为了让它工作,我不得不更换:

      ssh-add <(echo "$SSH_PRIVATE_KEY")
      

      与:

      printenv SSH_PRIVATE_KEY | ssh-add -
      

      【讨论】:

        猜你喜欢
        • 2015-08-22
        • 1970-01-01
        • 1970-01-01
        • 2015-01-11
        • 1970-01-01
        • 2020-01-28
        • 2020-10-25
        • 1970-01-01
        • 2019-05-05
        相关资源
        最近更新 更多