---
version: 2.1

orbs:
  prometheus: prometheus/prometheus@0.15.0
  go: circleci/go@1.7.0
  win: circleci/windows@2.3.0

executors:
  # Whenever the Go version is updated here, .promu.yml
  # should also be updated.
  golang:
    docker:
      - image: quay.io/prometheus/golang-builder:1.17-base
  golang_115:
    docker:
      - image: quay.io/prometheus/golang-builder:1.15-base

jobs:
  test_go:
    executor: golang

    steps:
      - prometheus/setup_environment
      - go/load-cache:
          key: v1
      - run:
          command: make GO_ONLY=1
          environment:
            # Run garbage collection more aggressively to avoid getting OOMed during the lint phase.
            GOGC: "20"
            # By default Go uses GOMAXPROCS but a Circle CI executor has many
            # cores (> 30) while the CPU and RAM resources are throttled. If we
            # don't limit this to the number of allocated cores, the job is
            # likely to get OOMed and killed.
            GOOPTS: "-p 2"
            GOMAXPROCS: "2"
            GO111MODULE: "on"
      - run: go test ./tsdb/ -test.tsdb-isolation=false
      - prometheus/check_proto:
          version: "3.15.8"
      - prometheus/store_artifact:
          file: prometheus
      - prometheus/store_artifact:
          file: promtool
      - go/save-cache:
          key: v1
      - store_test_results:
          path: test-results

  test_ui:
    executor: golang

    steps:
      - checkout
      - restore_cache:
          keys:
            - v3-npm-deps-{{ checksum "web/ui/package-lock.json" }}
            - v3-npm-deps-
      - run: make ui-install
      - run: make ui-lint
      - run: make ui-build-module
      - run: make ui-test
      - save_cache:
          key: v3-npm-deps-{{ checksum "web/ui/package-lock.json" }}
          paths:
            - ~/.npm

  test_windows:
    executor:
      name: win/default
      shell: powershell
    working_directory: /go/src/github.com/prometheus/prometheus
    steps:
      - checkout
      - run:
          # Temporary workaround until circleci updates go.
          command: |
            choco upgrade -y golang
      - run:
          command: refreshenv
      - run:
          command: |
            $env:GOARCH=""; $env:GOOS=""; cd web/ui; go generate
            cd ../..
            $TestTargets = go list ./... | Where-Object { $_ -NotMatch "(github.com/prometheus/prometheus/discovery.*|github.com/prometheus/prometheus/config|github.com/prometheus/prometheus/web)"}
            go test $TestTargets -vet=off -v
          environment:
            GOGC: "20"
            GOOPTS: "-p 2"

  test_tsdb_go115:
    executor: golang_115
    steps:
      - checkout
      - run: go test ./tsdb/...
      - run: go test ./tsdb/ -test.tsdb-isolation=false

  test_mixins:
    executor: golang
    steps:
      - checkout
      - run: go install ./cmd/promtool/.
      - run: go install github.com/google/go-jsonnet/cmd/jsonnet@latest
      - run: go install github.com/google/go-jsonnet/cmd/jsonnetfmt@latest
      - run: go install github.com/jsonnet-bundler/jsonnet-bundler/cmd/jb@latest
      - run: make -C documentation/prometheus-mixin clean
      - run: make -C documentation/prometheus-mixin jb_install
      - run: make -C documentation/prometheus-mixin
      - run: git diff --exit-code

  repo_sync:
    executor: golang
    steps:
      - checkout
      - run: ./scripts/sync_repo_files.sh
      - run: ./scripts/sync_codemirror.sh

workflows:
  version: 2
  prometheus:
    jobs:
      - test_go:
          filters:
            tags:
              only: /.*/
      - test_ui:
          filters:
            tags:
              only: /.*/
      - test_tsdb_go115:
          filters:
            tags:
              only: /.*/
      - test_mixins:
          filters:
            tags:
              only: /.*/
      - test_windows:
          filters:
            tags:
              only: /.*/
      - prometheus/build:
          name: build
          parallelism: 3
          promu_opts: "-p linux/amd64 -p windows/amd64 -p linux/arm64 -p darwin/amd64 -p darwin/arm64 -p linux/386"
          filters:
            tags:
              ignore: /^v[0-9]+(\.[0-9]+){2}(-.+|[^-.]*)$/
            branches:
              ignore: /^(main|release-.*|.*build-all.*)$/
      - prometheus/build:
          name: build_all
          parallelism: 12
          filters:
            branches:
              only: /^(main|release-.*|.*build-all.*)$/
            tags:
              only: /^v[0-9]+(\.[0-9]+){2}(-.+|[^-.]*)$/
      - prometheus/publish_main:
          context: org-context
          requires:
            - test_go
            - test_ui
            - build_all
          filters:
            branches:
              only: main
          image: circleci/golang:1-node
      - prometheus/publish_release:
          context: org-context
          requires:
            - test_go
            - test_ui
            - build_all
          filters:
            tags:
              only: /^v[0-9]+(\.[0-9]+){2}(-.+|[^-.]*)$/
            branches:
              ignore: /.*/
          image: circleci/golang:1-node
  nightly:
    triggers:
      - schedule:
          cron: "0 0 * * *"
          filters:
            branches:
              only:
                - main
    jobs:
      - repo_sync:
          context: org-context