variables:
  CONFIGURATION_REPOSITORY: https://gitlab.coko.foundation/bogdan/xpub-faraday-deployment-config.git
  DOCKER_REPO: $CI_ECR_URL
  DOCKER_REPO_REVIEW: $REVIEW_ECR

stages:
  - build
  - test
  - push
  - deploy
  - rollback

build:
  image: docker:latest
  stage: build
  script:
    # Setup
    - export AWS_REGION="eu-west-1"
    - export AWS_ACCESS_KEY_ID=$AWS_ACCESS_KEY_ID
    - export AWS_SECRET_ACCESS_KEY=$AWS_SECRET_ACCESS_KEY
    - apk update
    - apk --no-cache add --update curl python python-dev py-pip
    - pip install awscli --upgrade --user
    - export PATH=~/.local/bin:/usr/bin/:$PATH
    # AUTH
    - CERT=`aws ecr get-login --no-include-email --region ${AWS_REGION}`
    - ${CERT}
    # Build
    - docker build -t ${CI_PROJECT_NAME}:$CI_COMMIT_SHA .
    - docker tag $CI_PROJECT_NAME:$CI_COMMIT_SHA $DOCKER_REPO:$CI_COMMIT_SHA
    - docker push $DOCKER_REPO:$CI_COMMIT_SHA

lint:
  image: $DOCKER_REPO:$CI_COMMIT_SHA
  stage: test
  variables:
    GIT_STRATEGY: none
  script:
    - cd ${HOME}
    - npm run lint

test:
  image: $DOCKER_REPO:$CI_COMMIT_SHA
  stage: test
  variables:
    GIT_STRATEGY: none
  script:
    - cd ${HOME}
    - npm run test

push:qa:
  image: docker:latest
  stage: push
  only:
    - develop
  variables:
    NODE_ENV: test
  script:
    # Setup
    - export AWS_REGION="eu-west-1"
    - export AWS_ACCESS_KEY_ID=$AWS_ACCESS_KEY_ID
    - export AWS_SECRET_ACCESS_KEY=$AWS_SECRET_ACCESS_KEY
    - apk update
    - apk --no-cache add --update curl python python-dev py-pip
    - pip install awscli --upgrade --user
    - export PATH=~/.local/bin:/usr/bin/:$PATH
    # AUTH
    - CERT=`aws ecr get-login --no-include-email --region ${AWS_REGION}`
    - ${CERT}
    # Tag
    - docker tag ${CI_PROJECT_NAME}:$CI_COMMIT_SHA $DOCKER_REPO:latest
    - docker push $DOCKER_REPO:latest

push:staging:
  image: docker:latest
  when: manual
  only:
    - staging
    - master
  stage: push
  variables:
    NODE_ENV: production
  script:
    # Setup
    - export AWS_REGION="eu-west-1"
    - export AWS_ACCESS_KEY_ID=$AWS_ACCESS_KEY_ID
    - export AWS_SECRET_ACCESS_KEY=$AWS_SECRET_ACCESS_KEY
    - apk update
    - apk --no-cache add --update curl python python-dev py-pip
    - pip install awscli --upgrade --user
    - export PATH=~/.local/bin:/usr/bin/:$PATH
    # AUTH
    - CERT=`aws ecr get-login --no-include-email --region ${AWS_REGION}`
    - ${CERT}
    # Tag
    - docker tag ${CI_PROJECT_NAME}:$CI_COMMIT_SHA $DOCKER_REPO:staging
    - docker push $DOCKER_REPO:staging

push:production:review:
  image: docker:latest
  stage: push
  when: manual
  only:
    - master
  variables:
    NODE_ENV: production
  script:
    # Setup
    - export AWS_REGION="eu-west-1"
    - export AWS_ACCESS_KEY_ID=$REVIEW_AWS_ACCESS
    - export AWS_SECRET_ACCESS_KEY=$REVIEW_AWS_SECRET
    - apk update
    - apk --no-cache add --update curl python python-dev py-pip
    - pip install awscli --upgrade --user
    - export PATH=~/.local/bin:/usr/bin/:$PATH
    # AUTH
    - CERT=`aws ecr get-login --no-include-email --region ${AWS_REGION}`
    - ${CERT}
    # Tag
    - docker tag ${CI_PROJECT_NAME}:$CI_COMMIT_SHA $DOCKER_REPO_REVIEW:production
    - docker push $DOCKER_REPO_REVIEW:production

deploy:qa:
  image: pubsweet/deployer:latest
  stage: deploy
  only:
    - develop
  variables:
    PACKAGE_NAME: xpub-faraday
    IMAGE_REPO: $CI_ECR_URL
    IMAGE_TAG: latest
    NODE_ENV: test
  environment:
    name: qa
    url: http://qa.review.hindawi.com
  script:
    - source deploy.sh
    - create_deployment

deploy:styleguide:
  image: pubsweet/deployer:latest
  stage: deploy
  when: manual
  only:
    - develop
  variables:
    PACKAGE_NAME: xpub-styleguide
    IMAGE_REPO: $CI_ECR_URL
    IMAGE_TAG: latest
  environment:
    name: styleguide
    url: http://styleguide.review.hindawi.com
  script:
    - source deploy.sh
    - create_deployment

deploy:staging:
  image: pubsweet/deployer:latest
  stage: deploy
  when: manual
  only:
    - staging
    - master
  variables:
    PACKAGE_NAME: xpub-faraday
    IMAGE_REPO: $CI_ECR_URL
    IMAGE_TAG: staging
    NODE_ENV: production
  environment:
    name: staging
    url: http://demo.review.hindawi.com
  script:
    - source deploy.sh
    - create_deployment

deploy:production:review:
  image: pubsweet/deployer:latest
  stage: deploy
  when: manual
  only:
    - master
  variables:
    PACKAGE_NAME: xpub-faraday
    IMAGE_REPO: $REVIEW_ECR
    IMAGE_TAG: production
    NODE_ENV: production
  environment:
    name: production
    url: http://review.hindawi.com
  script:
    - source deploy.sh
    - create_deployment
