name: main-release

concurrency:
  group: main-release
  cancel-in-progress: true

on:
  push:
    branches:
    - main

permissions:
  contents: write
  packages: write

jobs:
  main-release:
    runs-on: buildjet-16vcpu-ubuntu-2004
    steps:
      - name: Checkout
        uses: actions/checkout@v3
        with:
          fetch-depth: 0
      - name: Set up Go
        uses: actions/setup-go@v3
        with:
          go-version: "1.19"
      - name: Set up QEMU
        uses: docker/setup-qemu-action@v2
      - name: Set up Docker Buildx
        id: buildx
        uses: docker/setup-buildx-action@v2
      - name: Login to GitHub Container Registry
        uses: docker/login-action@v2
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}
      - name: Install Cosign
        uses: sigstore/cosign-installer@main
        with:
          cosign-release: "v1.4.1"
      - name: Setup Cosign
        run: |
          echo "${COSIGN_KEY}" > "$GITHUB_WORKSPACE/cosign.key"
        env:
          COSIGN_KEY: ${{ secrets.COSIGN_KEY }}
      - run: make validate-ci
      - name: Run GoReleaser
        uses: goreleaser/goreleaser-action@v4
        with:
          distribution: goreleaser
          version: latest
          args: release --rm-dist --snapshot
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          COSIGN_PASSWORD: ${{ secrets.COSIGN_PASSWORD }}
          ACORN_DNS_ENDPOINT: https://staging-dns.acrn.io/v1
      - name: Push Docker Images
        run: |
          VERSION=v$(cat releases/metadata.json | jq -r .version)
          IMAGES=$(docker images --format "{{.Repository}}:{{.Tag}}" | grep "$VERSION")
          for i in ${IMAGES}; do
            docker push $i
          done
          docker manifest create ghcr.io/acorn-io/acorn:main ${IMAGES}
          docker manifest push ghcr.io/acorn-io/acorn:main
          docker manifest create ghcr.io/acorn-io/acorn:${VERSION} ${IMAGES}
          docker manifest push ghcr.io/acorn-io/acorn:${VERSION}
      - name: Upload to S3
        uses: jakejarvis/s3-sync-action@v0.5.1
        env:
          SOURCE_DIR: releases
          DEST_DIR: cli
          AWS_REGION: ${{ secrets.AWS_REGION }}
          AWS_S3_ENDPOINT: ${{ secrets.AWS_ENDPOINT }}
          AWS_S3_BUCKET: ${{ secrets.AWS_BUCKET }}
          AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY }}
          AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_KEY }}
      - name: report failure to slack
        if: failure()
        id: slack-failure
        uses: slackapi/slack-github-action@v1.23.0
        with:
          channel-id: '${{ secrets.SLACK_BOT_FAILURE_CHANNEL }}'
          slack-message: "❌ Main-Release failed: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}"
        env:
          SLACK_BOT_TOKEN: ${{ secrets.SLACK_BOT_TOKEN }}

