name: Maybe release
on:
  push:
    branches: 
      - main 
jobs:
  # elixir_release:
  #   runs-on: ubuntu-latest
  #   container: elixir:alpine
  #   steps:
  #     - uses: actions/checkout@v2
  #     - name: Install system deps
  #       run: apk add git npm mailcap ca-certificates openssl-dev tzdata gettext curl rust cargo make gcc libc-dev
  #     - name: Install hex
  #       run: mix local.hex --force
  #     - name: Install rebar
  #       run: mix local.rebar --force
  #     - name: Install Elixir dependencies
  #       run: mix deps.get --only prod
  #     - name: Install JS dependencies
  #       run: make js.deps.get
  #     - name: Prepare release
  #       run: |
  #         mix compile
  #         mix assets.release
  #         mix phx.digest
  #         mix release
  #       env:
  #         MIX_ENV: prod
  #     - name: Publish release 
  #       uses: mayel/actions/bin/ghr@master
  #       env:
  #         GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
  #         RELEASE_PATH: _build/prod/rel
  #         CI: 1
  coordination_flavour_docker_release_build_push:
    name: Coordination flavour - Maybe tag version & release Docker image
    runs-on: ubuntu-latest
    env:
      FLAVOUR: coordination
      FLAVOUR_PATH: flavours/coordination
      DOCKER_REPO: bonfire
    steps:
      - 
        name: Checkout
        uses: actions/checkout@v2
        with:
          fetch-depth: 2 # needed for action-detect-and-tag-new-version
      - 
        name: Maybe tag/release new version
        id: version
        uses: salsify/action-detect-and-tag-new-version@v2
        with:
          version-command: |
            grep -m 1 'version:' mix.exs | cut -d '"' -f2
      - 
        if: steps.version.outputs.current-version == steps.version.outputs.previous-version
        name: Cancel workflow if the version has not changed
        uses: andymckay/cancel-action@0.2
      # - 
      #   name: Get branch names
      #   id: branch-name
      #   uses: tj-actions/branch-names@v4
      -
        name: Set up QEMU
        uses: docker/setup-qemu-action@v1
      -
        name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v1
      -
        name: Login to DockerHub
        uses: docker/login-action@v1 
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}
      - 
        name: Cancel any already running releases workflows
        uses: styfle/cancel-workflow-action@0.9.0
        with:
          access_token: ${{ github.token }}
      - 
        name: Pre-build prep 
        run: 	mkdir forks/ && mkdir -p data/uploads/ && make rel.config.prepare && touch data/current_flavour/config/deps.path
      -
        name: Build and push
        id: docker_build
        uses: docker/build-push-action@v2
        env:
          FLAVOUR: coordination
          FLAVOUR_PATH: data/current_flavour
        with:
          context: .
          file: Dockerfile.release
          platforms: linux/amd64 #,linux/arm64,linux/arm/v7
          push: true
          tags: |
            bonfirenetworks/${{ env.DOCKER_REPO }}:latest-${{ env.FLAVOUR }}
            bonfirenetworks/${{ env.DOCKER_REPO }}:${{steps.version.outputs.current-version}}-${{ env.FLAVOUR }}
      -
        name: Image digest
        run: echo ${{ steps.docker_build.outputs.digest }}
  reflow_flavour_docker_release_build_push:
    name: Reflow flavour - Maybe release Docker image
    runs-on: ubuntu-latest
    env:
      FLAVOUR: reflow
      FLAVOUR_PATH: flavours/reflow
      DOCKER_REPO: reflow
      APP_NAME: reflow
    steps:
      - 
        name: Checkout
        uses: actions/checkout@v2
        with:
          fetch-depth: 2 # needed for action-detect-and-tag-new-version
      - 
        name: Maybe tag/release new version
        id: version
        uses: salsify/action-detect-and-tag-new-version@v2
        with:
          create-tag: false # tag already created in first job
          version-command: |
            grep -m 1 'version:' mix.exs | cut -d '"' -f2
      - 
        if: steps.version.outputs.current-version == steps.version.outputs.previous-version
        name: Cancel workflow if the version has not changed
        uses: andymckay/cancel-action@0.2
      # - 
      #   name: Get branch names
      #   id: branch-name
      #   uses: tj-actions/branch-names@v4
      -
        name: Set up QEMU
        uses: docker/setup-qemu-action@v1
      -
        name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v1
      -
        name: Login to DockerHub
        uses: docker/login-action@v1 
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}
      - 
        name: Cancel any already running releases workflows
        uses: styfle/cancel-workflow-action@0.9.0
        with:
          access_token: ${{ github.token }}
      - 
        name: Pre-build prep 
        run: 	mkdir forks/ && mkdir -p data/uploads/ && make rel.config.prepare && touch data/current_flavour/config/deps.path
      -
        name: Build and push
        id: docker_build
        uses: docker/build-push-action@v2
        env:
          FLAVOUR: reflow
          FLAVOUR_PATH: data/current_flavour
        with:
          context: .
          file: Dockerfile.release
          platforms: linux/amd64 #,linux/arm64,linux/arm/v7
          push: true
          tags: |
            bonfirenetworks/${{ env.DOCKER_REPO }}:latest
            bonfirenetworks/${{ env.DOCKER_REPO }}:${{steps.version.outputs.current-version}}
      -
        name: Image digest
        run: echo ${{ steps.docker_build.outputs.digest }}
