Skip to content

Instantly share code, notes, and snippets.

@antoninbouchal
Last active November 6, 2024 09:41
Show Gist options
  • Save antoninbouchal/8bdc29ed995252c001070c3f0c7c767e to your computer and use it in GitHub Desktop.
Save antoninbouchal/8bdc29ed995252c001070c3f0c7c767e to your computer and use it in GitHub Desktop.
Deploy APP through SSH to VPS with Gitlab CI
stages:
- build
- deploy
before_script:
- |
# docker variables for name and tag of new image
export DOCKER_TAG="${CI_COMMIT_SHA:0:8}"
export DOCKER_REPO="$CI_REGISTRY_IMAGE"
export DOCKER_IMAGE="${DOCKER_REPO}:${DOCKER_TAG}"
export DOCKER_CACHE_IMAGE="${DOCKER_REPO}:${CI_COMMIT_REF_NAME}"
# Build of docker image and push it to project Container registry
build:
# Lightweight image with support of running docker in docker
image: docker:stable
services:
- docker:dind
stage: build
when: manual
script:
# Login to Container registry
- docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" "$CI_REGISTRY"
# Load existing images for build optimisation
- docker pull "$DOCKER_CACHE_IMAGE" || docker pull "${DOCKER_REPO}:master" || true # allow failure
- docker pull "$DOCKER_IMAGE" || true # allow failure
# Build our docker image
- docker build --pull --cache-from "$DOCKER_IMAGE" --cache-from "$DOCKER_CACHE_IMAGE" --cache-from "${DOCKER_REPO}:master" -t "$DOCKER_IMAGE" .
# Save built image to Container registry under branch name tag and commit hash tag
- docker push "$DOCKER_IMAGE"
- docker tag "$DOCKER_IMAGE" "$DOCKER_CACHE_IMAGE"
- docker push "$DOCKER_CACHE_IMAGE"
only:
- master
deploy:
# Lightweight image with ssh
image: kroniak/ssh-client
stage: deploy
when: manual
script:
# Set right chmod on SSH key file
- chmod 400 $MASTER_SSH_KEY
# Login to Gitlab Container registry
- ssh -o StrictHostKeyChecking=no -i $MASTER_SSH_KEY "${MASTER_SSH_USER}@${MASTER_HOST}" "docker login -u ${CI_DEPLOY_USER} -p ${CI_DEPLOY_PASSWORD} ${CI_REGISTRY}"
# Remove old containers and images if exists
- ssh -o StrictHostKeyChecking=no -i $MASTER_SSH_KEY "${MASTER_SSH_USER}@${MASTER_HOST}" "docker rm -f ${CI_PROJECT_NAME} || true"
- ssh -o StrictHostKeyChecking=no -i $MASTER_SSH_KEY "${MASTER_SSH_USER}@${MASTER_HOST}" "docker rmi \$(docker images -q ${DOCKER_REPO}) || true"
# Download and run new image
- ssh -o StrictHostKeyChecking=no -i $MASTER_SSH_KEY "${MASTER_SSH_USER}@${MASTER_HOST}"
docker run
--name=$CI_PROJECT_NAME
--restart=always
--network="host"
-v "/app/storage:/src/storage"
-d $DOCKER_IMAGE
only:
- master

Deploy APP through SSH to VPS with Gitlab CI

Video tutorial: https://youtu.be/pChVWAVY7RU

  • Set-up VPS
    • Install Docker (link)
    • Create SSH key and put it to authorized_keys for login (link)
  • Containerize application
  • Set-up Gitlab repository
    • Create Gitlab deploy token (link)
    • Set CI variables
  • Create Gitlab CI file (template)
  • Profit!
@Jumbo810
Copy link

Gitlab

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment