variables:
  CONTAINER_IMAGE: 'harbor.sch.bme.hu/kszk/$CI_PROJECT_NAME:$CI_COMMIT_REF_NAME'

stages:
  - Pre Build
  - Docker Build
  - deploy

PreBuild:
  stage: Pre Build
  tags: [kszk]
  image: node:14.19-alpine3.14
  script:
    -   'echo -n "Node version: " && node --version \
      && echo -n "NPM version: " && npm --version'
    - 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\":{\"harbor.sch.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: [kszk]
  environment:
    name: master
  before_script:
    - chmod 600 $KUBECONFIG
    - 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
  script:
    - cd k8s
    - sed -i "s|##IMAGETAG##|${CI_COMMIT_REF_NAME}|" deployment.yml
    - kubectl apply -f deployment.yml
  only:
    - tags
