stages:
  - build
  - deploy

.kube-context:
  before_script:
    - if [ -n "$KUBE_CONTEXT" ]; then kubectl config use-context "$KUBE_CONTEXT"; fi

build:
  stage: build
  image: quay.io/buildah/stable
  variables:
    STORAGE_DRIVER: vfs
    BUILDAH_FORMAT: docker
    IMAGE: "$REGISTRY/$REGISTRY_USERNAME/$CI_PROJECT_NAME"
  before_script:
    - buildah login -u "$REGISTRY_USERNAME" -p "$REGISTRY_PASSWORD" "$REGISTRY"
  script:
    - buildah images
    - buildah build -t $IMAGE:latest -t $IMAGE:$(sh VERSION)
    - buildah images
    - buildah push --all $IMAGE
  after_script:
    - buildah logout "$REGISTRY"

deploy:
  stage: deploy
  when: always
  extends: [.kube-context]
  image:
    name: bitnami/kubectl:latest
    entrypoint: [""]
  script:
    - ls
    - kubectl apply -f $CI_PROJECT_DIR/deployment.yaml
    - kubectl get pods
