name: Maybe release
on:
  push:
    branches: 
      - main 
jobs:
  # elixir_release: 
  #   name: Elixir/OTP release without Docker
  #   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 cargojust 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: just js.deps.get
  #     - name: Prepare release
  #       run: |
  #         mix compile
  #         mix assets.build
  #         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

  classic_flavour_common_docker_release_build_push:
    name: Classic flavour - multiarch - Maybe tag version & release Docker image
    runs-on: ubuntu-latest
    permissions: write-all
    env:
      FLAVOUR: classic
      FLAVOUR_PATH: flavours/classic
      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: Create tags in extensions repos
        uses: bonfire-networks/create-tags-multirepo@v0.6
        with:
          version: ${{ steps.version.outputs.current-version }}
          message: ${{ github.event.inputs.message }}
          token: ${{ secrets.GH_TOKEN }}
          owner: "bonfire-networks"
          repos: "bonfire_api_graphql,bonfire_data_identity,bonfire_fail,bonfire_ui_me,bonfire_boundaries,bonfire_data_shared_user,bonfire_federate_activitypub,bonfire_quantify,bonfire_breadpub,bonfire_data_social,bonfire_files,bonfire_recyclapp,bonfire_ui_social,bonfire_classify,bonfire_geolocate,bonfire_search,bonfire_ui_valueflows,bonfire_common,bonfire_ecto,bonfire_invite_links,bonfire_social,bonfire_data_access_control,bonfire_editor_ck,bonfire_livebook,bonfire_tag,bonfire_valueflows,bonfire_data_activity_pub,bonfire_editor_quill,bonfire_mailer,bonfire_valueflows_api_schema,bonfire_data_assort,bonfire_me,bonfire_ui_common,bonfire_valueflows_observe,bonfire_epics,bonfire_notify,bonfire_ui_coordination,bonfire_data_edges,bonfire_extension_template,bonfire_open_id,bonfire_ui_kanban"
          # bonfire_upcycle,bonfire_publisher_thesis,bonfire_ui_reflow,bonfire_data_tasks,bonfire_taxonomy_seeder,bonfire_encryption_backend,bonfire_pages,bonfire_web_phoenix,bonfire_website
      # - 
      #   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: sudo apt-get install -y cargo && cargo install just && echo "/github/home/.cargo/bin" >> $GITHUB_PATH && just rel-prepare
      -
        name: Build and push
        id: docker_build
        uses: docker/build-push-action@v2
        env:
          FLAVOUR: classic
          FLAVOUR_PATH: data/current_flavour
        with:
          context: .
          file: Dockerfile.release
          # platforms: linux/amd64
          # platforms: linux/amd64,linux/arm64
          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 }}

# FIXME: tried running this separately with the same Docker tag to not slow down the release (currently about 10 min for x86, 40 min for x86+arm64, and several hours if you add armv7), but putting this as a separate task overwrites the image pushed to docker hub by the first one

  classic_flavour_common_docker_release_build_push_x86:
    name: Classic flavour for x86 - Maybe tag version & release Docker image
    runs-on: ubuntu-latest
    env:
      FLAVOUR: classic
      FLAVOUR_PATH: flavours/classic
      DOCKER_REPO: bonfire
    steps:
      - 
        name: Checkout
        uses: actions/checkout@v2
        with:
          fetch-depth: 2 # needed for action-detect-and-tag-new-version
      - 
        name: Detect 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: sudo apt-get install -y cargo && cargo install just && echo "/github/home/.cargo/bin" >> $GITHUB_PATH && just rel-prepare
      -
        name: Build and push
        id: docker_build
        uses: docker/build-push-action@v2
        env:
          FLAVOUR: classic
          FLAVOUR_PATH: data/current_flavour
        with:
          context: .
          file: Dockerfile.release
          platforms: linux/amd64
          # platforms: linux/amd64,linux/arm64
          # platforms: linux/amd64,linux/arm64,linux/arm/v7
          push: true
          tags: |
            bonfirenetworks/${{ env.DOCKER_REPO }}:latest-${{ env.FLAVOUR }}-x86
            bonfirenetworks/${{ env.DOCKER_REPO }}:${{steps.version.outputs.current-version}}-${{ env.FLAVOUR }}-x86
      -
        name: Image digest
        run: echo ${{ steps.docker_build.outputs.digest }}


  cooperation_flavour_docker_release_build_push:
    name: Cooperation flavour - multiarch - Maybe release Docker image
    runs-on: ubuntu-latest
    env:
      FLAVOUR: cooperation
      FLAVOUR_PATH: flavours/cooperation
      DOCKER_REPO: bonfire
    steps:
      - 
        name: Checkout
        uses: actions/checkout@v2
        with:
          fetch-depth: 2 # needed for action-detect-and-tag-new-version
      - 
        name: Detect 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
      -
        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: Pre-build prep 
        run: sudo apt-get install -y cargo && cargo install just && echo "/github/home/.cargo/bin" >> $GITHUB_PATH && just rel-prepare
      -
        name: Build and push
        id: docker_build
        uses: docker/build-push-action@v2
        env:
          FLAVOUR: cooperation
          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: Detect 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
  #     -
  #       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: Pre-build prep 
  #       run: sudo apt-get install -y cargo && cargo install just && echo "/github/home/.cargo/bin" >> $GITHUB_PATH && just rel-prepare
  #     -
  #       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
  #         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 }}
