#This program has been developed by students from the bachelor Computer Science at Utrecht University within the Software Project course.
#© Copyright Utrecht University (Department of Information and Computing Sciences)
image: golang:1.17

stages:
  - test
  - docker
  - integration
  - deploy
  

lint:
  stage: test
  script:
    - make lint

unit_tests:
  stage: test
  script:
    - make dep
    - make test

coverage:
  stage: test
  script:
    - make coverage
  after_script:
    - mkdir $CI_COMMIT_BRANCH
    - cp cover.html $CI_COMMIT_BRANCH
    - mv $CI_COMMIT_BRANCH/cover.html $CI_COMMIT_BRANCH/index.html

    - if [[ $CI_COMMIT_BRANCH = "develop" || $CI_COMMIT_BRANCH = "main" ]]; then COVERAGE_PATH=""; else COVERAGE_PATH="features"; fi

    # install openssh client and add ssh keys
    - apt-get install openssh-client curl -y >/dev/null
    - mkdir ~/.ssh/
    - eval $(ssh-agent -s)
    - echo "$SSH_PRIVATE_KEY" | tr -d '\r' > ~/.ssh/id_rsa
    - chmod 600 ~/.ssh/id_rsa
    - ssh-add ~/.ssh/id_rsa
    - echo "$SSH_KNOWN_HOSTS" >> ~/.ssh/known_hosts
    - chmod 644 ~/.ssh/known_hosts
    - ssh -fN -L 1234:science-vs260.science.uu.nl:22 sivan@up.science.uu.nl
    - scp -r -o StrictHostKeyChecking=no -P 1234 -i ~/.ssh/id_rsa $CI_COMMIT_BRANCH root@localhost:/datadisk/documentation-coverage/home/backend/$CI_PROJECT_NAME/$COVERAGE_PATH
  artifacts:
    untracked: false
    expire_in: 30 days
    paths:
      - cover.html

docker:
  image: docker:stable
  tags:
    - docker
  stage: docker
  only:
    - develop
    - main
  before_script:
    # Make sure docker uses Buildkit
    - export DOCKER_BUILDKIT=1
    # Log full docker build progress
    - export BUILDKIT_PROGRESS=plain
    # Enable experimental features (buildx)
    - export DOCKER_CLI_EXPERIMENTAL=enabled
    # Install docker buildx
    - mkdir -p $HOME/.docker/cli-plugins
    - wget -O $HOME/.docker/cli-plugins/docker-buildx https://github.com/docker/buildx/releases/download/v0.4.2/buildx-v0.4.2.linux-amd64
    - chmod a+x $HOME/.docker/cli-plugins/docker-buildx
    # Log in to our private image registry
    - docker login datastropheregistry.azurecr.io -u $REGISTRY_USERNAME -p $REGISTRY_PASSWORD
    - docker buildx install
    - docker buildx create --use
    # Install openssh and curl
    - apk add openssh-client curl
    - mkdir ~/.ssh/
    - eval $(ssh-agent -s)
    - echo "$SSH_PRIVATE_KEY" | tr -d '\r' > ~/.ssh/id_rsa
    - chmod 600 ~/.ssh/id_rsa
    # Add our private ssh key
    - ssh-add ~/.ssh/id_rsa
    # Add UU gitlab to known hosts
    - echo "$SSH_KNOWN_HOSTS" >> ~/.ssh/known_hosts
    - chmod 644 ~/.ssh/known_hosts
  script:
    # Build and push image for linux/amd64 as well as linux/arm64
    # Check if $CI_COMMIT_BRANCH is available, as it is not if this is a merge pipeline run
    - if [[ ! -z $CI_COMMIT_BRANCH+x ]]; then DOCKER_TAG=$CI_COMMIT_BRANCH; else DOCKER_TAG=$CI_MERGE_REQUEST_TARGET_BRANCH_NAME; fi
    - docker buildx build --platform linux/amd64,linux/arm64 -t datastropheregistry.azurecr.io/$CI_PROJECT_NAME:$DOCKER_TAG . --push --ssh default

docker-staging:
  image: docker:stable
  tags:
    - docker
  stage: docker
  except:
    - develop
    - main
  before_script:
    # # Make sure docker uses Buildkit
    # - export DOCKER_BUILDKIT=1
    # # Log full docker build progress
    # - export BUILDKIT_PROGRESS=plain
    # # Enable experimental features (buildx)
    # - export DOCKER_CLI_EXPERIMENTAL=enabled
    # - docker login datastropheregistry.azurecr.io -u $REGISTRY_USERNAME -p $REGISTRY_PASSWORD

      # Make sure docker uses Buildkit
    - export DOCKER_BUILDKIT=1
    # Log full docker build progress
    - export BUILDKIT_PROGRESS=plain
    # Enable experimental features (buildx)
    - export DOCKER_CLI_EXPERIMENTAL=enabled
    # Install docker buildx
    - mkdir -p $HOME/.docker/cli-plugins
    - wget -O $HOME/.docker/cli-plugins/docker-buildx https://github.com/docker/buildx/releases/download/v0.4.2/buildx-v0.4.2.linux-amd64
    - chmod a+x $HOME/.docker/cli-plugins/docker-buildx
    # Log in to our private image registry
    - docker login datastropheregistry.azurecr.io -u $REGISTRY_USERNAME -p $REGISTRY_PASSWORD
    - docker buildx install
    - docker buildx create --use
    # Install openssh and curl
    - apk add openssh-client curl
    - mkdir ~/.ssh/
    - eval $(ssh-agent -s)
    - echo "$SSH_PRIVATE_KEY" | tr -d '\r' > ~/.ssh/id_rsa
    - chmod 600 ~/.ssh/id_rsa
    # Add our private ssh key
    - ssh-add ~/.ssh/id_rsa
    # Add UU gitlab to known hosts
    - echo "$SSH_KNOWN_HOSTS" >> ~/.ssh/known_hosts
    - chmod 644 ~/.ssh/known_hosts
  script:
    # - docker build --progress plain -t $CI_PROJECT_NAME:latest .
    # - docker tag $CI_PROJECT_NAME datastropheregistry.azurecr.io/$CI_PROJECT_NAME-staging:latest
    # - docker push datastropheregistry.azurecr.io/$CI_PROJECT_NAME-staging:latest
    - docker buildx build --platform linux/amd64,linux/arm64 -t datastropheregistry.azurecr.io/$CI_PROJECT_NAME-staging:latest . --push --ssh default

  # dependencies:
  #   - build

integration-tests:
   image: golang:1.16
   stage: integration
   script:
     - apt-get install openssh-client curl grep -y >/dev/null
     - mkdir ~/.ssh/
     - eval $(ssh-agent -s)
     - echo "$SSH_PRIVATE_KEY" | tr -d '\r' > ~/.ssh/id_rsa
     - chmod 600 ~/.ssh/id_rsa
     - ssh-add ~/.ssh/id_rsa
     - echo "$SSH_KNOWN_HOSTS" >> ~/.ssh/known_hosts
     - chmod 644 ~/.ssh/known_hosts
     - ssh -fN -L 1234:science-vs260.science.uu.nl:22 sivan@up.science.uu.nl
     # Delete pod
     - ssh -p 1234 -o StrictHostKeyChecking=no -i ~/.ssh/id_rsa root@localhost "kubectl delete -f /root/kubernetes/$CI_PROJECT_NAME/integration/pod.yml --ignore-not-found=true"
     # Copy kubernetes files over
     - scp -r -o StrictHostKeyChecking=no -P 1234 -i ~/.ssh/id_rsa integration-testing/pod.yml root@localhost:/root/kubernetes/$CI_PROJECT_NAME/integration/pod.yml
     # Deploy all yml files
     - ssh -p 1234 -i ~/.ssh/id_rsa root@localhost "kubectl apply -f /root/kubernetes/$CI_PROJECT_NAME/integration/pod.yml"
     # Wait 20 seconds for the pod to start up
     - sleep 20
     # CURL the config json to the integration service and store the response in the environment variable 'output'
     - |
       output=$(curl -k --header "Content-Type: multipart/form-data" --request POST -F "configFile=@integration-testing/config.json;type=application/json" https://datastrophe.science.uu.nl/integration-test/start/)
     - echo $output
     # Count the number of occurences of the word FAIL in the output, if this is 1 then the test has failed
     - echo "$output" | grep -c "SUCCESS"
   dependencies: []

deploy:
  stage: deploy
  only:
    - develop
    - main
  script:
    - apt-get install openssh-client curl -y >/dev/null
    - mkdir ~/.ssh/
    - eval $(ssh-agent -s)
    - echo "$SSH_PRIVATE_KEY" | tr -d '\r' > ~/.ssh/id_rsa
    - chmod 600 ~/.ssh/id_rsa
    - ssh-add ~/.ssh/id_rsa
    - echo "$SSH_KNOWN_HOSTS" >> ~/.ssh/known_hosts
    - chmod 644 ~/.ssh/known_hosts
    - ssh -fN -L 1234:science-vs260.science.uu.nl:22 sivan@up.science.uu.nl
    # Copy kubernetes files over
    - scp -r -o StrictHostKeyChecking=no -P 1234 -i ~/.ssh/id_rsa deployments/* root@localhost:/root/kubernetes/$CI_PROJECT_NAME
    # Remove old deployment
    - ssh -p 1234 -i ~/.ssh/id_rsa root@localhost "kubectl delete -f kubernetes/$CI_PROJECT_NAME/"
    # Apply new deployment
    - ssh -p 1234 -i ~/.ssh/id_rsa root@localhost "kubectl apply -f kubernetes/$CI_PROJECT_NAME/"
  dependencies: []
