variables:
  GIT_SUBMODULE_STRATEGY: recursive
  DOCKER_HUB_REPO: libresh/discourse

.compute_version: &compute_version
  - if [ "$CHANNEL" == "stable" ]; then export FILTER="grep -v beta"; else export FILTER="grep beta"; fi
  - export DISCOURSE_VERSION=`wget -qO- https://api.github.com/repos/discourse/discourse/tags | grep name | cut -d'"' -f4 | $FILTER | head -n1`
  - export DATE=`date "+%Y-%m-%dT%H-%M-%SZ"`

.test:
  image:
    name: gcr.io/kaniko-project/executor:debug
    entrypoint: [""]
  variables:
    DESTINATION: "--no-push"
  script:
    - *compute_version
    - export DISCOURSE_MAJOR=`echo $DISCOURSE_VERSION | cut -d"." -f1-3`
    - echo $DISCOURSE_MAJOR
    - echo $DISCOURSE_VERSION
    - /kaniko/executor --single-snapshot --context .  ${DESTINATION} --build-arg DISCOURSE_VERSION=$DISCOURSE_VERSION
  except:
  - master

test-beta:
  extends: .test
  variables:
    CHANNEL: "beta"

test-stable:
  extends: .test
  variables:
    CHANNEL: "stable"

.push:
  extends: .test
  before_script:
    - *compute_version
    - cp $DOCKER_SECRET_CONFIG /kaniko/.docker/config.json
    - export DESTINATION="--destination ${DOCKER_HUB_REPO}:${DISCOURSE_VERSION}-${DATE} --destination ${DOCKER_HUB_REPO}:${DISCOURSE_VERSION} --destination ${DOCKER_HUB_REPO}:${DISCOURSE_MAJOR} --destination ${DOCKER_HUB_REPO}:${CHANNEL}"
  except: []
  only:
  - master

# bug: the CHANNEL variable is not passed to the Dockerfile. 
push-beta:
  extends: .push
  variables:
    CHANNEL: "beta"

#push-stable:
#  extends: .push
#  variables:
#    CHANNEL: "stable"
