Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Syntax error when adding SSH key in GitLab CI

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

The following script has problems. Even though I changed the last symbol from ` to ' , the question is how to fix the error as shown below:

$ ssh-add <(echo "$SSH_PRIVATE_KEY") /bin/sh: eval: line 24: syntax error: unexpected "("

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`
like image 942
tony.0919 Avatar asked Jul 15 '16 07:07

tony.0919


People also ask

How create SSH key and add to GitLab?

Log into GitLab and click on your account preferences. Click the SSH Keys link and paste the copied value into the text field. Set an expiration date, and then click the blue button to persistently add the GitLab SSH key. Configure GitLab SSH keys under your account preferences.

Can I use same SSH key for GitLab and GitHub?

Of course you can, the services don't cross-check against each other to tell if a key is in use elsewhere, but as VonC points out, you probably shouldn't.

What is SSH key in GitLab?

The SSH stands for Secure Shell or Secure Socket Shell used for managing the networks, operating systems and configurations and also authenticates to the GitLab server without using username and password each time. You can set the SSH keys to provide a reliable connection between the computer and GitLab.


2 Answers

I have tried many times, the following .gitlab-ci.yml should work properly.

    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 

I have also opened an issue here.

like image 50
tony.0919 Avatar answered Oct 25 '22 00:10

tony.0919


To make it work I had to replace:

ssh-add <(echo "$SSH_PRIVATE_KEY")

with:

printenv SSH_PRIVATE_KEY | ssh-add -
like image 26
Aleksei Mialkin Avatar answered Oct 25 '22 00:10

Aleksei Mialkin