Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deploy docker container to digital ocean droplet from gitlab-ci

So here is what I want to do.

  1. Push to master in git
  2. Have gitlab-ci hear that push an start a pipeline
  3. The pipeline builds code and pushes a docker container to the gitlab registry
  4. The pipeline logs into a digital ocean droplet via ssh
  5. The pipeline pulls the docker container from the gitlab registry
  6. The pipeline starts the container

I can get up to step 4 no problem. But step 4 just fails every which way. I've tried the ssh key approach:

  • https://gitlab.com/gitlab-examples/ssh-private-key/blob/master/.gitlab-ci.yml

But that did not work.

So I tried a plain text password approach like this:

image: gitlab/dind:latest

before_script:
 - apt-get update -y && apt-get install sshpass

stages:
 - deploy

deploy:
  stage: deploy
  script:
    - sshpass -p "mypassword" ssh [email protected] 'echo $HOME'

this version just exits with code 1 like so

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

ln: failed to create symbolic link '/sys/fs/cgroup/systemd/name=systemd': Operation not permitted
/usr/local/bin/wrapdocker: line 113:    54 Killed                  docker daemon $DOCKER_DAEMON_ARGS &> /var/log/docker.log
Timed out trying to connect to internal docker host. 

Is there a better way to do this? How can I at the very least access my droplet from inside the gitlab-ci build environment?

like image 433
Fresheyeball Avatar asked Sep 20 '16 05:09

Fresheyeball


1 Answers

I just answered this related question: Create react app + Gitlab CI + Digital Ocean droplet - Pipeline succeeds but Docker container is deleted right after

Heres the solution he is using to get ssh creds set:

before_script:
  ## Install ssh agent (so we can access the Digital Ocean Droplet) and run it.
  - apk update && apk add openssh-client
  - eval $(ssh-agent -s)

  ## Write the environment variable value to the agent store, create the ssh directory and give the right permissions to it.
  - echo "$SECRETS_DIGITAL_OCEAN_DROPLET_SSH_KEY" | ssh-add -
  - mkdir -p ~/.ssh
  - chmod 700 ~/.ssh

  ## Make sure that ssh will trust the new host, instead of asking
  - echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config

  ## Test it!
  - ssh -t ${SECRETS_DIGITAL_OCEAN_DROPLET_USER}@${SECRETS_DIGITAL_OCEAN_DROPLET_IP} 'echo $HOME'

Code credit goes to https://stackoverflow.com/users/6655011/leonardo-sarmento-de-castro

like image 157
d g Avatar answered Oct 19 '22 22:10

d g