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

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
  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 $DOCKER_REPO:$CI_COMMIT_SHA $DOCKER_REPO:latest
    - docker push $DOCKER_REPO:latest

push:demo:
  image: docker:latest
  stage: push
  when: manual
  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 $DOCKER_REPO:$CI_COMMIT_SHA $DOCKER_REPO:demo
    - docker push $DOCKER_REPO:demo

push:staging:
  image: docker:latest
  stage: push
  only:
    - master
  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 $DOCKER_REPO:$CI_COMMIT_SHA $DOCKER_REPO:staging
    - docker push $DOCKER_REPO:staging

deploy:aws-qa:
  stage: deploy
  when: manual
  only:
    - develop
  script:
    - 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 --no-cache add --update python python-dev py-pip
    - pip install ecs-deploy
    # Deploy
    - ecs deploy ${CI_CLUSTER_NAME} ${CI_SERVICE_NAME} --region ${AWS_REGION} --timeout 1200 -e ${CI_CONTAINER_NAME} AWS_S3_ACCESS_KEY $AWS_S3_ACCESS_KEY -e ${CI_CONTAINER_NAME} AWS_S3_SECRET_KEY $AWS_S3_SECRET_KEY -e ${CI_CONTAINER_NAME} AWS_S3_REGION $AWS_S3_REGION -e ${CI_CONTAINER_NAME} AWS_S3_BUCKET $AWS_S3_BUCKET -e ${CI_CONTAINER_NAME} AWS_SES_SECRET_KEY $AWS_SES_SECRET_KEY -e ${CI_CONTAINER_NAME} AWS_SES_ACCESS_KEY $AWS_SES_ACCESS_KEY -e ${CI_CONTAINER_NAME} AWS_SES_REGION $AWS_SES_REGION -e ${CI_CONTAINER_NAME} EMAIL_SENDER $EMAIL_SENDER -e ${CI_CONTAINER_NAME} secret $SECRET -e ${CI_CONTAINER_NAME} DATABASE $DATABASE -e ${CI_CONTAINER_NAME} DB_USER $DB_USER -e ${CI_CONTAINER_NAME} DB_PASS $DB_PASS -e ${CI_CONTAINER_NAME} DB_HOST $DB_HOST -e ${CI_CONTAINER_NAME} ORCID_CLIENT_ID $ORCID_CLIENT_ID -e ${CI_CONTAINER_NAME} ORCID_CLIENT_SECRET $ORCID_CLIENT_SECRET -e ${CI_CONTAINER_NAME} CLIENT_BASE_URL $CLIENT_BASE_URL -e ${CI_CONTAINER_NAME} FTP_USERNAME $FTP_USERNAME -e ${CI_CONTAINER_NAME} FTP_PASSWORD $FTP_PASSWORD -e ${CI_CONTAINER_NAME} FTP_HOST $FTP_HOST 
  environment:
    name: qa
    url: $CI_ALB_URL

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

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