variables:
  CONTAINER_IMAGE: 'registry.kszk.bme.hu/kszk/devteam/$CI_PROJECT_NAME:$CI_COMMIT_REF_NAME'

stages:
  - Pre Build
  - Docker Build
  - deploy

PreBuild:
  stage: Pre Build
  tags: [kszk]
  image: node:latest
  script:
    - npm install
    - npm run build
  cache:
    paths:
      - build/
  artifacts:
    expire_in: 1 day
    paths:
      - build/
  only:
    - tags

Docker build:
  stage: Docker Build
  tags: [kszk]
  only:
    - tags
  image:
    name: gcr.io/kaniko-project/executor:debug
    entrypoint: ['']
  script:
    - echo "{\"auths\":{\"registry.kszk.bme.hu\":{\"username\":\"$REGISTRY_USER\",\"password\":\"$REGISTRY_PASSWORD\"}}}" > /kaniko/.docker/config.json
    - /kaniko/executor --context $CI_PROJECT_DIR --dockerfile $CI_PROJECT_DIR/Dockerfile --destination $CONTAINER_IMAGE

Deploy to Kubernetes:
  stage: deploy
  image: alpine
  tags: [devteam]
  environment:
    name: master
  script:
    - apk add --no-cache curl
    - curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
    - chmod +x ./kubectl
    - mv ./kubectl /usr/local/bin/kubectl
    - kubectl version
    - cd k8s
    - sed -i "s|##IMAGETAG##|${CI_COMMIT_REF_NAME}|" deployment.yml
    - kubectl apply -f deployment.yml
  only:
    - tags
