
stages:
        - build
        - test
        - staging

variables:
        IMAGE_TAG: $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_NAME

build:
        image: docker:latest
        stage: build
        script:
                - docker login -u gitlab-ci-token -p $CI_JOB_TOKEN $CI_REGISTRY
                - docker build -t $IMAGE_TAG .
                - docker push $IMAGE_TAG


.test_template: &test_definition
        stage: test
        image: $IMAGE_TAG

test_coverage:
        <<: *test_definition
        services:
                - postgres:9.5
        script:
                - coverage run manage.py test --noinput
                - coverage report --skip-covered

test_pylint:
        <<: *test_definition
        script:
                - pylint core || exit 0


.staging_template: &staging_definition
        stage: staging
        image: docker:latest
        only:
                - master

staging:
        <<: *staging_definition
        environment:
                name: review/$CI_COMMIT_REF_NAME
                url: https://staging.grady.janmax.org
                on_stop: staging_stop
        script:
                - apk add --update py-pip && pip install docker-compose
                - docker login -u gitlab-ci-token -p $CI_JOB_TOKEN $CI_REGISTRY
                - docker-compose pull
                - docker-compose up -d --build

staging_stop:
        <<: *staging_definition
        script:
                - apk add --update py-pip && pip install docker-compose
                - docker-compose rm --force --stop
        when: manual
        environment:
                name: review/$CI_COMMIT_REF_NAME
                action: stop
