---
kind: pipeline
name: default

steps:
- name: download-dependencies
  image: golang:1.14
  commands:
    # wait for services to be ready.
    - cd scripts/liveness_probe
    - go run .
    - cd ../..
    - go mod download
    - go mod vendor # this is for when the Dockerfile gets built
  environment:
    GO111MODULE: on
    GOPROXY: http://athens-proxy:3000
  volumes:
  - name: cache
    path: /go

- name: build
  image: golang:1.14
  commands:

  # build
  - go build cmd/proxy/*.go

  # test
  - ./scripts/check_gofmt.sh
  - go vet ./...
  - go test -v ./...

  # end to end test
  - ./main & # run the just-built athens server
  - sleep 3 # wait for it to spin up
  - curl localhost:3000
  - mkdir -p ~/happy
  - mkdir -p ~/emptygopath # ensure the gopath has no modules cached.
  - cd ~/happy
  - git clone https://github.com/athens-artifacts/happy-path.git
  - cd happy-path
  - GOPATH=~/emptygopath GOPROXY=http://localhost:3000 go build
  environment:
    GO111MODULE: on
    ATHENS_MONGO_STORAGE_URL: mongodb://mongo:27017
    ATHENS_MINIO_ENDPOINT: minio:9000
    REDIS_TEST_ENDPOINT: redis:6379
    REDIS_SENTINEL_TEST_ENDPOINT: redis-sentinel:26379
    REDIS_SENTINEL_TEST_MASTER_NAME: redis-1
    REDIS_SENTINEL_TEST_PASSWORD: sekret
    PROTECTED_REDIS_TEST_ENDPOINT: protectedredis:6380
    ATHENS_PROTECTED_REDIS_PASSWORD: AthensPass1
    GCS_SERVICE_ACCOUNT:
      from_secret: GCS_SERVICE_ACCOUNT
    GCS_PROJECT_ID: 
      from_secret: GOOGLE_CLOUD_PROJECT
    ATHENS_AZURE_ACCOUNT_NAME:
      from_secret: ATHENS_AZURE_ACCOUNT_NAME
    ATHENS_AZURE_ACCOUNT_KEY: 
      from_secret: ATHENS_AZURE_ACCOUNT_KEY
  when:
    branch:
      - main
    event:
      - push
      - pull_request
  volumes:
  - name: cache
    path: /go

- name: docker-latest
  image: plugins/docker
  settings:
    username:
      from_secret: DOCKER_USER
    password:
      from_secret: DOCKER_PASSWORD
    repo: gomods/athens
    tags:
      - canary
    dockerfile: cmd/proxy/Dockerfile
    build_args:
      - VERSION=${DRONE_COMMIT}
  when:
    branch:
      - main
    event:
      - push

- name: docker-latest-commit-sha
  image: plugins/docker
  settings:
    username:
      from_secret: DOCKER_USER
    password:
      from_secret: DOCKER_PASSWORD
    repo: gomods/athens-dev
    tags:
      - ${DRONE_COMMIT:0:7}
    dockerfile: cmd/proxy/Dockerfile
    build_args:
      - VERSION=${DRONE_COMMIT}
  when:
    branch:
      - main
    event:
      - push

- name: docker-release
  image: plugins/docker
  settings:
    username:
      from_secret: DOCKER_USER
    password:
      from_secret: DOCKER_PASSWORD
    repo: gomods/athens
    tags:
      - ${DRONE_TAG}
      - latest
    dockerfile: cmd/proxy/Dockerfile
    build_args:
      - VERSION=${DRONE_TAG}

  when:
    event:
      - tag

- name: publish-helm
  image: gomods/drone-helm
  settings:
    charts_repo: https://athens.blob.core.windows.net
  environment:
    AZURE_STORAGE_CONNECTION_STRING:
      from_secret: AZURE_STORAGE_CONNECTION_STRING
  when:
    branch:
      - main
    event:
     - push

# Here we can add any backend storage that can be tested.
services:
- name: mongo
  image: mongo
  ports:
  - 27017
- name: minio
  image: minio/minio:latest
  command:
    - server
    - /data
  ports:
    - 9000
  environment:
    MINIO_ACCESS_KEY: minio
    MINIO_SECRET_KEY: minio123
- name: redis
  image: redis
  ports:
    - 6379
- name: redis-sentinel
  image: bitnami/redis-sentinel
  environment:
    REDIS_MASTER_HOST: redis
    REDIS_MASTER_SET: redis-1
    REDIS_SENTINEL_PASSWORD: sekret
    REDIS_SENTINEL_QUORUM: "1"
  ports:
    - 26379
- name: protectedredis
  image: redis
  ports:
    - 6380
  commands:
    - "redis-server ./test/redis.conf"
- name: athens-proxy
  image: gomods/athens:canary
  pull: always
  ports:
    - 3000
  environment:
    ATHENS_GOGET_WORKERS: 5
    ATHENS_PROTOCOL_WORKERS: 10
    ATHENS_SINGLE_FLIGHT_TYPE: gcp
    ATHENS_STORAGE_TYPE: gcp
    ATHENS_STORAGE_GCP_BUCKET: "athens_ci_build"
    ATHENS_STORAGE_GCP_JSON_KEY:
      from_secret: GCS_SERVICE_ACCOUNT
volumes:
  - name: cache
    temp: {}
---
kind: signature
hmac: 8ee2a04fedfd1a0c1ab3da19a3441485bf447de3e04fe057f626c83454a042b6

...
