version: 2.1

orbs:
  slack: circleci/slack@3.4.2

commands:
  prepare-ssh-keys:
    description: Prepare SSH keys to be mounted with the right permissions for Git
    steps:
      - run:
          name: Prepare SSH keys to be mounted with the right permissions for Git
          command: |
            if [ ! -z "$SSH_KEY_NAME" ]; then
              sudo cp -r $HOME/.ssh $HOME/project/
              printf "Host github.com\n  IdentityFile /root/.ssh/$SSH_KEY_NAME\n" | sudo tee $HOME/project/.ssh/config
              sudo chmod -R 600 $HOME/project/.ssh
              sudo chown -R root:root $HOME/project/.ssh
            else
              echo "No SSH key found. Skipping..."
            fi
  docker-run:
    # enabling docker executor will require to use large resource class, which
    # implies extra cost - we don't really have to do that, we can just run the
    # image this way
    description: Run docker command
    parameters:
      cmd:
        type: string
      options:
        type: string
        default: ""
      timeout:
        type: string
        default: 30m
    steps:
      - run:
          name: docker run << parameters.cmd >>
          command: |
            docker run \
              << parameters.options >> \
              --env=JUNIT_REPORT_DIR=/src/test-results \
              --env=GOPRIVATE \
              --volume=$(pwd):/src \
              --volume=$HOME/.cache/go-build/:/root/.cache/go-build \
              --volume=$HOME/go/pkg/mod/:/go/pkg/mod \
              --volume=$HOME/project/.ssh:/root/.ssh \
              weaveworks/eksctl-build:$(cat build/docker/image_tag) << parameters.cmd >>
          no_output_timeout: << parameters.timeout >>
  docker-pull-build-image:
    description: Docker pull image
    steps:
      - run: docker pull "weaveworks/eksctl-build:$(cat build/docker/image_tag)"
  restore-cache:
    description: Restore cache
    steps:
      - restore_cache:
          keys:
            - go-cache-{{ checksum "go.sum" }}
            - go-cache-
  save-cache:
    description: Save cache
    steps:
      - run:
          name: Prepare cache to be saved
          command: |
            sudo chown -R circleci.circleci $HOME/.cache/go-build/
            sudo chown -R circleci.circleci $HOME/go/pkg/mod/
      - save_cache:
          key: go-cache-{{ checksum "go.sum" }}
          paths:
            - "/home/circleci/.cache/go-build/"
            - "/home/circleci/go/pkg/mod/"
  notify-slack:
    description: Notify failures via Slack
    parameters:
      fail_only:
        type: boolean
        default: true
      only_for_branches:
        type: string
      success_message:
        type: string
        default: ':tada: A $CIRCLE_JOB job has succeeded!'
      failure_message:
        type: string
        default: ':red_circle: A $CIRCLE_JOB job has failed!'
      channel:
        type: string
        default: team-pitch-black
    steps:
      - run: exit 0
      - slack/status:
          channel: << parameters.channel >>
          include_job_number_field: false
          include_project_field: false
          fail_only: << parameters.fail_only >>
          success_message: << parameters.success_message >>
          failure_message: << parameters.failure_message >>
          only_for_branches: << parameters.only_for_branches >>

jobs:
  test-and-build:
    machine:
      image: ubuntu-2004:202104-01
    environment:
      GOPRIVATE: github.com/weaveworks/aws-sdk-go-private
    steps:
      - checkout
      - add_ssh_keys:
          fingerprints:
            - "81:df:39:29:89:d6:34:ed:2e:4e:70:71:f1:f3:f6:0c"
      - prepare-ssh-keys
      - restore-cache
      - docker-pull-build-image
      - run: mkdir test-results
      - docker-run:
          cmd: go mod download
      - docker-run:
          cmd: make circleci-test build
      - docker-run:
          cmd: make -f Makefile.docker check-build-image-manifest-up-to-date
      - run: mkdir bin; mv eksctl "bin/eksctl-$(./eksctl version)"
      - save-cache
      - store_test_results:
          path: ./test-results
      - store_artifacts:
          path: ./test-results
      - store_artifacts:
          path: ./bin
      - notify-slack:
          only_for_branches: main
  build-all-distros:
    machine:
      image: ubuntu-2004:202104-01
    environment:
      GOPRIVATE: github.com/weaveworks/aws-sdk-go-private
    steps:
      - checkout
      - add_ssh_keys:
          fingerprints:
            - "81:df:39:29:89:d6:34:ed:2e:4e:70:71:f1:f3:f6:0c"
      - prepare-ssh-keys
      - restore-cache
      - docker-pull-build-image
      - docker-run:
          cmd: make build-all
      - docker-run:
          cmd: make -f Makefile.docker eksctl-image
          options: >-
            --env=EKSCTL_IMAGE_VERSION=<< pipeline.git.branch >>
            --volume=/var/run/docker.sock:/var/run/docker.sock
      - store_artifacts:
          path: dist/
      - notify-slack:
          only_for_branches: main
  release-candidate:
    machine:
      image: ubuntu-2004:202104-01
    steps:
      - checkout
      - restore-cache
      - docker-pull-build-image
      - docker-run:
          cmd: go mod download
      - docker-run:
          cmd: ./build/scripts/do-release-candidate.sh
          timeout: 21m
          options: >-
            --env=GITHUB_TOKEN
            --env=CIRCLE_TAG
            --env=CIRCLE_PROJECT_USERNAME
            --env=CIRCLE_PROJECT_REPONAME
            --env=DOCKER_HUB_PASSWORD
            --volume=/var/run/docker.sock:/var/run/docker.sock
      - store_artifacts:
          path: dist/
      - notify-slack:
          channel: aws-dev
          fail_only: false
          only_for_branches: ""
          failure_message: ':red_circle: `eksctl` $CIRCLE_TAG release failed!'
          success_message: ':tada: `eksctl` $CIRCLE_TAG released!'
  release:
    machine:
      image: ubuntu-2004:202104-01
    steps:
      - checkout
      - restore-cache
      - docker-pull-build-image
      - docker-run:
          cmd: go mod download
      - docker-run:
          cmd: ./build/scripts/do-release.sh
          timeout: 21m
          options: >-
            --env=GITHUB_TOKEN
            --env=CIRCLE_TAG
            --env=CIRCLE_PROJECT_USERNAME
            --env=CIRCLE_PROJECT_REPONAME
            --env=DOCKER_HUB_PASSWORD
            --volume=/var/run/docker.sock:/var/run/docker.sock
      - store_artifacts:
          path: dist/
      - notify-slack:
          channel: aws-dev
          fail_only: false
          only_for_branches: ""
          failure_message: ':red_circle: `eksctl` $CIRCLE_TAG release failed!'
          success_message: ':tada: `eksctl` $CIRCLE_TAG released!'

workflows:
  version: 2
  # Uncomment this to create binaries for all distros in a PR
#  build-all:
#    jobs:
#      - build-all-distros: # Build locally with gorelease
#          filters:
#            tags:
#              ignore: /.*/
  release:
    jobs:
      - test-and-build:
          filters:
            branches:
              ignore: /.*/
            tags:
              ignore: /^latest_release$/
      - release-candidate:
          requires: [test-and-build]
          filters:
            branches:
              ignore: /.*/
            tags:
              only: /^[0-9]+\.[0-9]+\.[0-9]+-rc\.[0-9]+/
      - release:
          requires: [test-and-build]
          filters:
            branches:
              ignore: /.*/
            tags:
              only: /^[0-9]+\.[0-9]+\.[0-9]+/
  nightly-build:
    triggers:
      - schedule:
          cron: "0 9 * * *"
          filters:
            branches:
              only:
                - main
    jobs:
      - build-all-distros
