spec:
  inputs:
    stage:
      default: deploy
    project:
      description: "Le nom du projet Dokku"
    when:
      default: manual
    environment:
      default: production
    url:
      default: ""
    image:
      description: "L'image Docker à déployer"
      default: ""
    remote:
      description: "L'adresse SSH du serveur Dokku"
      default: "ssh://dokku@deploy.runner.iiens.net"
---
.dokku:
  stage: $[[ inputs.stage ]]
  when: $[[ inputs.when ]]
  image: ${CI_DEPENDENCY_PROXY_DIRECT_GROUP_IMAGE_PREFIX}/dokku/ci-docker-image
  variables:
    GIT_PUSH_FLAGS: --force
    BRANCH: main
    GIT_REMOTE_URL: $[[ inputs.remote ]]/$[[ inputs.project ]]
    DEPLOY_DOCKER_IMAGE: $[[ inputs.image ]]
  script:
    - echo "Deploying $DEPLOY_DOCKER_IMAGE to $GIT_REMOTE_URL..."
    - dokku-deploy
  after_script:
    - dokku-unlock

"deploy-$[[ inputs.environment ]]":
  extends: .dokku
  environment:
    name: $[[ inputs.environment ]]
    url: $[[ inputs.url ]]
    action: start
    on_stop: stop-deploy-$[[ inputs.environment ]]

"stop-deploy-$[[ inputs.environment ]]":
  extends: .dokku
  when: manual
  needs:
    - "deploy-$[[ inputs.environment ]]"
  environment:
    name: $[[ inputs.environment ]]
    url: $[[ inputs.url ]]
    action: stop
  script:
    - setup-ssh
    - ssh "$[[ inputs.remote ]]" -- ps:stop "$(parse-app-name)"
