version: 2
jobs:
  build:
    working_directory: /go/src/github.com/mozilla/tls-observatory
    docker:
      - image: golang:1.15
      - image: circleci/postgres:11-alpine
        environment:
          POSTGRES_USER: postgres
          POSTGRES_DB: observatory
          POSTGRES_PASSWORD: ""
    steps:
      - checkout
      - run: git submodule update --init
      - run: apt-get update && apt-get install -y dos2unix postgresql-client unzip docker.io
      - run:
          name: Build TLS Observatory
          command: |
            make truststores cipherscan ciscotop1m alexatop1m
            make
      - run:
          name: Run a simple test scan
          command: |
            psql -h 127.0.0.1 -U postgres -d observatory -f database/schema.sql
            ln -s $GOPATH/src/github.com/mozilla/tls-observatory/conf /etc/tls-observatory
            ln -s $GOPATH/src/github.com/mozilla/tls-observatory/cipherscan /opt/cipherscan
            $GOPATH/bin/tlsobs-scanner &
            $GOPATH/bin/tlsobs-api &
            # send SIGKILL after 10m and SIGHUP after 5m
            timeout --kill-after=10m --signal=HUP 5m $GOPATH/bin/tlsobs -observatory http://localhost:8083 www.mozilla.org || exit 1
      - run:
          name: Create version.json
          command: |
            printf '{"commit":"%s","version":"%s","source":"https://github.com/%s/%s","build":"%s"}\n' \
            "$CIRCLE_SHA1" \
            "$CIRCLE_TAG" \
            "$CIRCLE_PROJECT_USERNAME" \
            "$CIRCLE_PROJECT_REPONAME" \
            "$CIRCLE_BUILD_URL" > version.json
      - setup_remote_docker
      - run:
          name: Build containers
          command: |
            docker build -t mozilla/tls-observatory .
            docker tag mozilla/tls-observatory "mozilla/tls-observatory:latest"
      - run:
          name: Push containers
          command: |
             if [ ! -z "${CIRCLE_TAG}" ]; then
                 docker login -u="$DOCKER_USERNAME" -p="$DOCKER_PASSWORD"
                 docker tag mozilla/tls-observatory "mozilla/tls-observatory:$CIRCLE_TAG"
                 docker push "mozilla/tls-observatory:latest"
                 docker push "mozilla/tls-observatory:$CIRCLE_TAG"
             fi

workflows:
  version: 2

  # workflow jobs are _not_ run in tag builds by default
  # we use filters to whitelist jobs that should be run for tags

  # workflow jobs are run in _all_ branch builds by default
  # we use filters to blacklist jobs that shouldn't be run for a branch

  # see: https://circleci.com/docs/2.0/workflows/#git-tag-job-execution

  build-test-push:
    jobs:
      - build:
          filters:
            tags:
              only: /.*/
