image: golang:1.9.2
pipelines:
  branches:
    master:
      - step:
          script:
            - curl https://glide.sh/get -L -o - | sh
            - mkdir -p /go/src/bitbucket.org/nemt/nemt-portal-api
            - pwd
            - ls -al
            - cp -R ./* /go/src/bitbucket.org/nemt/nemt-portal-api/
            - cd /go/src/bitbucket.org/nemt/nemt-portal-api/
            - rm -rf glide.lock
            - rm -rf vendor/
            - glide install -force
            - go build -o nemt-portal-api
            - mkdir -p dist/{static,docs,certs}
            - cp nemt-portal-api ./dist/
            - cp -R static/* ./dist/static/
            - cp -R docs/swagger/ ./dist/docs/
            - cp -R certs/prd/* ./dist/certs/
            - cp config.prd.toml ./dist/config.toml
            - cp default.prd.conf ./dist/default.conf
            - cp nginx.conf ./dist/nginx.conf
            - cp authorization_model.conf ./dist/authorization_model.conf
            - cp authorization_policy.csv ./dist/authorization_policy.csv
            - docker build -f Dockerfile.run -t nemt-portal-api-run:prod --force-rm --build-arg BIN_NAME=nemt-portal-api --build-arg APP_NAME=nemt-portal-api .
            - apt-get update -y && apt-get upgrade -y && apt-get dist-upgrade -y &&  apt-get -y autoremove &&  apt-get clean
            - apt-get install python3-pip -y
            - pip3 install --upgrade awscli
            - eval $(aws ecr get-login --no-include-email --region us-east-2)
            - docker tag nemt-portal-api-run:prod 105690980714.dkr.ecr.us-east-2.amazonaws.com/nemt-portal-api:prod
            - docker push 105690980714.dkr.ecr.us-east-2.amazonaws.com/nemt-portal-api:prod
            - eval $(aws --region us-east-2 ecs register-task-definition --cli-input-json file://ecs.prd.json >/dev/null)
            - eval $(aws --region us-east-2 ecs update-service --cluster App --service portal-api --task-definition portal-api-prod >/dev/null)
    development:
      - step:
          script:
            - curl https://glide.sh/get -L -o - | sh
            - mkdir -p /go/src/bitbucket.org/nemt/nemt-portal-api
            - pwd
            - ls -al
            - cp -R ./* /go/src/bitbucket.org/nemt/nemt-portal-api/
            - cd /go/src/bitbucket.org/nemt/nemt-portal-api/
            - rm -rf glide.lock
            - rm -rf vendor/
            - glide install -force
            - go build -o nemt-portal-api
            - mkdir -p dist/{static,docs,certs}
            - cp nemt-portal-api ./dist/
            - cp -R static/* ./dist/static/
            - cp -R docs/swagger/ ./dist/docs/
            - cp -R certs/stg/* ./dist/certs/
            - cp config.stg.toml ./dist/config.toml
            - cp default.stg.conf ./dist/default.conf
            - cp nginx.conf ./dist/nginx.conf
            - cp authorization_model.conf ./dist/authorization_model.conf
            - cp authorization_policy.csv ./dist/authorization_policy.csv
            - docker build -f Dockerfile.run -t nemt-portal-api-run:dev --force-rm --build-arg BIN_NAME=nemt-portal-api --build-arg APP_NAME=nemt-portal-api .
            - apt-get update -y && apt-get upgrade -y && apt-get dist-upgrade -y &&  apt-get -y autoremove &&  apt-get clean
            - apt-get install python3-pip -y
            - pip3 install --upgrade awscli
            - eval $(aws ecr get-login --no-include-email --region us-east-2)
            - docker tag nemt-portal-api-run:dev 105690980714.dkr.ecr.us-east-2.amazonaws.com/nemt-portal-api:dev
            - docker push 105690980714.dkr.ecr.us-east-2.amazonaws.com/nemt-portal-api:dev
            - eval $(aws --region us-east-2 ecs register-task-definition --cli-input-json file://ecs.stg.json >/dev/null)
            - eval $(aws --region us-east-2 ecs update-service --cluster NemtCluster2 --service portal-api --task-definition portal-api-dev >/dev/null)
options:
  docker: true