variables:
  IMAGE_NAME: your_registry/<%= appname %>

stages:
  - build
  - push
  - deploy
  - cleanup

build_staging:
  stage: build
  script:
    - npm run build
    - docker build -t $IMAGE_NAME:staging -f Dockerfile.build .
  only:
    - develop
    - /^release\/.*$/
  tags:
    - build

push_staging:
  stage: push
  script:
    - docker push $IMAGE_NAME:staging
  only:
    - develop
    - /^release\/.*$/
  tags:
    - push

deploy_staging:
  stage: deploy
  script:
    - docker-compose -f staging.yml pull
    - docker-compose -f staging.yml up -d
  only:
    - develop
    - /^release\/.*$/
  tags:
    - staging

cleanup_staging:
  stage: cleanup
  script:
    - EMPTYIMAGE=$(docker images | awk '{print $1, $3}' | grep '^<none>' | awk '{print $2}') sh -c 'if [ ! -z "$EMPTYIMAGE" ]; then docker rmi $EMPTYIMAGE; fi'
  only:
    - develop
    - /^release\/.*$/
  tags:
    - staging
  allow_failure: true

build_production:
  stage: build
  script:
    - npm run build
    - docker build -t $IMAGE_NAME:dist -f Dockerfile.build .
  only:
    - master
  tags:
    - build

push_production:
  stage: push
  script:
    - docker push $IMAGE_NAME:dist
  only:
    - master
  tags:
    - push

deploy_production:
  stage: deploy
  script:
    - docker-compose -f production.yml pull
    - docker-compose -f production.yml up -d
  only:
    - master
  tags:
    - production

cleanup_production:
  stage: cleanup
  script:
    - EMPTYIMAGE=$(docker images | awk '{print $1, $3}' | grep '^<none>' | awk '{print $2}') sh -c 'if [ ! -z "$EMPTYIMAGE" ]; then docker rmi $EMPTYIMAGE; fi'
  only:
    - master
  tags:
    - production
  allow_failure: true
