version: v1.0
name: node
agent:
  machine:
    type: e1-standard-2
    os_image: ubuntu1804

execution_time_limit:
  hours: 4

global_job_config:
  secrets:
  - name: docker-hub
  prologue:
    commands:
      # make some room on the disk
      - sudo rm -rf ~/.kiex ~/.phpbrew ~/.rbenv ~/.nvm ~/.kerl ~/.sbt ~/.npm /usr/lib/jvm /opt/firefox* /opt/apache-maven* /opt/scala /usr/local/golang
      # Semaphore mounts a copy-on-write FS as /var/lib/docker in order to provide a pre-loaded cache of
      # some images. However, the cache is not useful to us and the copy-on-write FS is a big problem given
      # how much we churn docker containers during the build.  Disable it.
      - sudo systemctl stop docker
      - sudo umount /var/lib/docker && sudo killall qemu-nbd || true
      - sudo systemctl start docker
      - echo $DOCKERHUB_PASSWORD | docker login --username "$DOCKERHUB_USERNAME" --password-stdin
      - checkout
      # Semaphore is doing shallow clone on a commit without tags.
      # unshallow it for GIT_VERSION:=$(shell git describe --tags --dirty --always) @ Makefile.common
      - git fetch --unshallow

blocks:
  - name: "Build Images"
    dependencies: []
    task:
      jobs:
        - name: "Build all node images"
          execution_time_limit:
            minutes: 120
          commands:
            - TEST_IMAGE_BUILD=true make image-all
        - name: "Build Windows images"
          execution_time_limit:
            minutes: 120
          commands:
            - make build-windows-upgrade-archive image-tar-windows-all

  - name: "General Tests"
    dependencies: []
    task:
      jobs:
        - name: "Static Checks / UT and FV tests"
          execution_time_limit:
            minutes: 120
          commands:
            - make static-checks
            - make ut
            - make fv

  - name: "Build amd64 Image"
    dependencies: []
    task:
      jobs:
        - name: "Build amd64 Image"
          execution_time_limit:
            minutes: 120
          commands:
            - make image

  - name: "System Tests"
    dependencies: ["Build amd64 Image"]
    task:
      prologue:
        commands:
          - make mod-download
      jobs:
        - name: "BGP Tests"
          execution_time_limit:
            minutes: 120
          commands:
            - ST_TO_RUN=tests/st/bgp/ make st
        - name: "CalicoCtl Tests"
          execution_time_limit:
            minutes: 120
          commands:
            - ST_TO_RUN=tests/st/calicoctl/ make st
#        Config tests don't seem to work and seem like they were never run
#        - name: "Config Tests"
#          commands:
#            - ST_TO_RUN=tests/st/config/ make st
        - name: "IPAM Tests"
          execution_time_limit:
            minutes: 120
          commands:
            - ST_TO_RUN=tests/st/ipam/ make st
        - name: "Policy Tests"
          execution_time_limit:
            minutes: 120
          commands:
            - ST_TO_RUN=tests/st/policy/ make st
        - name: "Utils Tests"
          execution_time_limit:
            minutes: 120
          commands:
            - ST_TO_RUN=tests/st/utils/ make st

  - name: "Specialized Tests"
    dependencies: []
    task:
      agent:
        machine:
          type: e1-standard-4
          os_image: ubuntu1804
      jobs:
        - name: "Dual Stack Tests"
          execution_time_limit:
            minutes: 120
          commands:
            - make k8s-test

  # If tests passed, then build and push images.
  # We'll only do this on non-PR builds, where we have credentials to do so.
  - name: "Push images (non-PR builds only)"
    skip:
      # Only run on branches, not PRs.
      when: "branch !~ '.+'"
    dependencies: ["General Tests", "System Tests", "Specialized Tests"]
    task:
      secrets:
      - name: quay-robot-calico+semaphoreci
      - name: docker
      prologue:
        commands:
        - echo $DOCKER_TOKEN | docker login --username "$DOCKER_USER" --password-stdin
        - echo $QUAY_TOKEN | docker login --username "$QUAY_USER" --password-stdin quay.io
        - export BRANCH_NAME=$SEMAPHORE_GIT_BRANCH
      jobs:
      - name: "make cd"
        execution_time_limit:
          minutes: 120
        commands:
        - >-
          if [ -z "${SEMAPHORE_GIT_PR_NUMBER}" ]; then
              make image-all cd CONFIRM=true
          fi
      - name: "make cd-windows-upgrade"
        execution_time_limit:
          minutes: 120
        commands:
        - >-
          if [ -z "${SEMAPHORE_GIT_PR_NUMBER}" ]; then
              make build-windows-upgrade-archive image-tar-windows-all cd-windows-upgrade CONFIRM=true
          fi

  - name: "build windows archive"
    dependencies: ["General Tests", "System Tests", "Specialized Tests"]
    task:
      jobs:
      - name: "build windows archive"
        execution_time_limit:
          minutes: 120
        commands:
        - export BRANCH_NAME=$SEMAPHORE_GIT_BRANCH
        - make release-windows-archive VERSION=${BRANCH_NAME}
        - make build-windows-upgrade-archive

promotions:
  # Run the pin update process in case there were a backlog of pin update requests
  - name: Update Pins
    pipeline_file: update_pins.yml
    auto_promote:
      # If the block has passed and the branch is for master or a release branch then run the pin updates. Note that
      # this doesn't try to restrict which release branches, as the presence of this auto promotion code means that
      # it can handle updating the pins in this fashion.
      when: "(result = 'passed') and ((branch = 'master') or (branch =~ '^release-v\d*\.\d*'))"
