Skip to content

Instantly share code, notes, and snippets.

@MayMeow
Forked from htuscher/.gitlab-ci.yml
Created April 15, 2022 08:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MayMeow/59794f2a2de0912272eef4ebf59d1b0e to your computer and use it in GitHub Desktop.
Save MayMeow/59794f2a2de0912272eef4ebf59d1b0e to your computer and use it in GitHub Desktop.
Deploying with docker-compose via SSH tunnel in Gitlab CI
deploy:live:
image: 1drop/docker:git
stage: deploy
when: manual
environment:
name: production
url: https://www.somecustomer.de
before_script:
- eval $(ssh-agent -s)
- ssh-add <(echo "$SSH_PRIVATE_KEY")
- mkdir -p ~/.ssh
- echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config
- apk add socat
script:
- ./bin/deploy.sh
tags:
- vpn
only:
- master
#!/usr/bin/env bash
set -o pipefail # trace ERR through pipes
set -o errtrace # trace ERR through 'time command' and other functions
set -o nounset ## set -u : exit the script if you try to use an uninitialised variable
set -o errexit ## set -e : exit the script if any statement returns a non-true return value
REMOTE=sshuser@192.168.0.222 # todo: change here
TYPO3_IMAGE=registry.gitlab.com/vendor/customer/typo3 # todo: change here
echo " * OPENING DOCKER SOCKET TUNNEL"
socat \
"UNIX-LISTEN:/tmp/docker.sock,reuseaddr,fork" \
"EXEC:'ssh -kTax $REMOTE socat STDIO UNIX-CONNECT\:/var/run/docker.sock'" \
&
export DOCKER_HOST=unix:///tmp/docker.sock
export COMPOSE_PROJECT_NAME=some-fixed-name # todo: change here
echo " * LOGIN WITH GITLAB-CI TOKEN"
docker login -u gitlab-ci-token -p $CI_BUILD_TOKEN $CI_REGISTRY
# backup current image if already present locally
if [[ ! "$(docker images -q ${TYPO3_IMAGE} 2> /dev/null)" == "" ]]; then
echo " * BACKING UP CURRENT IMAGE VERSION"
docker tag ${TYPO3_IMAGE} typo3-backup
fi
echo " * PULLING NEW IMAGES"
docker-compose -f docker-compose.live.yml pull
echo " * UPDATING RUNNING CONTAINERS"
docker-compose -f docker-compose.live.yml up -d
echo " * CLEANING OLD IMAGES"
ssh -t ${REMOTE} "docker-clean images"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment