variables:
  PICO_DOCKER_IMAGE: registry.forge.hefr.ch/team-raclette/project-softweng/pico-sensor:latest

stages:
  - pico-sensor-docker-build
  - pico-sensor-test
  - pico-sensor-release

pico-sensor-docker-build:
  image: docker:latest
  stage: pico-sensor-docker-build
  services:
    - docker:dind
  script:
    - docker build -t $PICO_DOCKER_IMAGE -f pico-sensor/Dockerfile .
    - echo $CI_REGISTRY_PASSWORD | docker login -u $CI_REGISTRY_USER --password-stdin $CI_REGISTRY
    - docker push $PICO_DOCKER_IMAGE
  rules:
    - if: $CI_PIPELINE_SOURCE == 'merge_request_event' && $CI_COMMIT_BRANCH == 'main'
    - changes:
        - pico-sensor/Dockerfile

# Uncomment the following section if you want to run tests on the pico-sensor device.
# Note: This requires a tunnel to the device
# Currently no tunnel is set up, so this section is commented out.
# pico-sensor-test:
#   stage: pico-sensor-test
#   image: $PICO_DOCKER_IMAGE
#   variables:
#     TUNNEL_ID: "801000372" # Tunnel ID to reach the pico-sensor device
#   script:
#     - cd pico-sensor
#     - cmake --preset Test -DTUNNEL_ID=$TUNNEL_ID
#     - cmake --build --preset app-test
#     - cd build/Test
#     - ctest -T test --output-on-failure --output-junit test-results.xml$
#   artifacts:
#     paths:
#       - pico-sensor/build/Test/test-results.xml

pico-sensor-release:
  stage: pico-sensor-release
  image: $PICO_DOCKER_IMAGE
  script:
    - cd pico-sensor
    - cmake --preset Release
    - cmake --build --preset app-release
  artifacts:
    paths:
      - pico-sensor/build/Release/**/*.uf2
  rules:
    - if: '$CI_COMMIT_BRANCH == "main" && $CI_PIPELINE_SOURCE == "merge_request_event"'
      when: always

