# CircleCI 2.0 configuration file
#
# Check https://circleci.com/docs/2.0/configuration-reference/ for more details
# See https://circleci.com/docs/2.0/config-intro/#section=configuration for spec
#
version: 2.1

# Commands

commands:

  pull_cache:
    description: Pulls Quay.io docker images (latest) for our cache
    parameters:
      tag:
        type: string
        default: "devel"
    steps:
      - run: echo "Tag is << parameters.tag >>"
      - run: docker pull quay.io/nushell/nu:<< parameters.tag >>
      - run: docker pull quay.io/nushell/nu-base:<< parameters.tag >>

orbs:
  # https://circleci.com/orbs/registry/orb/circleci/docker
  docker: circleci/docker@0.5.13

workflows:
  version: 2.0

  # This builds on all pull requests to test, and ignores main
  build_without_deploy:
    jobs:
      - docker/publish:
          deploy: false
          image: nushell/nu-base
          tag: latest
          dockerfile: docker/Dockerfile.nu-base
          extra_build_args: --cache-from=quay.io/nushell/nu-base:devel
          filters:
            branches:
              ignore:
                - main
          before_build:
            - pull_cache
          after_build:
            - run:
                name: Build Multistage (smaller) container
                command: |
                  docker build -f docker/Dockerfile -t quay.io/nushell/nu .
            - run:
                name: Preview Docker Tag for Nushell Build
                command: |
                   DOCKER_TAG=$(docker run quay.io/nushell/nu --version | cut -d' ' -f2)
                   echo "Version that would be used for Docker tag is v${DOCKER_TAG}"
            - run:
                name: Test Executable
                command: |
                   docker run --rm quay.io/nushell/nu-base --help
                   docker run --rm quay.io/nushell/nu --help

  # workflow publishes to Docker Hub, with each job having different triggers
  build_with_deploy:
    jobs:

        # Deploy versioned and latest images on tags (releases) only - builds --release.
      - docker/publish:
          image: nushell/nu-base
          registry: quay.io
          tag: latest
          dockerfile: docker/Dockerfile.nu-base
          extra_build_args: --cache-from=quay.io/nushell/nu-base:latest,quay.io/nushell/nu:latest --build-arg RELEASE=true
          filters:
            branches:
              ignore: /.*/
            tags:
              only: /^\d+\.\d+\.\d+$/
          before_build:
            - run: docker pull quay.io/nushell/nu:latest
            - run: docker pull quay.io/nushell/nu-base:latest
          after_build:
            - run:
                name: Build Multistage (smaller) container
                command: |
                  docker build -f docker/Dockerfile -t quay.io/nushell/nu .
            - run:
                name: Test Executable
                command: |
                   docker run --rm quay.io/nushell/nu --help
                   docker run --rm quay.io/nushell/nu-base --help
            - run:
                name: Publish Docker Tag with Nushell Version
                command: |
                   DOCKER_TAG=$(docker run quay.io/nushell/nu --version | cut -d' ' -f2)
                   echo "Version for Docker tag is ${DOCKER_TAG}"
                   docker tag quay.io/nushell/nu-base:latest quay.io/nushell/nu-base:${DOCKER_TAG}
                   docker tag quay.io/nushell/nu:latest quay.io/nushell/nu:${DOCKER_TAG}
                   docker push quay.io/nushell/nu-base
                   docker push quay.io/nushell/nu


  # publish devel to Docker Hub on merge to main (doesn't build --release)
  build_with_deploy_devel:
    jobs:

      # Deploy devel tag on merge to main
      - docker/publish:
          image: nushell/nu-base
          registry: quay.io
          tag: devel
          dockerfile: docker/Dockerfile.nu-base
          extra_build_args: --cache-from=quay.io/nushell/nu-base:devel
          before_build:
            - pull_cache
          filters:
            branches:
              only: main
          after_build:
            - run:
                name: Build Multistage (smaller) container
                command: |
                  docker build --build-arg FROMTAG=devel -f docker/Dockerfile -t quay.io/nushell/nu:devel .
            - run:
                name: Test Executable
                command: |
                   docker run --rm quay.io/nushell/nu:devel --help
                   docker run --rm quay.io/nushell/nu-base:devel --help
            - run:
                name: Publish Development Docker Tags
                command: |
                   docker push quay.io/nushell/nu-base:devel
                   docker push quay.io/nushell/nu:devel

  nightly:
    triggers:
      - schedule:
          cron: "0 0 * * *"
          filters:
            branches:
              only:
                - main
    jobs:
      - docker/publish:
          image: nushell/nu-base
          registry: quay.io
          tag: nightly
          dockerfile: docker/Dockerfile.nu-base
          extra_build_args: --cache-from=quay.io/nushell/nu-base:nightly --build-arg RELEASE=true
          before_build:
            - run: docker pull quay.io/nushell/nu:nightly
            - run: docker pull quay.io/nushell/nu-base:nightly
          after_build:
            - run:
                name: Build Multistage (smaller) container
                command: |
                  docker build -f docker/Dockerfile -t quay.io/nushell/nu:nightly .
            - run:
                name: Test Executable
                command: |
                   docker run --rm quay.io/nushell/nu:nightly --help
                   docker run --rm quay.io/nushell/nu-base:nightly --help
            - run:
                name: Publish Nightly Nushell Containers
                command: |
                   docker push quay.io/nushell/nu-base:nightly
                   docker push quay.io/nushell/nu:nightly
