简体   繁体   中英

Gitlab CI: Docker connect to remote MySQL via SSH Portforwarding before running tests

I am trying to integrate my unit tests into Gitlab CI, which is mostly working.

The NodeJS application uses MySQL databases hosted on a different server (using: ssh -L 3306:127.0.0.1:3306 username@remoteserver ) which we locally port forward to, and as such, all the tests pass locally as we are connected to it.

The CI script (included below) seems to work and the tests pass on any function that doesn't require the mysql connection. I need my CI runner to SSH into the remote server and let those remaining functions be tested.

However, I am struggling to find a way to have my gitlab-ci.yml script execute the SSH (using a public key) into this remote server and locally port forward it to 127.0.0.1, before the tests are run.

I also am unsure as to whether the public/private key pair is to be generated inside Docker, or generally on the machine that the Runner is set up on.

Can anyone point me in the right direction?

image: node:7.4

before_script:
  - apt-get update -qy
  - npm install -g mocha chai assert mysql require moment
stages:
  - test

test_job:
  stage: test
  tags: ["mySpecificRunner"]
  script:
    - npm run test

  environment:

  only:
  - development

It is not straight forward, but there is a way. GitLab provides documentation and even an example .

What you want to do is:

  1. Generate a public/private key pair
  2. On the server you want to connect to, add the public part of the key to the file listing the authorized ones (usually ~/.ssh/authorized_keys )
  3. In Gitlab, create a new variable called SSH_PRIVATE_KEY with the private part of the key as value
  4. In your project, modify the file .gitlab-ci.yml so that the Docker container uses the private part of the key:

     image: debian:latest before_script: # install & run ssh-agent - apt-get -qq update -y - apt-get -qq install openssh-client -y # setup the private key - eval $(ssh-agent -s) - ssh-add <(echo "$SSH_PRIVATE_KEY") - mkdir -p ~/.ssh - echo -e "Host *\\n\\tStrictHostKeyChecking no\\n\\n" > ~/.ssh/config
  5. You script should then be able to connect seamlessly to the server and run commands or scripts there, eg( $HOST and $USER are also secret variables):

     deploy-dev: stage: deploy script: - | ssh -t $USER@$HOST << EOF git fetch --all -v git checkout -f dev git reset --hard origin/dev EOF

Note that at the time of writing this answer I have been unable to keep the SSH connection active and run commands one by one there. That is the reason behind the use << EOF .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM