stages:
  - build_master
  - build_develop

build_develop:
  stage: build_develop
  image: 'docker:18.09.7'
  except:
    - master
  services:
    - 'docker:18.09.7-dind'
  script:
    - docker build -t $CI_REGISTRY_IMAGE:develop .
    - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
    - docker push $CI_REGISTRY_IMAGE:develop

build_image:
  stage: build_master
  image: 'docker:18.09.7'
  only:
    - master
  services:
    - 'docker:18.09.7-dind'
  variables:
    DOCKERHUB_NAME: clarinpl/$CI_PROJECT_NAME
  script:
    - docker build -t $DOCKERHUB_NAME .
    - echo $DOCKER_PASSWORD > pass.txt
    - cat pass.txt | docker login --username $DOCKER_USERNAME --password-stdin
    - rm pass.txt
    - docker push $DOCKERHUB_NAME
    - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
    - docker image tag $DOCKERHUB_NAME $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG
    - docker image tag $DOCKERHUB_NAME $CI_REGISTRY_IMAGE:latest
    - docker push $CI_REGISTRY_IMAGE

