###################################################################
#  _____ _      _          _____ _             _ _                #
# |  __ (_)    (_)        / ____| |           | (_)               #
# | |__) |__  ___  ___   | (___ | |_ _   _  __| |_  ___           #
# |  ___/ \ \/ / |/ _ \   \___ \| __| | | |/ _` | |/ _ \          #
# | |   | |>  <| | (_) |  ____) | |_| |_| | (_| | | (_) |         #
# |_|   |_/_/\_\_|\___/  |_____/ \__|\__,_|\__,_|_|\___/          #
#                                                                 #
###################################################################
#
#
# This is a sample build configuration for PHP.
# Check our guides at https://pixiostudio.com/supports for more examples.
# Only use spaces to indent your .yml configuration.
# -----
# You can specify a custom docker image from Docker Hub as your build environment.
#

# image: samueldebruyn/debian-git
#

options:
  docker: true
  size: 2x

pipelines:
  branches:
    master:
      - step:
          name: Publish image
          script:
            - export IMAGE_REPO=eastplayers/csdesign
            - mkdir -p storage/framework/{sessions,views,cache}
            - >- 
              docker build 
              -t $IMAGE_REPO:0.1.$BITBUCKET_BUILD_NUMBER
              -t $IMAGE_REPO:latest
              -f deployment/Dockerfile
              .
            - >-
              docker login 
              -u $DOCKER_HUB_USER
              -p $DOCKER_HUB_PASSWORD
            - docker push $IMAGE_REPO:0.1.$BITBUCKET_BUILD_NUMBER
            - docker push $IMAGE_REPO:latest
          services:
            - docker
          size: 2x
      - step:
          name: Deploy to Develop
          image: eastplayers/bitbucket-kubectl
          script:
            - export NAMESPACE="production"
            - export SERVER_URL=${K8S_SERVER_URL}
            - export TOKEN=${K8S_PRODUCTION_TOKEN}
            - export IMAGE_NAME=eastplayers/csdesign:0.1.$BITBUCKET_BUILD_NUMBER
            - export DEPLOYMENT="csdesign"
            - export CONTAINER_NAME="csdesign"

            - curl https://raw.githubusercontent.com/Foxhound401/k8s-rollout-action/master/backup-entrypoint.sh > bitbucket-deploy.sh
            - chmod +x ./bitbucket-deploy.sh
            - >-
              ./bitbucket-deploy.sh \
                $SERVER_URL \
                $TOKEN \
                $DEPLOYMENT \
                $IMAGE_NAME \
                $NAMESPACE \
                $CONTAINER_NAME
      - step:
          name: Clear Cache Cloudflare
          script:
            - bash clear-cache-cloudflare.sh ${CF_EP_ZONE} ${CF_EP_TOKEN}




