# GitLab CI Snippets

Some .gitlab-ci.yml examples from my projects.

## Deploy via SSH

This example will log in to $VPS_ADDRESS as $VPS_USERNAME via $SSH_PRIVATE_KEY and execute simple update command in $WORKING_DIR.

image: centos:7

stages:
    - deploy

before_script:
    - yum install which -y
    - 'which ssh-agent || ( yum install openssh-clients -y )'
    - eval $(ssh-agent -s)
    - ssh-add <(echo "$SSH_PRIVATE_KEY")
    - mkdir -p ~/.ssh
    - '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config'

production:
    stage: deploy
    script:
        - ssh -t $VPS_USERNAME@$VPS_ADDRESS "cd $WORKING_DIR && git pull && make composer-install && make migration"
    only:
        - main

## Check, Then Deploy

This script will run flake8 check on latest Python image and deploy only if check step is succeed.

image: python:latest

variables:
  PIP_CACHE_DIR: "$CI_PROJECT_DIR/.cache/pip"

stages:
    - check
    - deploy

check:
    stage: check
    cache:
      paths:
        - .cache/pip
        - venv/
    script:
        - python -m pip install --upgrade pip
        - pip install flake8
        - flake8 . --count --max-line-length=127 --show-source --statistics --exclude .git,__pycache__,venv

deploy:
    stage: deploy
    script:
        - 'which ssh-agent || ( apt-get install openssh-client -y )'
        - eval $(ssh-agent -s)
        - ssh-add <(echo "$SSH_PRIVATE_KEY")
        - mkdir -p ~/.ssh
        - '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config'
        - ssh -t $VPS_USERNAME@$VPS_ADDRESS "cd $WORKING_DIR && git checkout main && git pull && source venv/bin/activate && pip install -r requirements.txt && sudo systemctl restart $SERVICE_NAME"
    only:
        - main