stages:
  - publish
  - deploy

variables:
  REGISTRY: registry.sc07.dev
  IMAGE_NAME: $GITLAB_USER_LOGIN/$CI_PROJECT_NAME:edge

publish:
  image: docker
  stage: publish
  services:
    - docker:dind
  before_script:
    - echo $PAT | docker login $REGISTRY -u $GITLAB_USER_LOGIN --password-stdin
  script:
    - docker build . --tag $REGISTRY/$IMAGE_NAME
    - docker push $REGISTRY/$IMAGE_NAME

deploy:
  image: ubuntu:latest
  stage: deploy
  only:
    - main
  before_script:
    - apt-get -yq update
    - apt-get -yqq install ssh
    - install -m 600 -D /dev/null ~/.ssh/id_rsa
    - echo "$SSH_PRIVATE_KEY" | base64 -d > ~/.ssh/id_rsa
    - ssh-keyscan -H $SSH_HOST > ~/.ssh/known_hosts
  script:
    - ssh $SSH_USER@$SSH_HOST "cd $WORK_DIR && docker compose pull && docker compose up -d && exit"
  after_script:
    - rm -rf ~/.ssh
