name: Release

on:
  push:
    branches:
      - 'develop'
      - 'main'

concurrency:
  group: mizu-publish-${{ github.ref }}
  cancel-in-progress: true

jobs:
  docker-registry:
    name: Push Docker image to Docker Hub
    runs-on: ubuntu-latest

    strategy:
      max-parallel: 2
      fail-fast: false
      matrix:
        target:
          - amd64
          - arm64v8

    steps:
      - name: Check out the repo
        uses: actions/checkout@v2

      - name: Determine versioning strategy
        uses: haya14busa/action-cond@v1
        id: condval
        with:
          cond: ${{ github.ref == 'refs/heads/main' }}
          if_true: "stable"
          if_false: "dev"

      - name: Auto Increment Ver Action
        uses: docker://igorgov/auto-inc-ver:v2.0.0
        id: versioning
        with:
          mode: ${{ steps.condval.outputs.value }}
          suffix: 'dev'
          github_token: ${{ secrets.GITHUB_TOKEN }}

      - name: Get version parameters
        shell: bash
        run: |
          echo "##[set-output name=build_timestamp;]$(echo $(date +%s))"
          echo "##[set-output name=branch;]$(echo ${GITHUB_REF#refs/heads/})"
        id: version_parameters

      - name: Docker meta
        id: meta
        uses: docker/metadata-action@v3
        with:
          images: |
            up9inc/mizu
          tags: |
            type=raw,${{ steps.versioning.outputs.version }}
            type=raw,value=latest,enable=${{ steps.condval.outputs.value == 'stable' }}
            type=raw,value=dev-latest,enable=${{ steps.condval.outputs.value == 'dev' }}
          flavor: |
            latest=auto
            prefix=
            suffix=-${{ matrix.target }},onlatest=true

      - name: Login to Docker Hub
        uses: docker/login-action@v1
        with:
          username: ${{ secrets.DOCKERHUB_USER }}
          password: ${{ secrets.DOCKERHUB_PASS }}

      - name: Build and push
        uses: docker/build-push-action@v2
        with:
          context: .
          push: true
          tags: ${{ steps.meta.outputs.tags }}
          labels: ${{ steps.meta.outputs.labels }}
          build-args: |
            TARGETARCH=${{ matrix.target }}
            VER=${{ steps.versioning.outputs.version }}
            BUILD_TIMESTAMP=${{ steps.version_parameters.outputs.build_timestamp }}
            GIT_BRANCH=${{ steps.version_parameters.outputs.branch }}
            COMMIT_HASH=${{ github.sha }}

  gcp-registry:
    name: Push Docker image to GCR
    runs-on: ubuntu-latest

    strategy:
      max-parallel: 2
      fail-fast: false
      matrix:
        target:
          - amd64
          - arm64v8

    steps:
      - name: Check out the repo
        uses: actions/checkout@v2

      - id: 'auth'
        uses: 'google-github-actions/auth@v0'
        with:
          credentials_json: '${{ secrets.GCR_JSON_KEY }}'

      - name: 'Set up Cloud SDK'
        uses: 'google-github-actions/setup-gcloud@v0'

      - name: Determine versioning strategy
        uses: haya14busa/action-cond@v1
        id: condval
        with:
          cond: ${{ github.ref == 'refs/heads/main' }}
          if_true: "stable"
          if_false: "dev"

      - name: Auto Increment Ver Action
        uses: docker://igorgov/auto-inc-ver:v2.0.0
        id: versioning
        with:
          mode: ${{ steps.condval.outputs.value }}
          suffix: 'dev'
          github_token: ${{ secrets.GITHUB_TOKEN }}

      - name: Get version parameters
        shell: bash
        run: |
          echo "##[set-output name=build_timestamp;]$(echo $(date +%s))"
          echo "##[set-output name=branch;]$(echo ${GITHUB_REF#refs/heads/})"
        id: version_parameters

      - name: Get base image name
        shell: bash
        run: echo "##[set-output name=image;]$(echo gcr.io/up9-docker-hub/mizu/${GITHUB_REF#refs/heads/})"
        id: base_image_step

      - name: Docker meta
        id: meta
        uses: docker/metadata-action@v3
        with:
          images: |
            ${{ steps.base_image_step.outputs.image }}
          tags: |
            type=raw,${{ steps.versioning.outputs.version }}
            type=raw,value=latest,enable=${{ steps.condval.outputs.value == 'stable' }}
            type=raw,value=dev-latest,enable=${{ steps.condval.outputs.value == 'dev' }}
          flavor: |
            latest=auto
            prefix=
            suffix=-${{ matrix.target }},onlatest=true

      - name: Login to GCR
        uses: docker/login-action@v1
        with:
          registry: gcr.io
          username: _json_key
          password: ${{ secrets.GCR_JSON_KEY }}

      - name: Build and push
        uses: docker/build-push-action@v2
        with:
          context: .
          push: true
          tags: ${{ steps.meta.outputs.tags }}
          labels: ${{ steps.meta.outputs.labels }}
          build-args: |
            TARGETARCH=${{ matrix.target }}
            VER=${{ steps.versioning.outputs.version }}
            BUILD_TIMESTAMP=${{ steps.version_parameters.outputs.build_timestamp }}
            GIT_BRANCH=${{ steps.version_parameters.outputs.branch }}
            COMMIT_HASH=${{ github.sha }}

  docker-manifest:
    name: Create and Push a Docker Manifest
    runs-on: ubuntu-latest
    needs: [docker-registry]
    steps:
      - name: Determine versioning strategy
        uses: haya14busa/action-cond@v1
        id: condval
        with:
          cond: ${{ github.ref == 'refs/heads/main' }}
          if_true: "stable"
          if_false: "dev"

      - name: Auto Increment Ver Action
        uses: docker://igorgov/auto-inc-ver:v2.0.0
        id: versioning
        with:
          mode: ${{ steps.condval.outputs.value }}
          suffix: 'dev'
          github_token: ${{ secrets.GITHUB_TOKEN }}

      - name: Get version parameters
        shell: bash
        run: |
          echo "##[set-output name=build_timestamp;]$(echo $(date +%s))"
          echo "##[set-output name=branch;]$(echo ${GITHUB_REF#refs/heads/})"
        id: version_parameters

      - name: Docker meta
        id: meta
        uses: docker/metadata-action@v3
        with:
          images: |
            up9inc/mizu
          tags: |
            type=raw,${{ steps.versioning.outputs.version }}
            type=raw,value=latest,enable=${{ steps.condval.outputs.value == 'stable' }}
            type=raw,value=dev-latest,enable=${{ steps.condval.outputs.value == 'dev' }}
      - name: Login to Docker Hub
        uses: docker/login-action@v1
        with:
          username: ${{ secrets.DOCKERHUB_USER }}
          password: ${{ secrets.DOCKERHUB_PASS }}

      - name: Create manifest
        run: |
          while IFS= read -r line; do
            docker manifest create $line --amend $line-amd64 --amend $line-arm64v8
          done <<< "${{ steps.meta.outputs.tags }}"

      - name: Push manifest
        run: |
          while IFS= read -r line; do
            docker manifest push $line
          done <<< "${{ steps.meta.outputs.tags }}"

  cli:
    name: Build the CLI and publish
    runs-on: ubuntu-latest
    needs: [docker-manifest, gcp-registry]
    steps:
      - name: Set up Go 1.17
        uses: actions/setup-go@v2
        with:
          go-version: '1.17'

      - name: Check out the repo
        uses: actions/checkout@v2

      - id: 'auth'
        uses: 'google-github-actions/auth@v0'
        with:
          credentials_json: '${{ secrets.GCR_JSON_KEY }}'

      - name: 'Set up Cloud SDK'
        uses: 'google-github-actions/setup-gcloud@v0'

      - name: Determine versioning strategy
        uses: haya14busa/action-cond@v1
        id: condval
        with:
          cond: ${{ github.ref == 'refs/heads/main' }}
          if_true: "stable"
          if_false: "dev"

      - name: Auto Increment Ver Action
        uses: docker://igorgov/auto-inc-ver:v2.0.0
        id: versioning
        with:
          mode: ${{ steps.condval.outputs.value }}
          suffix: 'dev'
          github_token: ${{ secrets.GITHUB_TOKEN }}

      - name: Get version parameters
        shell: bash
        run: |
          echo "##[set-output name=build_timestamp;]$(echo $(date +%s))"
          echo "##[set-output name=branch;]$(echo ${GITHUB_REF#refs/heads/})"
        id: version_parameters

      - name: Build and Push CLI
        run: make push-cli VER='${{ steps.versioning.outputs.version }}' BUILD_TIMESTAMP='${{ steps.version_parameters.outputs.build_timestamp }}'

      - name: Log the version into a .txt file
        shell: bash
        run: |
          echo '${{ steps.versioning.outputs.version }}' >> cli/bin/version.txt

      - name: Release
        uses: ncipollo/release-action@v1
        with:
          token: ${{ secrets.GITHUB_TOKEN }}
          artifacts: "cli/bin/*"
          commit: ${{ steps.version_parameters.outputs.branch }}
          tag: ${{ steps.versioning.outputs.version }}
          prerelease: ${{ github.ref != 'refs/heads/main' }}
          bodyFile: 'cli/bin/README.md'
