on:
  release:
    types: [released]
name: Update binaries 
jobs:
  update-self-hosted-helm-registry:
    runs-on: ubuntu-latest
    steps:
      - name: Get tag name
        id: tag_name
        run: |
          tag=${GITHUB_TAG/refs\/tags\//}
          echo ::set-output name=tag::$tag
        env:
          GITHUB_TAG: ${{ github.ref }}
      - name: Run workflow
        run: gh workflow run release.yaml --repo porter-dev/porter-self-hosted -f version=${{steps.tag_name.outputs.tag}}
        env:
          GITHUB_TOKEN: ${{ secrets.PORTER_DEV_GITHUB_TOKEN }}  
  push-docker-server-latest:
    runs-on: ubuntu-latest
    steps:
      - name: Get tag name
        id: tag_name
        run: |
          tag=${GITHUB_TAG/refs\/tags\//}
          echo ::set-output name=tag::$tag
        env:
          GITHUB_TAG: ${{ github.ref }}
      - name: Setup docker
        uses: docker/login-action@v1
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}
      - name: Pull versioned server image and push to latest
        run: |
          docker pull porter1/porter:${{steps.tag_name.outputs.tag}}
          docker tag porter1/porter:${{steps.tag_name.outputs.tag}} porter1/porter:latest
          docker push porter1/porter:latest
  push-docker-cli-latest:
    name: Build a new porter-cli docker image
    runs-on: ubuntu-latest
    steps:
      - name: Get tag name
        id: tag_name
        run: |
          tag=${GITHUB_TAG/refs\/tags\//}
          echo ::set-output name=tag::$tag
        env:
          GITHUB_TAG: ${{ github.ref }}
      - name: Configure AWS credentials
        uses: aws-actions/configure-aws-credentials@v1
        with:
          aws-access-key-id: ${{ secrets.ECR_AWS_ACCESS_KEY_ID }}
          aws-secret-access-key: ${{ secrets.ECR_AWS_SECRET_ACCESS_KEY }}
          aws-region: us-east-2
      - name: Login to ECR public
        id: login-ecr
        run: |
          aws ecr-public get-login-password --region us-east-1 | docker login --username AWS --password-stdin public.ecr.aws/o1j4x7p4
      - name: Pull versioned CLI image and push to latest
        run: |
          docker pull public.ecr.aws/o1j4x7p4/porter-cli:${{steps.tag_name.outputs.tag}}
          docker tag public.ecr.aws/o1j4x7p4/porter-cli:${{steps.tag_name.outputs.tag}} public.ecr.aws/o1j4x7p4/porter-cli:latest
          docker push public.ecr.aws/o1j4x7p4/porter-cli:latest
  update-homebrew-repo:
    name: Update the Homebrew repo with the new CLI version
    runs-on: ubuntu-latest
    steps:
      - name: Get tag name
        id: tag_name
        run: |
          tag=${GITHUB_TAG/refs\/tags\//}
          echo ::set-output name=tag::$tag
        env:
          GITHUB_TAG: ${{ github.ref }}
      - name: Create and commit porter.rb file
        run: |
          version=${{steps.tag_name.outputs.tag}}
          name=porter_${{steps.tag_name.outputs.tag}}_Darwin_x86_64.zip
          curl -L https://github.com/porter-dev/porter/releases/download/${version}/porter_${version}_Darwin_x86_64.zip --output $name

          sha=$(cat porter_${{steps.tag_name.outputs.tag}}_Darwin_x86_64.zip | openssl sha256 | sed 's/(stdin)= //g')

          cat >porter.rb <<EOL
          class Porter < Formula
            homepage "https://porter.run"
            version "${{steps.tag_name.outputs.tag}}"
          
            on_macos do
              url "https://github.com/porter-dev/porter/releases/download/${{steps.tag_name.outputs.tag}}/porter_${{steps.tag_name.outputs.tag}}_Darwin_x86_64.zip"
              sha256 "$sha"
          
              def install
                bin.install "porter"
              end
            end
          end
          EOL
      - name: Add and commit porter.rb file
        run: |
          git clone https://abelanger5:${{ secrets.HOMEBREW_GITHUB_TOKEN }}@github.com/porter-dev/homebrew-porter

          cd homebrew-porter
          git config user.name "Update Bot"
          git config user.email "support@porter.run"

          mv ../porter.rb ./Formula/porter.rb

          git add Formula
          git commit -m "Update to version ${{steps.tag_name.outputs.tag}}"
          git push origin main
