version: 2
jobs:
  build:
    docker:
      - image: circleci/golang:1.11.0
    working_directory: ~/fdk-go
    steps:
      - checkout
      - setup_remote_docker:
          docker_layer_caching: true
      - run: docker version
      - run: docker pull fnproject/fnserver
      # installing Fn CLI and starting the Fn server
      - run:
          command: |
            curl -LSs https://raw.githubusercontent.com/fnproject/cli/master/install | sh
      - run:
          command: fn build
          working_directory: examples/hello
      - run:
          command: docker build -t fnproject/fdk-go-init .
          working_directory: images/init
      - run:
          command: docker build -t fnproject/fdk-go-build .
          working_directory: images/build
      - run:
          command: docker build -t fnproject/fdk-go-runtime .
          working_directory: images/runtime
      - deploy:
          command: |
            if [[ "${CIRCLE_BRANCH}" == "master" && -z "${CIRCLE_PR_REPONAME}" ]]; then
              func_version=$(awk '/^version:/ { print $2; }' func.yaml)
              printenv DOCKER_PASS | docker login -u ${DOCKER_USER} --password-stdin
              git config --global user.email "ci@fnproject.com"
              git config --global user.name "CI"
              git branch --set-upstream-to=origin/${CIRCLE_BRANCH} ${CIRCLE_BRANCH}
              docker tag "hello:${func_version}" "fnproject/fdk-go-hello:${func_version}"
              docker tag "hello:${func_version}" "fnproject/fdk-go-hello:latest"
              docker push "fnproject/fdk-go-hello:${func_version}"
              docker push "fnproject/fdk-go-hello:latest"
            fi
          working_directory: examples/hello
