name: Release
on:
  push:
    tags:
      - "v*"
env:
  GO_VERSION: "1.16"
  GH_USER: "aqua-bot"
jobs:
  release:
    name: Release
    runs-on: ubuntu-18.04 # 20.04 doesn't provide createrepo for now
    env:
      DOCKER_CLI_EXPERIMENTAL: "enabled"
    steps:
      - name: Install dependencies
        run: |
          sudo apt-get -y update
          sudo apt-get -y install rpm reprepro createrepo distro-info
      - name: Set up QEMU
        uses: docker/setup-qemu-action@v1
      - name: Set up Docker Buildx
        id: buildx
        uses: docker/setup-buildx-action@v1
      - name: Show available Docker Buildx platforms
        run: echo ${{ steps.buildx.outputs.platforms }}
      - name: Setup Go
        uses: actions/setup-go@v2
        with:
          go-version: ${{ env.GO_VERSION }}
      - name: Checkout code
        uses: actions/checkout@v2
        with:
          fetch-depth: 0
      - name: Cache Go modules
        uses: actions/cache@v2
        with:
          path: ~/go/pkg/mod
          key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
          restore-keys: |
            ${{ runner.os }}-go-
      - name: Login to docker.io registry
        uses: docker/login-action@v1
        with:
          username: ${{ secrets.DOCKERHUB_USER }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}
      - name: Login to ghcr.io registry
        uses: docker/login-action@v1
        with:
          registry: ghcr.io
          username: ${{ env.GH_USER }}
          password: ${{ secrets.ORG_GITHUB_TOKEN }}
      - name: Login to ECR
        uses: docker/login-action@v1
        with:
          registry: public.ecr.aws
          username: ${{ secrets.ECR_ACCESS_KEY_ID }}
          password: ${{ secrets.ECR_SECRET_ACCESS_KEY }}
      - name: Release
        uses: goreleaser/goreleaser-action@v2
        with:
          version: v0.164.0
          args: release --rm-dist
        env:
          GITHUB_TOKEN: ${{ secrets.ORG_GITHUB_TOKEN }}
      - name: Checkout trivy-repo
        uses: actions/checkout@v2
        with:
          repository: ${{ github.repository_owner }}/trivy-repo
          path: trivy-repo
          fetch-depth: 0
          token: ${{ secrets.ORG_GITHUB_TOKEN }}
      - name: Setup git settings
        run: |
          git config --global user.email "knqyf263@gmail.com"
          git config --global user.name "Teppei Fukuda"
      - name: Create rpm repository
        run: ci/deploy-rpm.sh
      - name: Import GPG key
        run: echo -e "${{ secrets.GPG_KEY }}" | gpg --import
      - name: Create deb repository
        run: ci/deploy-deb.sh
