# Define environment variables for all stages
variables:
  # Tell docker images where the host docker daemon is
  # and how to talk to it
  DOCKER_TLS_CERTDIR: ""
  DOCKER_HOST: "tcp://docker:2375"
  BUILDBUCKET_IMAGE: $CI_REGISTRY_IMAGE/buildbucket
  GRIDDED_GLM_IMAGE: $CI_REGISTRY_IMAGE/gridded_glm

stages:
    - buildbucket_build
    - gridded_glm_build
    - gridded_glm_tester_build
    - test
    - release

create the Gridded GLM buildbucket:
  stage: buildbucket_build
  image: docker:19.03.1
  tags:
    - docker
  services:
    - docker:19.03.1-dind
  script:
    - docker login --username $CI_REGISTRY_USER --password $CI_REGISTRY_PASSWORD $CI_REGISTRY
    - docker build -f buildbucket/Dockerfile --tag $BUILDBUCKET_IMAGE:$CI_COMMIT_SHORT_SHA --cache-from $BUILDBUCKET_IMAGE:latest buildbucket/
    - docker push $BUILDBUCKET_IMAGE:$CI_COMMIT_SHORT_SHA

create the Gridded GLM package:
  stage: gridded_glm_build
  image: docker:19.03.1
  tags:
    - docker
  services:
    - docker:19.03.1-dind
  script:
    - docker login --username $CI_REGISTRY_USER --password $CI_REGISTRY_PASSWORD $CI_REGISTRY
    # Creates the gridded GLM package
    - docker run --rm -v "${PWD}":/dock -v $PWD:/work/cspp-geo-gridded-glm $BUILDBUCKET_IMAGE:$CI_COMMIT_SHORT_SHA ./package.sh $CI_COMMIT_SHORT_SHA
    # Create a simple Docker image with the gridded GLM package included
    # TODO: this should probably be its own stage?
    - docker build . -f Dockerfile -t $GRIDDED_GLM_IMAGE:$CI_COMMIT_SHORT_SHA
    - docker push $GRIDDED_GLM_IMAGE:$CI_COMMIT_SHORT_SHA
# worry about this later; rsync isn't available in busybox. we probably need some refactoring to use a build image more directly & upload from there.
#    # upload the package to /apollo/ via a limited-use key on espresso
#    - rsync -auv -e "ssh -T -o StrictHostKeyChecking=no -o IdentitiesOnly=yes -o ForwardAgent=no -i $NICKB_UPLOAD_KEY" cspp-geo-gridded-glm-$CI_COMMIT_SHORT_SHA.tar.xz nickb@espresso:/apollo/csppgeo/packages/gridded-glm/ci/cspp-geo-gridded-glm-$CI_COMMIT_SHORT_SHA.tar.xz

push our images as latest tags:
  stage: release
  image: docker:19.03.1
  tags:
    - docker
  services:
    - docker:19.03.1-dind
  script:
    - docker login --username $CI_REGISTRY_USER --password $CI_REGISTRY_PASSWORD $CI_REGISTRY
# buildbucket
    - docker pull $BUILDBUCKET_IMAGE:$CI_COMMIT_SHORT_SHA
    - docker tag $BUILDBUCKET_IMAGE:$CI_COMMIT_SHORT_SHA $BUILDBUCKET_IMAGE:latest
    - docker push $BUILDBUCKET_IMAGE:latest
# gridded_glm
    - docker pull $GRIDDED_GLM_IMAGE:$CI_COMMIT_SHORT_SHA
    - docker tag $GRIDDED_GLM_IMAGE:$CI_COMMIT_SHORT_SHA $GRIDDED_GLM_IMAGE:latest
    - docker push $GRIDDED_GLM_IMAGE:latest

  only:
    - tags
    - master
