version: 2.1
workflows:

  # Defines a workflow for building and pushing updated documentation to GitHub pages
  build-documentation:
    jobs:
      - build-and-deploy-gh-pages:
          filters:
            branches:
              only: master
  
  # Defines workflow for when to build the different Docker images
  build-docker-images:
    jobs:
      - build-ubuntu-image:
          context: dockerhub
          filters:
            branches:
              only:
                - dev
                - master
            tags:
              only: /^\d+\.\d+.*/
      - build-alpine-image:
          context: dockerhub
          filters:
            branches:
              only:
                - dev
                - master
            tags:
              only: /^\d+\.\d+.*/
      - build-armhf-image:
          context: dockerhub
          filters:
            branches:
              only:
                - dev
                - master
            tags:
              only: /^\d+\.\d+.*/
      - build-arm64-image:
          context: dockerhub
          filters:
            branches:
              only:
                - dev
                - master
            tags:
              only: /^\d+\.\d+.*/

# Job definitions, used by the workflows.
jobs:
  build-ubuntu-image:
    environment:
      IMAGE_NAME: haugene/transmission-openvpn
    docker:
      - image: circleci/buildpack-deps:stretch
    steps:
      - checkout
      - setup_remote_docker
      - calculate-tag-name:
          distro: ubuntu
      - run:
          name: Build Docker image
          command: docker build -t $IMAGE_NAME:$IMAGE_TAG .
      - run:
          name: Login to Docker Hub
          command: echo $DOCKER_PASS | docker login -u $DOCKER_USER --password-stdin
      - run:
          name: Push image
          command: docker push $IMAGE_NAME:$IMAGE_TAG

  build-alpine-image:
    environment:
      IMAGE_NAME: haugene/transmission-openvpn
    docker:
      - image: circleci/buildpack-deps:stretch
    steps:
      - checkout
      - setup_remote_docker
      - calculate-tag-name:
          distro: alpine
      - run:
          name: Build Docker image
          command: docker build -t $IMAGE_NAME:$IMAGE_TAG -f Dockerfile.alpine .
      - run:
          name: Login to Docker Hub
          command: echo $DOCKER_PASS | docker login -u $DOCKER_USER --password-stdin
      - run:
          name: Push image
          command: docker push $IMAGE_NAME:$IMAGE_TAG

  build-armhf-image:
    environment:
      IMAGE_NAME: haugene/transmission-openvpn
    docker:
      - image: circleci/buildpack-deps:stretch
    steps:
      - checkout
      - setup_remote_docker
      - calculate-tag-name:
          distro: armhf
      - run:
          name: Build Docker image
          command: |
            echo "Un-commenting cross-build instructions in Dockerfile.armhf"
            sed -i 's/^#\(.*cross-build.*\)/\1/' Dockerfile.armhf
            docker build -t $IMAGE_NAME:$IMAGE_TAG -f Dockerfile.armhf .
      - run:
          name: Login to Docker Hub
          command: echo $DOCKER_PASS | docker login -u $DOCKER_USER --password-stdin
      - run:
          name: Push image
          command: docker push $IMAGE_NAME:$IMAGE_TAG

  build-arm64-image:
    environment:
      IMAGE_NAME: haugene/transmission-openvpn
    docker:
      - image: circleci/buildpack-deps:stretch
    steps:
      - checkout
      - setup_remote_docker
      - calculate-tag-name:
          distro: arm64
      - run:
          name: Build Docker image
          command: |
            echo "Un-commenting cross-build instructions in Dockerfile.armhf"
            sed -i 's/^#\(.*cross-build.*\)/\1/' Dockerfile.armhf
            docker build \
              -t $IMAGE_NAME:$IMAGE_TAG \
              -f Dockerfile.armhf \
              --build-arg base_image=balenalib/raspberrypi3-64:stretch .
      - run:
          name: Login to Docker Hub
          command: echo $DOCKER_PASS | docker login -u $DOCKER_USER --password-stdin
      - run:
          name: Push image
          command: docker push $IMAGE_NAME:$IMAGE_TAG

  build-and-deploy-gh-pages:
    docker:
      - image: circleci/python:3.7
    steps:
      - checkout
      - run:
          name: "Set Git name for commits"
          command: git config --global user.name "CircleCI"
      - run:
          name: "Install Mkdocs and Material theme"
          command: pip install --user mkdocs mkdocs-material
      - run:
          name: "Build and push site"
          command: mkdocs gh-deploy --message "Deployed {sha} with MkDocs version {version}. [skip ci]"

commands:
  calculate-tag-name:
    description: "Calculates the target tag name for Docker image"
    parameters:
      distro:
        type: string
    steps:
      - run:
          name: Calculate tag to set for the built Docker image
          command: |
            if [ ! -z $CIRCLE_TAG ]; then
              TAG_VERSION=$CIRCLE_TAG
            elif [ "$CIRCLE_BRANCH" = "master" ]; then
              TAG_VERSION=latest
            else
              TAG_VERSION=$CIRCLE_BRANCH;
            fi

            if [ "<< parameters.distro >>" = "ubuntu" ]; then
              TAG_DISTRO="";
            else
              TAG_DISTRO=-<< parameters.distro >>;
            fi

            echo "export IMAGE_TAG=$TAG_VERSION$TAG_DISTRO" >> $BASH_ENV
