workflow:
  rules:
    # NOTE: don't run pipeline if commit is a Merge/Pull-Request
    - if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
      when: 'never'
    # Otherwise:
    - when: 'always'

variables:
  version: '0.0.$CI_PIPELINE_IID'
  k8sVersion: '1.20.5'
  MONGOIMAGE: 'mongo:4.2.3-bionic'

stages:
  - build
  - test
  - deploy
  - release

job_build-image:
  
  stage: build
  tags:
    - docker-privileged
  image: docker:20.10.12
  services:
    - name: docker:20.10.12-dind
  before_script:
    # - apk add --no-cache docker-compose
    - docker network create -d bridge my-build-network
    - docker pull mongo
    - docker run --name mongo --network my-build-network -d mongo
    # - docker run --network my-build-network --rm mongo mongo #--host some-mongo test
  script: 
    # - docker-compose up -f docker-compose.test.yml -d
    - docker build --file "./app/server/Dockerfile" --tag ${CI_REGISTRY_IMAGE}:${CI_PIPELINE_IID}-${CI_COMMIT_SHORT_SHA} .
      # NOTE: push image to local registry so that it can be accessed in subsequent jobs
    - docker login -u ${CI_REGISTRY_USER} -p ${CI_REGISTRY_PASSWORD} ${CI_REGISTRY}
    - docker push "${CI_REGISTRY_IMAGE}:${CI_PIPELINE_IID}-${CI_COMMIT_SHORT_SHA}"
  after_script:
    # - docker-compose down
    - docker image rm --force "${CI_REGISTRY_IMAGE}:${CI_PIPELINE_IID}-${CI_COMMIT_SHORT_SHA}"
    - docker image rm --force mongo
    - docker network rm my-build-network
    - docker network ls
    - docker network prune
    - echo y
    - docker network ls


job_test-image:
  stage: test
  image: docker:20.10.12
  tags:
    - docker-privileged
  services:
    - name: docker:20.10.12-dind
  variables:
    # NOTE: some random port that hopefully is free on the runner
    outerPort: '3002'
  before_script:
    - docker network create -d bridge test-img-network
    # - apk add --no-cache docker-compose
    # - docker-compose -f docker-compose.mongo.yml up 
    - docker login -u ${CI_REGISTRY_USER} -p ${CI_REGISTRY_PASSWORD} ${CI_REGISTRY}
    - docker pull mongo
    - docker volume create mongodb
    - docker run -p 27017:27017 -v mongodb:/.local/db/data/db --name mongo --network test-img-network -d mongo:4.4.13-rc0
    # - docker pull "${CI_REGISTRY_IMAGE}:${CI_PIPELINE_IID}-${CI_COMMIT_SHORT_SHA}"

  script:
    - docker build --file "./app/server/Dockerfile.test" --tag testimg:latest .
    - docker run
      --network=test-img-network
      --env MONGODB_URL=mongodb://mongo:27017
      --env JWT_SECRET=test
      --name "${CI_PROJECT_ID}-${CI_PIPELINE_IID}"
      "testimg:latest"

  after_script:
    # NOTE: clean up after test run (stop and remove container, clean up network and mongo db)
    - docker rm --force "${CI_PROJECT_ID}-${CI_PIPELINE_IID}"
    - docker image rm --force mongo
    - docker network rm test-img-network
    - docker network ls
    - docker network prune
    - docker kill $(docker ps -q)

job_deploy-new-version:
  stage: deploy
  needs:
    - job_build-image
    - job_test-image
  tags:
    - docker
  trigger:
    - project: /s80984/devopsproject
    - branch: master
    - strategy: depend

  # image:
  #   name: quay.io/bitnami/kubectl:1.20.5
  #   entrypoint: ['']
  # script:
  #   - VERSION="${CI_PIPELINE_IID}-${CI_COMMIT_SHORT_SHA}"
  #   - IMAGE="${CI_REGISTRY_IMAGE}:${VERSION}"
  #   - DEPLOYMENT_MANIFEST=$(sed -e "s,{{image}},${IMAGE},g" ./infra/deployment.yaml.tmpl)
  #   - echo "${DEPLOYMENT_MANIFEST}" | kubectl apply
  #       --kubeconfig ${SECRET_KUBECONFIG_PATH}
  #       --namespace devops-lecture-showcase
  #       --filename -


# job_create-release:
#   stage: release
#   only:
#     - stable
#   needs:
#     - job_build-image
#   tags:
#     - docker-privileged
#   image: docker:20.10.12
#   services:
#     - name: docker:20.10.12-dind
#   before_script:
#     - docker login -u ${CI_REGISTRY_USER} -p ${CI_REGISTRY_PASSWORD} ${CI_REGISTRY}
#     - docker pull "${CI_REGISTRY_IMAGE}:${CI_PIPELINE_IID}-${CI_COMMIT_SHORT_SHA}"
#   script:
#     - VERSION=$(cat ./src/package.json | docker run --interactive stedolan/jq --raw-output '.version')
#     - docker tag "${CI_REGISTRY_IMAGE}:${CI_PIPELINE_IID}-${CI_COMMIT_SHORT_SHA}" "${CI_REGISTRY_IMAGE}:${VERSION}"
#     - docker push "${CI_REGISTRY_IMAGE}:${VERSION}"
#     - git tag "v${VERSION}" && git push origin "v${VERSION}"
#   after_script:
#     - docker image rm --force
#         $(docker images --format '{{.Repository}}{{.Tag}}' | grep '${CI_REGISTRY_IMAGE}')

# job_deploy-new-release:
#   only:
#     - stable
#   tags:
#     - docker-privileged
#   image: docker:20.10.12
#   services:
#     - name: docker:20.10.12-dind
#   script:
#     - VERSION=$(cat ./src/package.json | docker run --interactive stedolan/jq --raw-output '.version')
#     - IMAGE="${CI_REGISTRY_IMAGE}:${VERSION}"
#     - DEPLOYMENT_MANIFEST=$(sed -e "s/{{image}}/${IMAGE}/g" infra/deployment.yaml.tmpl)
#     - echo "${DEPLOYMENT_MANIFEST}" | docker run
#         --rm
#         --volume ${SECRET_KUBECONFIG_PATH}:/.kube/config
#         quay.io/bitnami/kubectl:1.20.5
#         apply --namespace devops-lecture-showcase --filename -


# job_update-infra:
#   stage: deploy
#   rules:
#     - changes:
#         - infra/*
#   tags:
#     - docker
#   image:
#     name: quay.io/bitnami/kubectl:1.20.5
#     entrypoint: ['']
#   script:
#     - kubectl apply
#         --kubeconfig ${SECRET_KUBECONFIG_PATH}
#         --namespace devops-lecture-showcase
#         --filename ./infra
