steps:
  test:
    image: mcr.microsoft.com/dotnet/sdk:8.0
    when:
      event:
        - push
        - commit
    commands:
      - (dotnet nuget add source $${ADAS_NUGET_URL} -n "ADAS Nugget Server") | 2>/dev/null
      - dotnet build  --configuration Debug /p:Platform=x64 /nowarn:1701,1702,IL2121,CS1591,IL2121,1701,1702,CS1587,CA1416
        "adas-core/adas-core.csproj"
      - dotnet test  --configuration Debug /p:Platform=x64 /nowarn:1701,1702,IL2121,CS1591,IL2121,1701,1702,CS1587,CA1416
        "./adas-core.Test/adas-core.Test.csproj"
    environment:
      ADAS_NUGET_URL:
        from_secret: ADAS_NUGET_URL
  upload:
    image: docker:dind
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
    when:
      event:
        - push
        - commit
      branch:
        - main
    commands:
      - docker login -u $${CI_REGISTRY_USER} -p $${CI_REGISTRY_PASSWORD} $${CI_REGISTRY}
      - docker build -t $${CI_REGISTRY}/adas/adas-core/adas-core:latest .
      - docker push $${CI_REGISTRY}/adas/adas-core/adas-core:latest
    environment:
      CI_REGISTRY_USER:
        from_secret: CI_REGISTRY_USER
      CI_REGISTRY_PASSWORD:
        from_secret: CI_REGISTRY_PASSWORD
      CI_REGISTRY:
        from_secret: CI_REGISTRY
      ADAS_HYDE_SSH_KEY:
        from_secret: ADAS_HYDE_SSH_KEY
      SSH_SERVER_HOSTKEYS:
        from_secret: SSH_SERVER_HOSTKEYS
  restart-docker-service:
    image: alpine
    when:
      branch:
        - main
    commands:
      - command -v ssh-agent >/dev/null || ( apk add --update openssh )
      - eval $(ssh-agent -s)
      - echo "$${ADAS_HYDE_SSH_KEY}" | tr -d '\r' | ssh-add -
      - mkdir -p ~/.ssh
      - chmod 700 ~/.ssh
      - echo "$${SSH_SERVER_HOSTKEYS}" >> ~/.ssh/known_hosts
      - chmod 644 ~/.ssh/known_hosts
      - ssh -o "StrictHostKeyChecking no" gitlabci@10.0.20.67 -p 22 "cd /opt/smartuci;docker-compose
        stop adas-core;docker-compose rm -f adas-core;docker-compose pull adas-core;docker-compose
        up -d adas-core" || ssh -o "StrictHostKeyChecking no" gitlabci@red1.epigram.es
        -p 1522 "cd /opt/adascore;docker-compose stop adas-core;docker-compose rm
        -f adas-core;docker-compose pull adas-core;docker-compose up -d adas-core"
    environment:
      CI_REGISTRY_USER:
        from_secret: CI_REGISTRY_USER
      CI_REGISTRY_PASSWORD:
        from_secret: CI_REGISTRY_PASSWORD
      CI_REGISTRY:
        from_secret: CI_REGISTRY
      ADAS_HYDE_SSH_KEY:
        from_secret: ADAS_HYDE_SSH_KEY
      SSH_SERVER_HOSTKEYS:
        from_secret: SSH_SERVER_HOSTKEYS
